我得到了這樣的C-DLL中定義的函數:爲什麼在將此結構從C#調用/封送到C-DLL時此字符串爲空?
int AcquireArchive (char* archiveName, Password* password)
的結構密碼在DLL中定義爲:
typedef struct password {
unsigned char* pPwd;
unsigned long length;
} Password;
什麼我做的是試圖總結這個功能C#中使用:
[DllImport("name.dll", CharSet = CharSet.Ansi, EntryPoint = "[email protected]",
CallingConvention = CallingConvention.StdCall)]
public static extern int AcquireArchive(
[MarshalAs(UnmanagedType.LPStr)] string archiveName,
ref Password password
);
和:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Password
{
public string pwd;
public ulong length;
}
我通過調用此函數:
Password password;
password.pwd = "password";
password.length = (ulong)password.pwd.Length;
int code = AcquireArchive("Archive", ref password);
現在的問題是,返回的代碼信號無效的密碼,表明(根據文檔)的0 密碼長度是否有可能密碼的值.pwd和/或password.length會在調用/編組過程中丟失?
很遺憾,我無法訪問dll的源代碼。
編輯:字符集現在是ANSI,TYPO 「長度」,除去[UnmanagedType.BStr]
C++代碼是否使用Unicode?因爲'CharSet.Auto'會在WIndows XP或更高版本上如此。 –
它不是一個UnmanagedType.BStr,刪除它。結構上的CharSet.Auto是錯誤的,你必須使用CharSet.Ansi。 –
我認爲這裏有一個錯誤:password.lenght <-----你應該改正「lenght」 – Mimouni