Click here to Skip to main content
15,881,882 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 vb.net when I use windows API .
I use this method in calculator but when I use windows api , I have error
always return = 0

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

I put this
VB
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"}
        cp.ReferencedAssemblies.AddRange(refs)
        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
        Else
            Return "0"
        End If


where is the problem ?
Posted
Updated 18-Mar-15 17:56pm
v3
Comments
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.
—SA
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?
—SA

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
https://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerparameters%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerparameters.compilerparameters%28v=vs.110%29.aspx[^].

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: https://msdn.microsoft.com/en-us/library/saf5ce06%28v=vs.110%29.aspx[^].

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.

—SA
 
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