My client just sent me a delphi dll to be consumed for my asp.net app, and below is the dll's signature:
function GerarChave(pChave: ShortString; pData: ShortString; pAcao: ShortString): PAnsiChar; stdcall;
How should I call it? I've tried everything like
[DllImport("CEIINT.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "GerarChave")]
public static extern string GerarChave([MarshalAs(UnmanagedType.BStr)]string pChave, [MarshalAs(UnmanagedType.BStr)]string pData, [MarshalAs(UnmanagedType.BStr)]string pAcao);
string chave = "ABC123";
string data = "19/09/2019";
string acao = "0";
GerarChave(chave, data, acao);
but I always get a System.AccessViolationException error which says:
System.AccessViolationException... Attempted to read or write protected memory. This is often an indication that other memory is corrupt
Could anybody help me please? Thanks in advance!
MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)
whereas in the DllImport-Attribute the CharSet parameter should be set toCharSet.Ansi
or a byte array withMarshalAs(UnmanagedType.ByValArray, SizeConst=256)
. Of course, the first character must be manually read/written as the length. – ckuri