大佬教程收集整理的这篇文章主要介绍了vb.net静态动态调用c++dll的方法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
vb.net静态调用c++dll的方法:
在新建的工程中,add一个@H_817_1@module,添加声明函数等代码。
如: Public Declare Function AdvDVP_Start Lib "xxx.dll" (ByVal nDevNum As Long,ByVal SwitchingChans As Long,ByVal Main As Long,ByVal hwandPreview As Long) As Long
Xxx为待测试的c++的dll名称。
主程序,直接使用就好了。
当然,也可以用工具直接从c#转到vb.net,哈哈
vb.net动态调用c++dll的方法:
在工程中添加一个class。在文件中,添加如下代码:
在Public Class Class1前,添加如下:
Imports System.Runtime.Interopservices ' 用DllImport 需用此命名空间
Imports System.Reflection ' 使用Assembly 类需用此命名空间
Imports System.Reflection.Emit ' 使用ILGenerator 需用此命名空间
Public Class Class1中,添加如下代码:
Private Declare Auto Function LoadLibrary Lib "kernel32" _
(ByVal LibFilePath As String) As Integer
Private Declare Function GetProcAddress Lib "kernel32" _
(ByVal ModuleHandle As Integer,ByVal ProcName As String) _
As Integer
Private Declare Function FREELIbrary Lib "kernel32" _
(ByVal ModuleHandle As Integer) As Integer
Public Enum ModePass
ByValue = &H1
ByAdd = &H2
End Enum
Private hmodule As IntPtr = IntPtr.Zero
Private farProc As IntPtr = IntPtr.Zero
Public Sub LoadDll(ByVal lpFilename As String)
hmodule = LoadLibrary(lpFileName)
If hmodule = IntPtr.Zero Then
Console.WriteLine("没有找到:" & lpFileName)
Exit Sub
Else
Console.WriteLine("装入:" & lpFilename & "成功!")
End If
End Sub
Public Sub LoadFun(ByVal lpProcName As String)
If hmodule = IntPtr.Zero Then
Console.WriteLine("函数库模块的句柄为空,请确保已进行LoadDll 操作")
Exit Sub
Else
farProc = GetProcAddress(hmodule,lpProcName)
If farProc = IntPtr.Zero Then
Console.WriteLine("没有找到:" & lpProcName & " 这个函数的入口点")
Exit Sub
Else
Console.WriteLine("找到:" & lpProcName & " 这个函数的入口点,并载入成功!")
End If
End If
End Sub
Public Sub UnLoadDll()
@H_867_102@me.hmodule = IntPtr.Zero
@H_867_102@me.farProc = IntPtr.Zero
Console.WriteLine("清除完毕!")
End Sub
Public Function Invoke(ByVal ObjArray_Parameter As Object(),ByVal TypeArray_ParameterType As Type(),ByVal ModePassArray_Parameter As ModePass(),ByVal Type_Return As TypE) As Object
Dim MyAssemblyName As New AssemblyName
Dim MyAssemblyBuilder As AssemblyBuilder
Dim MymoduleBuilder As ModuleBuilder
Dim MymethodBuilder As MethodBuilder
Dim IL As ILGenerator
Dim MymethodInfo As MethodInfo
If hmodule = IntPtr.Zero Then
Throw (New Exception(" 函数库模块的句柄为空,请确保已进行LoadDll 操作!"))
End If
If farProc = IntPtr.Zero Then
Throw (New Exception(" 函数指针为空,请确保已进行LoadFun 操作!"))
End If
If ObjArray_Parameter.Length <> ModePassArray_Parameter.Length Then
Throw (New Exception(" 参数个数及其传递方式的个数不匹配."))
End If
'
Dim i As Integer
@H_355_2@myAssemblyName.Name = "InvokeFun" '程序集的名称
@H_355_2@myAssemblyBuilder = AppDomain.CurrentDomain.DefineDynamiCassembly(MyAssemblyName,AssemblyBuilderAccess.Run) '使用指定的名称、访问模式和自定义属性定义动态程序集
@H_355_2@mymoduleBuilder = MyAssemblyBuilder.DefineDynamicModule("InvokeDll")
@H_355_2@mymethodBuilder = MymoduleBuilder.DefineGlobalMethod("MyFun",MethodAttributes.Public Or MethodAttributes.Static,Type_Return,TypeArray_ParameterTypE)
IL = MymethodBuilder.GetILGenerator()
For i = 0 To ObjArray_Parameter.Length - 1
SELEct Case ModePassArray_Parameter(i)
Case ModePass.byValue
Exit For
Case ModePass.byAdd
Exit For
Case Else
Throw (New Exception(" 第" + (i + 1).ToString() + " 个参数没有给定正确的传递方式."))
End SELEct
Next
If IntPtr.Size = 4 Then
Il.Emit(OpCodes.Ldc_I4,farProc.ToInt32())
ElseIf IntPtr.Size = 8 Then
Il.Emit(OpCodes.Ldc_I8,farProc.ToInt64())
Else
Throw New PlatformNotSupportedException()
End If
Il.EmitCalli(OpCodes.Calli,CallingConvention.StdCall,TypeArray_ParameterTypE)
@H_355_2@mymoduleBuilder.CreateGlobalFunctions()
@H_355_2@mymethodInfo = MymoduleBuilder.GetMethod("MyFun")
Dim ret As Integer
Try
ret = MymethodInfo.Invoke(Nothing,ObjArray_Parameter) '调用方法,并返回其值
Catch ex As Exception
End Try
'Return MymethodInfo.Invoke(Nothing,ObjArray_Parameter) '调用方法,并返回其值
Return ret
End Function
添加一个button,和一个name为txRet的textbox,双击按钮,并添加如下代码:
Dim ret As Integer
Dim myDLD As New Class1
@H_355_2@myDLD.LoadDll("DVP7010B.dll")
@H_355_2@myDLD.LoadFun("AdvDVP_InitSDK")
Dim Parameters() As Object = {}
Dim ParameterTypes() As Type = {}
Dim themode() As Class1.ModePass = {}
Dim Type_Return As Type
Type_Return = GetType(Integer)
ret = myDLD.Invoke(Parameters,ParameterTypes,themode,Type_Return)
txRet.Text = ret.ToString()
If ret <> 1 Then
@H_355_2@msgBox("InitSDK failed !")
End If
If ret = 1 Then
@H_355_2@msgBox("InitSDK sucessed !")
End If
以上是大佬教程为你收集整理的vb.net静态动态调用c++dll的方法全部内容,希望文章能够帮你解决vb.net静态动态调用c++dll的方法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。