在VB.NET中声明和<dllimport>有不同的结果</dllimport>

时间:2010-06-25 15:17:43

标签: dllimport declare

我一直在尝试调用非托管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 

有没有人知道为什么我会这样做?

1 个答案:

答案 0 :(得分:1)

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

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

MSDN documentation for the Declare statement