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:
In C# you would declare this like so:
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: