如何根据平台加载Dll
我有一个 32 位和 64 位的 dll,现在我希望我的 exe 根据解决方案平台调用该 dll,这意味着当设置 x64 时,将调用 64 位的 dll。为此,我声明了一个函数 GetPlatform()。
Public Function GetPlateform() As String
Dim var1 As String
If (IntPtr.Size = 8) Then
var1 = hellox64
Else
var1 = hello
End If
Return var1
End Function
当表单加载时 这个var1被分配给var,最后。
Public Declare Function function1 Lib "var" (ByVal Id As Integer) As Integer
但是当我调试代码时,出现“DllNotFoundException”。 注意:dll 是用 vc++ 编写的。
I have a dll for 32bit and 64bit and now I want that my exe call the dll from according to solution platform,means when x64 is set then the dll of 64bit will call.For this I declare a function GetPlatform().
Public Function GetPlateform() As String
Dim var1 As String
If (IntPtr.Size = 8) Then
var1 = hellox64
Else
var1 = hello
End If
Return var1
End Function
and when the form load
this var1 is assign to var and finally.
Public Declare Function function1 Lib "var" (ByVal Id As Integer) As Integer
But When I debug the code "DllNotFoundException" is ocuured.
NOTE:The dll is in vc++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将本机 dll 存储到子文件夹中,并通过相应填写 库加载器 来提示 < code>PATH 进程环境变量,包含要加载的正确版本的路径。
例如,给定这个树布局......
...并且此代码(抱歉,C#,没有 VB.Net :-/ )...
...
function1
和function2
将动态绑定到 32或本机代码的 64 位版本,具体取决于 IntPtr 的大小(Scott Hanselman 或这个 StackOverflow问题 )。注 1: 当 dll 的两个版本具有相同的名称或者您不愿意复制每个外部引用时,此解决方案特别有用。
注2:这已经在LibGit2Sharp。
Store your native dlls into subfolders and hint the Library Loader by filling accordingly the
PATH
process environment variable with the path to the correct version to load.For instance, given this tree layout...
...and this code (sorry, C#, no VB.Net :-/ )...
...
function1
andfunction2
would be dynamically bound to either the 32 or 64 bits version of the native code, depending on the size of an IntPtr (more on this in this post from Scott Hanselman or this StackOverflow question).Note 1: This solution is especially useful when both versions of the dll bear the same name or if you're not willing to duplicate every extern references.
Note 2: This has already been successfully implemented in LibGit2Sharp.
不可以,您不能在 lib 语句中动态创建对 DLL 的引用。但是,您可能(免责声明:尚未尝试过)能够创建两个引用并在代码中调用适当的引用。
然后,您需要在平台上进行分支并根据平台调用适当的别名函数名称(Function132 或 Function164)。
No, you cannot dynamically create a reference to the DLL in a lib statement. However, you may (disclaimer: have not tried) be able to create two references and call the appropriate one in your code.
You will then need to branch on the platform and call the appropriate alias function name (Function132 or Function164) depending on the platform.