如何在运行时使用windows api编译vb.net代码 [英] How do I compile vb.net code in runtime use windows api

查看:78
本文介绍了如何在运行时使用windows api编译vb.net代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
当我使用Windows API时,我想在vb.net中编译VB 6.0中的代码。

我在计算器中使用此方法但是当我使用windows api时,我有错误

总是返回= 0



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

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"}
        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 ?

推荐答案

kosay85写道:
kosay85 wrote:

我以windows API为例使用外部dll文件。一般来说我如何使用带有CodeDOM的dll文件?

I give windows API as an example to use external dll files . in general how I can use dll files with CodeDOM ??

这就是

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 [ ^ ]。



对于带参数的构造函数,第一个是参考数组ed程序集名称。



要查看 CompilerParameters 的位置,请参阅此MDSN页面上的代码示例并阅读整个概述: https://msdn.microsoft.com/ en-us / library / saf5ce06%28v = vs.110%29.aspx [ ^ ]。



有两个重要的事情:1)你永远不会需要使用P / Invoke(来自kernel32.dll或其他任何东西); CodeDOM为您提供编译程序集的所有功能;仅使用P / Invoke会影响您的平台兼容性,因此仅在特殊需要时使用它; 2)使用.NET,您不应该考虑DLL,中心概念是可以使用一个或多个模块的程序集; DLL是其中一个模块; EXE,DLL或其他任何东西之间没有根本区别(EXE也有一个入口点,那又怎么样?);它只是不同的文件命名模式。



-SA

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


这篇关于如何在运行时使用windows api编译vb.net代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆