2010-11-11 40 views
4

我试图通过从C#中的字符串德尔福创建的DLL。 德尔福DLL期待PChar。传递字符串作为PChar类型从CSHARP德尔福DLL

这里是德尔福出口

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

C#进口(最后一个我想,有串字符*参考串...)

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

我收到访问冲突在任何访问值办法。

是否有任何解决方案,通过在C#中的字符串值作为PChar类型?

回答

4

尝试用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 
     ); 
+0

的MarshalAs。这是默认编组。对于DllImport属性的参数同样如此。只需要文件名。 – 2012-02-15 00:10:48

+0

你确定吗?不是默认的UnmanagedType.LPWStr(UTF-16)? – 2012-02-15 10:02:10

+1

显然,这是BSTR:http://msdn.microsoft.com/en-us/library/s9ts558h.aspx – 2012-02-15 10:08:02