【发布时间】:2016-07-15 09:55:51
【问题描述】:
我在 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 = "_AcquireArchive@16",
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);
现在的问题是返回的代码发出 INVALID PASSWORD 信号,指示(根据文档)密码长度为 0。 password.pwd 和/或 password.length 的值是否可能在调用/编组过程中丢失?
遗憾的是,我无法访问 dll 的源代码。
编辑:CharSet 现在是 Ansi,TYPO “长度”,已删除 [UnmanagedType.BStr]
【问题讨论】:
-
C++ 代码肯定使用 Unicode 吗?因为
CharSet.Auto会在 WINdows XP 或更高版本上假设。 -
它不是 UnmanagedType.BStr,删除它。 struct 上的 CharSet.Auto 错误,必须使用 CharSet.Ansi。
-
我认为这里有一个错误:password.lenght
-
@MatthewWatson:OP 询问的是 C,而不是 C++!
-
@Olaf 同样的问题也适用——不管它的 C 还是 C++ 对结构的编组方式没有任何影响。
标签: c# c dll interop marshalling