我正在嘗試使用P/Invoke Interop Assistant在C#中調用C++ Dll。大多數頭的轉換很好,但我有這個麻煩:我該如何聲明一個指向P/Invoke結構的指針?
#define FULLOCTAVE_BINS 12
#define THIRDOCTAVE_BINS 36
typedef struct tagTimeHistory
{
UINT m_nAction;
int m_nFlag;
int m_nRecordNum;
int m_nTimeStamp;
int m_nMiscStartIndex;
float m_pfTHFloatVals[256]; // Number of valid values given by m_nNumFloatVals in Settings.
float m_pfTH11OBAVals[4][FULLOCTAVE_BINS]; // 0-4 spectra given by m_nNumOBA11Vals in Settings
float m_pfTH13OBAVals[4][THIRDOCTAVE_BINS]; // 0-4 spectra given by m_nNumOBA13Vals in Settings
float m_fDuration;
} stTimeHistory_t;
typedef struct tagSlmBulkRecords
{
int nRecType;
union
{
stTimeHistory_t *m_ThRecs;
stInterval_t *m_Interval;
stExceedence_t *m_Exceedences;
stRunRecord_t *m_RunRecord;
stSpeechData_t *m_VoiceRecord;
stSpeechData_t *m_AudioRecord;
};
} stSlmBulkRecord_t;
這被轉換爲:
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Explicit)]
public struct Anonymous_d2bf9406_c664_4664_9196_800cc23f445a {
/// stTimeHistory_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr m_ThRecs;
/// stInterval_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr m_Interval;
/// stExceedence_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr m_Exceedences;
/// stRunRecord_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr m_RunRecord;
/// stSpeechData_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr m_VoiceRecord;
/// stSpeechData_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr m_AudioRecord;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct tagSlmBulkRecords {
/// int
public int nRecType;
/// Anonymous_d2bf9406_c664_4664_9196_800cc23f445a
public Anonymous_d2bf9406_c664_4664_9196_800cc23f445a Union1;
}
但我怎麼使用m_ThRecs時,它只是一個System.IntPtr?有什麼方法顯式聲明它是stTimeHistory_t的指針嗎?我移植到C#的C++代碼使用這樣的:
stSlmBulkRecord_t bulkRecord;
bulkRecord.m_ThRecs = new stTimeHistory_t[dataCounts.m_nNumTH];
但如果我嘗試在C#:
tagSlmBulkRecords bulkRecord;
bulkRecord.Union1.m_ThRecs = new tagTimeHistory[dataCounts.m_nNumTH];
我得到:
錯誤1無法隱式轉換鍵入'SlmTest.Program.tagTimeHistory []'到'SlmTest.Program.tagTimeHistory'「
如果我嘗試unsaf Ë定義:
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct tagTimeHistory
{
/// UINT->unsigned int
public uint m_nAction;
/// int
public int m_nFlag;
/// int
public int m_nRecordNum;
/// int
public int m_nTimeStamp;
/// int
public int m_nMiscStartIndex;
/// float[256]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 256, ArraySubType = System.Runtime.InteropServices.UnmanagedType.R4)]
public float[] m_pfTHFloatVals;
/// float[48]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 48, ArraySubType = System.Runtime.InteropServices.UnmanagedType.R4)]
public float[] m_pfTH11OBAVals;
/// float[144]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 144, ArraySubType = System.Runtime.InteropServices.UnmanagedType.R4)]
public float[] m_pfTH13OBAVals;
/// float
public float m_fDuration;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Explicit)]
public unsafe struct Anonymous_d2bf9406_c664_4664_9196_800cc23f445a
{
/// stTimeHistory_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public tagTimeHistory *m_ThRecs;
/// stInterval_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr *m_Interval;
/// stExceedence_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr m_Exceedences;
/// stRunRecord_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr m_RunRecord;
/// stSpeechData_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr m_VoiceRecord;
/// stSpeechData_t*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr m_AudioRecord;
}
我得到:
錯誤CS0208:不能取的地址,獲取的大小,或宣佈一個指向託管類型
將C#中的stSlmBulkRecord_t聲明爲'struct'或'class'嗎? – 2014-10-12 04:06:35
stSlmBulkRecord_t未在C#中聲明,只有tagSlmBulkRecords是一個如上所示的結構。 – parsley72 2014-10-12 05:23:55
這裏沒有明顯的不安全需要 – 2014-10-12 07:32:22