Jaco Van Deventer Jaco Van Deventer - 8 months ago 120
C# Question

Delphi dll importing to C# - System.AccessViolationException DllImport

Good day. I am struggling to get the function call to my Delphi to return data. I know the dll is working because if I use a show msg inside the dll it returns the right information.

I am thinking its to do with memory sizes that aren't the same. I also read a lot about prebuffering the memory, unfortunately, I don't have the know-who to implement it into my project

Exception details:
System.AccessViolationException
{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

Delphi function:

function doStuff({var} c:string): pchar; stdcall;


C#

[DllImport("Utili.dll",
CallingConvention = CallingConvention.StdCall,
CharSet = CharSet.Unicode)]
public static extern string doStuff(string a);

static void Main(string[] args)
{
try
{
string q = "1234567890123456";
string a = doStuff(q);
}
catch (Exception)
{

throw;
}

}

Answer Source

Got the supplier to fix their code.

If you have the Delphi code look into CoTaskMemAlloc

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download