gpt4 book ai didi

vb.net - 在 VB.NET 中动态声明第三方 DLL

转载 作者:行者123 更新时间:2023-12-02 00:15:07 26 4
gpt4 key购买 nike

我在不同环境中导入 DLL 时遇到问题。我必须检查 Windows 平台并导入将放置在 C:\Program Files\(对于 32 位)或 C: Program Files (x86)\(适用于 64 位)。

之前的代码是这样写的:

Declare Function RDRCConnect Lib "c:\program files\TP-DLL\RDRCAP32.DLL" (ByVal lpszServerName As String, ByVal lNetConnType As Integer, ByVal lpszParam1 As String, ByVal lpszParam2 As String, ByVal lpszParam3 As String, ByRef lNetConn As Integer, ByRef lNetErr As Integer) As Integer  
Declare Function RDRCDisconnect Lib "c:\program files\TP-DLL\RDRCAP32.DLL" (ByVal lNetConn As Integer, ByRef lNetErr As Integer) As Integer

...我改为使用属性:

Private Const CheminDLL As String = "C:\Program Files\TP-DLL\RDRCAP32.DLL"

<System.Runtime.InteropServices.DllImport(CheminDLL)>
Private Shared Function RDRCConnect(ByVal lpszServerName As String, ByVal lNetConnType As Integer, ByVal lpszParam1 As String, ByVal lpszParam2 As String, ByVal lpszParam3 As String, ByRef lNetConn As Integer, ByRef lNetErr As Integer) As Integer
End Function

在这种情况下,如果 DLLImport 需要一个常量作为参数,我该如何动态更改 DLL 路径?

最佳答案

除了常量之外,没有其他方法可以将任何东西传递给属性,因为根据定义,属性是在编译时计算的,而不是在运行时。可能有更好的选择,但我可以给你的一个选择是为每个版本创建单独的导入:

<DllImport("C:\Program Files\TP-DLL\RDRCAP32.DLL", EntryPoint := "RDRCConnect")>
Private Shared Function RDRCConnect32(ByVal lpszServerName As String, ByVal lNetConnType As Integer, ByVal lpszParam1 As String, ByVal lpszParam2 As String, ByVal lpszParam3 As String, ByRef lNetConn As Integer, ByRef lNetErr As Integer) As Integer
End Function

<DllImport("C:\Program Files (x86)\TP-DLL\RDRCAP32.DLL", EntryPoint := "RDRCConnect")>
Private Shared Function RDRCConnect64(ByVal lpszServerName As String, ByVal lNetConnType As Integer, ByVal lpszParam1 As String, ByVal lpszParam2 As String, ByVal lpszParam3 As String, ByRef lNetConn As Integer, ByRef lNetErr As Integer) As Integer
End Function

然后您需要在每次调用该方法时选择适本地调用哪一个。

关于vb.net - 在 VB.NET 中动态声明第三方 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13593206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com