方法的类型签名不与PInvoke兼容

时间:2013-05-17 21:58:33

标签: vb.net delphi dll

我是vb.net的新手并试图调用返回记录的Delphi Dll。如果我在结构中放置三个整数,当我尝试类似下面的代码时,我会得到“方法的类型签名不兼容PInvoke”。任何想法为什么我不能添加字节数组,或者即使我添加布尔它也失败。

Public Structure SysInfo
    Public iPrtRes As Integer
    Public iMaxRips As Integer
    Public iUnits As Integer
    Public str As Byte()
End Structure

<DllImport("C:\project2.DLL", CallingConvention:=CallingConvention.Cdecl)>
Public Function getsysinfoF() As SysInfo
End Function

Dim theSysInfoRec As SysInfo
ReDim theSysInfoRec.str(255)

theSysInfoRec = getsysinfoF()

的Delphi

type
  SysInfo = record
    iPrtRes: Integer;
    iMaxRips: Integer;
    iUnits: Integer;
    str: array[0..255] of Byte;
  end;

function getsysinfoF() : SysInfo; cDecl
begin
  result.iPrtRes := 400;
  result.iMaxRips := 300;
  result.iUnits := 200;
  result.str[0] := $ff;
end;

发现了解决方案 Passing record as a function result from Delphi DLL to C++

1 个答案:

答案 0 :(得分:3)

.NET托管阵列与其他语言中的非托管阵列不同。您需要告诉PInvoke如何编组结构的数组字段,这取决于DLL如何首先分配和管理该数组。它是C风格的阵列吗?一个Delphi风格的动态数组? ActiveX / COM SafeArray?这种信息需要使用MarshalAs属性包含在.NET端的结构的PInvoke声明中(显然,.NET不支持Delphi风格的动态数组)。

有关详细信息,请参阅MSND:

Default Marshaling for Arrays

MarshalAsAttribute Class

更新:例如:

的Delphi:

type
  SysInfo = record
    iPrtRes: Integer;
    iMaxRips: Integer;
    iUnits: Integer;
    str: array[0..255] of Byte;
  end;

.NET:

Public Structure <StructLayout(LayoutKind.Sequential)> SysInfo
    Public iPrtRes As Integer
    Public iMaxRips As Integer
    Public iUnits As Integer
    <MarshalAs(UnmanagedType.ByValArray, SizeConst := 256)>
    Public str() As Byte
End Structure
相关问题