我很费劲地想弄清楚为什么我无法从我的C#应用程序中调用旧的C++ .dll中的外部方法。
这是函数头:
int __export FAR PASCAL SimplePGPEncryptFile(
HWND hWnd1,
LPSTR InputFileName,
LPSTR OutputFileName,
BOOL SignIt,
BOOL Wipe,
BOOL Armor,
BOOL TextMode,
BOOL IDEAOnly,
BOOL UseUntrustedKeys,
LPSTR RecipientList,
LPSTR SignerKeyID,
int SignerBufferLen,
LPSTR SignerPassphrase,
int SignerPwdBufferLen,
LPSTR IDEAPassphrase,
int IDEAPwdBufferLen,
LPSTR PublicKeyRingName,
LPSTR PrivateKeyRingName);
这是我的C#声明:
[DllImport("smplpgp_32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SimplePGPEncryptFile(
IntPtr hWnd1,
[MarshalAs(UnmanagedType.LPStr)] string InputFileName,
[MarshalAs(UnmanagedType.LPStr)] string OutputFileName,
bool SignIt,
bool Wipe,
bool Armor,
bool TextMode,
bool IDEAOnly,
bool UseUntrustedKeys,
[MarshalAs(UnmanagedType.LPStr)] string RecipientList,
[MarshalAs(UnmanagedType.LPStr)] string SignerKeyID,
int SignerBufferLen,
[MarshalAs(UnmanagedType.LPStr)] string SignerPassphrase,
int SignerPwdBufferLen,
[MarshalAs(UnmanagedType.LPStr)] string IDEAPassphrase,
int IDEAPwdBufferLen,
[MarshalAs(UnmanagedType.LPStr)] string PublicKeyRingName,
[MarshalAs(UnmanagedType.LPStr)] string PrivateKeyRingName);
当我调用这个方法时,我会得到以下两种错误之一(在头文件中声明):
#define SIMPLEPGPENCRYPTFILE_RECIPIENTLISTDOESNOTENDWITHNEWLINE 408
#define SIMPLEPGPENCRYPTFILE_RECIPIENTLISTDOESNOTSTARTWITHGOODCODECHAR 409
这也被定义为头文件中的常量:
#define INCLUDE_ONLYUSERIDS 1
这是已知可行的C++代码,调用该函数:
char recipients[512];
recipients[0] = INCLUDE_ONLYUSERIDS;
strcat(strcpy(&recipients[1], rID), "\n"); \\ rID is equal to "CA"
return 0 == SimplePGPEncryptFile(INI.m_hWnd,
(char*)plain, (char*)cipher,
0,
0,
1,
0,
0,
1, // UseUntrustedKeys
recipients,
0, 0,
0, 0,
0, 0,
PGPKM.pub, 0); //PGPKM.pub is declared earlier
将此内容作为“RecipientList”参数传递时,会出现“409”错误。
string recipientList = "1CA\n\0";
将此内容作为'RecipientList'参数传递会导致出现“408”错误:
char[] recipients = new char[512];
recipients[0] = '1';
recipients[1] = 'C';
recipients[2] = 'A';
recipients[3] = '\n'; // also tried '\r', then '\n'
recipients[4] = Char.MinValue;
string paramValue = recipients.ToString();
有没有人注意到我犯了什么明显的错误?我感觉我已经拥有解决这个问题所需的一切,但是没有任何预期效果。
顺便说一句:我成功地调用了同一个.dll文件中的不同函数。此外,我尝试使用StringBuilder构建RecipientList参数。
感谢任何建议!