如何从 C++ dll 中的 C# 调用以下方法?如何在 C# 中重新创建以下结构?
原来的
方法:
LONG MyMethod (P_HOLO_INFO pInfo, LPVOID pBuffer, LPUSHORT pTracksWritten);
结构:此方法使用以下结构:
typedef struct _HOLO_INFO
{
LONG lHoloType;
LONG lStatus;
HANDLE lThreadHandle;
LONG lErrorCode;
LONG lDriveIndex;
LONG lHeight;
SHORT iFormat;
INT iStartingTrack;
LONG lWrite;
LONG lSkip;
BOOL bSkipZero;
BOOL bInvert;
LONG lMaxConsecutiveErrors;
LONG lMaxTotalErrors;
LONG lMostConsecutiveErrors;
LONG lTotalErrors;
LPBYTE pBuffer;
LPUSHORT pTracksWritten;
LONG bUpsideDown;
} HOLO_INFO, *P_HOLO_INFO;
我像这样在 C# 中工作
方法:
[DllImport("My.dll", EntryPoint = "_MyMethod@12")]
public unsafe static extern long MyMethod(ref HOLO_INFO pInfo, Byte[] pBuffer,ref ushort pTracksWritten);
结构:
此方法使用以下结构:
unsafe public struct HOLO_INFO
{
public long lHoloType;
public long lStatus;
public long lThreadHandle;
public ulong lErrorCode;
public long lDriveIndex;
public long lHeight;
public short iFormat;
public int iStartingTrack;
public long lWrite;
public long lSkip;
public bool bSkipZero;
public bool bInvert;
public long lMaxConsecutiveErrors;
public long lMaxTotalErrors;
public long lMostConsecutiveErrors;
public long lTotalErrors;
public Byte* pBuffer;
public long* pTracksWritten;
public long bUpsideDown;
};
我调用了这样的方法:
do
{
result = MyMethod(ref pInfo,ptrBuf,pTracksWritten);
} while (result ==1 );
因为,它返回 1,如果它是 Active 0,如果它成功完成 3,如果它因为错误而停止。如果方法处于运行状态(Active-1)。它修改pInfo和pTracksWritten以更新状态信息。