![](/img/trans.png)
[英]'Cannot marshal 'return value': Invalid managed/unmanaged type combination.' while marshalling delegate with string array
[英]What does “Invalid managed/unmanaged type combination.” mean?
我有以下結構:
[StructLayout(LayoutKind.Auto,Pack=0)]
private unsafe struct BIRDSYSTEMCONFIG
{
public byte bySystemStatus;
public byte byError;
public byte byNumDevices;
public byte byNumServers;
public byte byXmtrNum;
public ushort wXtalSpeed;
public double dMeasurementRate;
public byte byChassisNum;
public byte byNumChassisDevices;
public byte byFirstDeviceNum;
public ushort wSoftwareRev;
public fixed byte byFlockStatus[127];
}
基於C ++結構:
typedef struct tagBIRDSYSTEMCONFIG
{
BYTE bySystemStatus; // current system status (see bird system status bits, above)
BYTE byError; // error code flagged by server or master bird
BYTE byNumDevices; // number of devices in system
BYTE byNumServers; // number of servers in system
BYTE byXmtrNum; // transmitter number (see transmitter number bits, above)
WORD wXtalSpeed; // crystal speed in MHz
double dMeasurementRate; // measurement rate in frames per second
BYTE byChassisNum; // chassis number
BYTE byNumChassisDevices; // number of devices within this chassis
BYTE byFirstDeviceNum; // number of first device in this chassis
WORD wSoftwareRev; // software revision of server application or master bird
BYTE byFlockStatus[BIRD_MAX_DEVICE_NUM + 1]; // status of all devices in flock, indexed by bird number (see note in BIRDFRAME definition) - see bird flock status bits, above
}
BIRDSYSTEMCONFIG;
並具有以下功能:
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern bool birdGetSystemConfig(
int nGroupID,
ref BIRDSYSTEMCONFIG psyscfg,
bool bGetDriverCopy
);
基於C ++函數:
BOOL DLLEXPORT birdGetSystemConfig(int nGroupID, BIRDSYSTEMCONFIG *psyscfg, BOOL bGetDriverCopy = FALSE);
我稱之為:
BIRDSYSTEMCONFIG sysconf = new BIRDSYSTEMCONFIG();
birdGetSystemConfig(1, ref sysconf, true);
但是告訴我一個錯誤:
無法編組'參數#2':無效的托管/非托管類型組合。
這意味着什么? 它為什么會發生? 我怎么能克服它? 歡迎所有建議!
事實證明我需要做的就是改變:
[StructLayout(LayoutKind.Auto,Pack=0)]
至
[StructLayout(LayoutKind.Sequential,Pack=0)]
既然問題不僅僅是如何解決它,我還會暫時擱置它。 很高興找到關於這個錯誤的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.