1

this is the Delphi function header:

function CryptStr(str, Key : pchar; DecryptStr : boolean) : pchar; stdcall;


how to call this function from C#. it's actually inside a DLL named Crypto.dll

please guide
thanks

aleroot
  • 71,077
  • 30
  • 176
  • 213
zeeshan
  • 43
  • 1
  • 4
  • This looks similar to http://stackoverflow.com/questions/19631722/use-delphi-dll-and-some-problems But it's slightly different. Are these questions related? If so, it would be better if there was just one question. I'm concerned that there will be a glut of these. – David Heffernan Oct 28 '13 at 12:22
  • Seems the same to me, just like the original poster signed up to another anonymous account to ask twice. – Warren P Oct 28 '13 at 20:58
  • @Warren Actually more interesting. Read the comments to my answer. – David Heffernan Oct 29 '13 at 08:06
  • Step 1 bid on freelance job, Step 2 ask people on SO to figure out problem, Step 3, Profit. Well, five bucks, anyways. – Warren P Oct 31 '13 at 02:52

1 Answers1

5

Assuming that PChar is PAnsiChar:

[DllImport("Crypto.dll")]
static extern IntPtr CryptStr(string str, string Key, bool DecryptStr);

And then call it like this:

IntPtr retvalPtr = CryptStr(str, Key, DecryptStr);
string retval = Marshal.PtrToStringAnsi(retvalPtr);

And I expect you'll also need to call the function that the DLL exports that frees the memory behind the pointer that CryptStr returned. You did export such a function I trust?

If PChar is PWideChar then it would be:

[DllImport("Crypto.dll", CharSet = CharSet.Unicode)]
static extern IntPtr CryptStr(string str, string Key, bool DecryptStr);

And then call it like this:

IntPtr retvalPtr = CryptStr(str, Key, DecryptStr);
string retval = Marshal.PtrToStringUni(retvalPtr);

Finally, if you wanted to be really cute, you could arrange for the Delphi code to allocate the return value string from the COM heap using CoTaskMemAlloc. If you did that, then you could avoid the manual marshalling, and let the p/invoke marshaller perform the deallocation. That would look like this.

[DllImport("Crypto.dll", CharSet = CharSet.Unicode)]
static extern string CryptStr(string str, string Key, bool DecryptStr);

And then call it like this:

string retval = CryptStr(str, Key, DecryptStr);

It should be obvious now how to code the ANSI variant and so I will omit it.

When you declare a p/invoke with a string return value, the marshaller assumes that the native code returns a pointer to a null-terminated array of characters. And it also assumes that the memory was allocated off the COM heap, and makes a call to CoTaskMemFree to deallocate the memory. So, doing it this way avoids the need for a separate deallocator function to be exported from the DLL.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I tried all of the possibilities, it never executes the function successfully and gives [this exception all the time](https://www.dropbox.com/s/w6dchxmrcarf31h/Error.png) – zeeshan Oct 28 '13 at 13:11
  • actually I'm not developer of this DLL and it's provided to me, alongwith the function signature. that's all I have right now. – zeeshan Oct 28 '13 at 13:12
  • The exception probably indicates that the function exported by the DLL does not have the signature that you state in the question. If the signature really is as you say, then the code above will not give the error your describe. Clearly this is the same question as the one asked earlier here: http://stackoverflow.com/questions/19631722/use-delphi-dll-and-some-problems But in that question the parameters were `AnsiString`. So it would seem that there is confusion at your end. Asking two questions instead of one is a bad sign. – David Heffernan Oct 28 '13 at 13:15
  • I'm sorry but that's not my account. actually it's a [freelancer project](https://www.freelancer.com/projects/C-Sharp-Programming-NET/NET-Web-Service-Scokets.html) and multiple people trying to win the bid. – zeeshan Oct 28 '13 at 13:23
  • 1
    Hmm, it looks to me like the person specifying the project does not know what they are doing. If I were you, I'd steer well clear of this one! Anyway, I'm sure that my answer is accurate give then information in the question. But I would also bet that the function that you have been supplied does not have the signature that you quote in this question. – David Heffernan Oct 28 '13 at 13:25
  • thanks, solved my problem, I was stuck for days without solution – Lucas Riechelmann Ramos Sep 20 '18 at 13:54