将字符串作为PChar从CSharp传递到Delphi DLL

Sof*_*ija 4 c# delphi import dll

我试图将字符串从C#传递给Delphi内置的DLL。Delphi DLL需要PChar。

这是Delphi的出口

procedure DLL_Message(Location:PChar;AIntValue :integer);stdcall;
external 'DLLTest.dll';
Run Code Online (Sandbox Code Playgroud)

C#导入(我尝试的最后一个,有字符串,char * ref字符串...)

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

我以任何方式获得访问冲突访问值。

有什么解决方案可以在C#中将字符串值作为PChar传递?

Jen*_*off 5

尝试使用MarshalAs属性,该属性使您可以控制所使用的本机类型。

可以在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
        );
Run Code Online (Sandbox Code Playgroud)