1

我一直在尝试调用非托管 DLL 的登录方法。

如果我使用 Declare 登录失败。

Private Declare Function Login Lib "dllCore" (ByVal lpName As String, ByVal lpPassword As String) As Int32

Login ("Steve", "123456") ' THIS FAILS TO LOGIN ALTHOUGH THE PARAMS ARE CORRECT

如果我使用 DllImport,它可以工作!

    <DllImport("dllCore.dll", 
                EntryPoint:="Login", 
                SetLastError:=True, 
                CharSet:=CharSet.Unicode, 
                ExactSpelling:=True, 
                CallingConvention:=CallingConvention.StdCall)> 
        Private Function Login(ByVal username As String, ByVal password As String) As Integer
        End Function

Login ("Steve", "123456") ' NOW WORKS 

有谁知道为什么我会出现这种行为?

4

1 回答 1

1

Declare 语句的默认字符集是 Ansi。您需要将字符集设置为 Unicode 以正确匹配 DllImport。

Private Declare Unicode Function Login Lib "dllCore" (ByVal lpName As String, ByVal lpPassword As String) As Int32

Declare 语句的 MSDN 文档

于 2011-12-19T17:06:38.433 回答