2010-06-25 48 views
1

我一直在試圖調用一個非託管DLL的登錄方法。聲明和<DllImport>在VB.NET有不同的結果

如果我使用聲明登錄失敗。

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

Declare語句的缺省字符集是Ansi。您需要將charset設置爲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