Passing a string as PChar from CSharp to Delphi DLL

I am trying to pass a string from C # to a Delphi built DLL. Delphi DLL expects PChar.

Here is the Delphi export

procedure DLL_Message(Location:PChar;AIntValue :integer);stdcall; external 'DLLTest.dll'; 

C # import (the last one I tried was a string, char * ref string ...)

 [DllImport( "DLLTest.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DLL_Message" )] public static extern void DLL_Message(IntPtr Location, int AIntValue); 

I am accessing an access violation value in any way.

Is there any solution to pass string value as PChar in C #?

+4
source share
1 answer

Try using the MarshalAs attribute, which allows you to control the type native used.

A list of types can be found on MSDN:

http://msdn.microsoft.com/de-de/library/system.runtime.interopservices.unmanagedtype.aspx

 DllImport( "DLLTest.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DLL_Message" )] public static extern void DLL_Message( [MarshalAs(UnmanagedType.LPStr)] string Location, int AIntValue ); 
+4
source

All Articles