使用Delphi Dll和一些问题 [英] Use Delphi Dll and some problems
问题描述
函数CryptStr(str,Key:AnsiString; DecryptStr:boolean):AnsiString; stdcall;
我将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行。
我试图使用元帅,但是上面的错误仍然存在。
你不能指望从编程环境调用该函数除了德尔福。这是因为它使用Delphi本机字符串,对于interop无效。即使您从Delphi调用,您也需要使用与用于编译DLL相同版本的Delphi,以及 ShareMem
单元,以便可以共享内存管理器。这个功能对于两个Delphi模块之间的互操作来说并不是很好。
您需要更改DLL函数的签名。例如,您可以使用:
procedure CryptStr(
str:PAnsiChar;
Key:PAnsiChar;
DecryptStr:boolean;
输出:PAnsiChar;
); STDCALL;
在C#中,您可以这样声明:
[DllImport(Crypt2.dll)]
static extern void CryptStr(
string str,
string Key,
bool DecryptStr,
StringBuilder输出
);
此更改要求调用者分配传递给该函数的缓冲区。如果你想找到这样做的例子,请搜索调用Win32 API GetWindowText
的示例。
如果你是使用UTF-16文本而不是8位ANSI,您可以使用在共享COM堆上分配的COM BSTR
,但我怀疑该选项不可用。 / p>
对于您的程序没有显示任何错误,我将您引荐到这些帖子:
- 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
- C#中的静默失败,看似未处理的异常,不会使程序崩溃
I want to use a dll that made by Delphi. It has this function : function CryptStr(str, Key : AnsiString; DecryptStr : boolean) : AnsiString; stdcall;
I copied the Dll in /bin/debug and in application root. my code is :
[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);
}
I have some problem : 1. even I delete Dll from those path application doesn't throw not found exception 2. when application run in g=CryptStr("999", "999999", true); it finishes execution and show the form without running Messagebox line. I tried to use Marshal but above errors remain.
You cannot expect to call that function from a programming environment other than Delphi. That's because it uses Delphi native strings which are not valid for interop. Even if you call from Delphi you need to use the same version of Delphi as was used to compile the DLL, and the ShareMem
unit so that the memory manager can be shared. That function is not even well designed for interop between two Delphi modules.
You need to change the DLL function's signature. For example you could use:
procedure CryptStr(
str: PAnsiChar;
Key: PAnsiChar;
DecryptStr: boolean;
output: PAnsiChar;
); stdcall;
In C# you would declare this like so:
[DllImport("Crypt2.dll")]
static extern void CryptStr(
string str,
string Key,
bool DecryptStr,
StringBuilder output
);
This change requires the caller to allocate the buffer that is passed to the function. If you want to find examples of doing that, search for examples calling the Win32 API GetWindowText
.
If you were using UTF-16 text instead of 8 bit ANSI, you could use COM BSTR
which is allocated on the shared COM heap, but I suspect that option is not available to you.
As for your program not showing any errors, I refer you to these posts:
- 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
- Silent failures in C#, seemingly unhandled exceptions that does not crash the program
这篇关于使用Delphi Dll和一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!