我想使用Delphi制作的dll。它有这个功能:
函数 CryptStr(str, Key : AnsiString; DecryptStr : boolean) : AnsiString;标准调用;
我将 Dll 复制到 /bin/debug 和应用程序根目录中。我的代码是:
[DllImport("Crypt2.dll", EntryPoint = "CryptStr", CallingConvention = CallingConvention.StdCall)]
static extern string CryptStr( string str, string Key, bool DecryptStr);
public string g = "";
private void Form1_Load(object sender, EventArgs e)
{
g=CryptStr("999", "999999", true);
MessageBox.Show(g);
}
我有一些问题:
1.即使我从这些路径应用程序中删除Dll也不会抛出未找到的异常
2. 当应用程序运行在 g=CryptStr("999", "999999", true); 中时它完成执行并显示表单而不运行 Messagebox 行。
我尝试使用 Marshal 但仍然存在上述错误。
您不能期望从 Delphi 以外的编程环境中调用该函数。这是因为它使用对互操作无效的 Delphi 本机字符串。即使您从 Delphi 调用,您也需要使用与编译 DLL 相同的 Delphi 版本,并且ShareMem
单元以便内存管理器可以共享。该函数甚至没有很好地设计用于两个 Delphi 模块之间的互操作。
您需要更改 DLL 函数的签名。例如您可以使用:
procedure CryptStr(
str: PAnsiChar;
Key: PAnsiChar;
DecryptStr: boolean;
output: PAnsiChar;
); stdcall;
在 C# 中,您可以这样声明:
[DllImport("Crypt2.dll")]
static extern void CryptStr(
string str,
string Key,
bool DecryptStr,
StringBuilder output
);
此更改要求调用者分配传递给函数的缓冲区。如果您想查找执行此操作的示例,请搜索调用 Win32 API 的示例GetWindowText
.
如果您使用 UTF-16 文本而不是 8 位 ANSI,则可以使用 COMBSTR
它是在共享 COM 堆上分配的,但我怀疑您不可以使用该选项。
至于你的程序没有显示任何错误,我建议你参考这些帖子:
- http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/ http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/
- http://blog.adamjcooper.com/2011/05/why-is-my-exception-being-swallowed-in.html http://blog.adamjcooper.com/2011/05/why-is-my-exception-being-swallowed-in.html
- C# 中的静默故障,看似未处理的异常,不会导致程序崩溃 https://stackoverflow.com/questions/1583351/silent-failures-in-c-seemingly-unhandled-exceptions-that-does-not-crash-the-pr
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)