2010-06-25 23 views
1

J'ai essayé d'appeler une méthode de connexion d'une DLL non managée.Declare et <DllImport> dans VB.NET ont des résultats différents

Si j'utilise Declare, la connexion échoue.

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 

Si j'utilise DllImport, ça marche !!

<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 

Quelqu'un at-il des idées pourquoi je reçois ce comportement ??

Répondre

1

Le jeu de caractères par défaut pour une instruction Declare est Ansi. Vous devez définir le jeu de caractères sur Unicode pour correspondre correctement à DllImport.

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

MSDN documentation for the Declare statement