Click here to Skip to main content
15,878,003 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi I want to compile code in VB 6.0 in when I use windows API .
I use this method in calculator but when I use windows api , I have error
always return = 0

Private Declare Function GetHandleInformation Lib "kernel32.dll" (ByVal hObject As Long, ByRef lpdwFlags As Long) As Long

I put this
Dim MyProvider As New VBCodeProvider
        Dim cp As New CodeDom.Compiler.CompilerParameters
        Dim y As CodeDom.Compiler.ICodeCompiler
        cp.GenerateExecutable = False
        cp.GenerateInMemory = True
        Dim TempModuleSource As String       
        Dim refs() As String = {"System.dll", "Microsoft.VisualBasic.dll", "mscorlib.dll", "System.Core.dll"}
        TempModuleSource = " Imports System.Runtime.InteropServices" & Environment.NewLine & _
 "Public Class class1" & Environment.NewLine & _
  " Private Declare Function GetHandleInformation Lib " & (34) & "kernel32" & Chr(34) & " (ByVal hObject As Long, ByRef lpdwFlags As Long) As Long " & Environment.NewLine & _
            "Public Function GetHandleInformation ( buval a as long , byval b as long)" & Environment.NewLine & _
            "GetHandleInformation = GetHandleInformation (a,b) " & Environment.NewLine & _           
            "End Function" & Environment.NewLine & _
            "End Class" & Environment.NewLine  
        Dim result As CodeDom.Compiler.CompilerResults = MyProvider.CompileAssemblyFromSource(cp, TempModuleSource)
        If result.Errors.Count = 0 Then
            Dim methInfo As Reflection.MethodInfo = result.CompiledAssembly.GetType("class1").GetMethod("GetSomeThing")
            Return Convert.ToDouble(methInfo.Invoke(Nothing, Nothing)).ToString
            Return "0"
        End If

where is the problem ?
Updated 18-Mar-15 17:56pm
Sergey Alexandrovich Kryukov 18-Mar-15 20:44pm    
Why would you do all that? CodeDOM will compile you the assembly without direct use of Windows API, which would only compromise platform compatibility.
KosayHatem 20-Mar-15 8:24am    
I give windows API as an example to use external dll files . in general how I can use dll files with CodeDOM ??
Sergey Alexandrovich Kryukov 20-Mar-15 9:01am    
Please see my answer. Why P/Invoke?

1 solution

kosay85 wrote:
I give windows API as an example to use external dll files . in general how I can use dll files with CodeDOM ??
This is how[^],[^].

For the constructors with arguments, first one is the array of referenced assembly names.

To see where CompilerParameters come into play, please see the code sample on this MDSN page and read the whole overview:[^].

There are two important things: 1) you never need to use P/Invoke (from "kernel32.dll" or anything else); CodeDOM gives you everything to compile assemblies and a lot more; using P/Invoke only can compromise your platform compatibility, so use it only in special need; 2) with .NET, you should not think in terms of DLLs, the central concept is assembly which can use one or more modules; a DLL is one of the modules; there is no fundamental difference between EXE, DLL or anything else (EXE also has an entry point, so what?); it's just different file naming patterns.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900