我正在尝试将字符串从 C# 传递到 Delphi 构建的 DLL。
Delphi DLL 需要 PChar。
这是Delphi导出
procedure DLL_Message(Location:PChar;AIntValue :integer);stdcall;
external 'DLLTest.dll';
C# import(我尝试的最后一个,有 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);
我以任何方式访问值时都会遇到访问冲突。
在 C# 中是否有任何解决方案可以将字符串值作为 PChar 传递?
尝试使用 MarshalAs 属性,它允许您控制所使用的本机类型。
可以在 MSDN 中找到类型列表:
http://msdn.microsoft.com/de-de/library/system.runtime.interopservices.unmanagementtype.aspx 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
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)