現在、C++ DLL を C# アプリケーションに統合しようとしていますが、メソッドの 1 つを呼び出す正しい方法を特定できません。ドキュメントの 2 つの異なる場所で、メソッド定義が等しくありません。
ImageAndScanError GetMicrInfo(unsigned char *ptrCodeline,int* iLength)
ImageAndScanError WINAPI GetMicrInfo(char* cMicrInfo,int* iInfoLength);
/*
ImageAndScanError GetMicrInfo(unsigned char *ptrCodeline,int* iLength)
Parameters:
ptrCodeline: a pointer to the output buffer that will receive the code line read by the MICR algorithm. The ptrCodeline should allocate room for 96 characters.
iLength: the number of characters contained in the code line
Function: Read MICR line on the check. This function must be called after StartScan .
Returns: ErrorNone is returned upon success. Otherwise, an enum ImageAndScanError value that indicates the reason for failure is returned.
*/
これは、dllメソッドを含める方法です
[DllImport("ScanDll.dll", CallingConvention = CallingConvention.Winapi)]
そして、これは私がこれまでに作ったすべての組み合わせです
public static extern ImageAndScanError GetMicrInfo(out IntPtr cMicrInfo, out int iInfoLength);
public static extern ImageAndScanError GetMicrInfo(out byte[] cMicrInfo, out int iInfoLength);
public static extern ImageAndScanError GetMicrInfo(out string cMicrInfo, out int iInfoLength);
public static extern ImageAndScanError GetMicrInfo(out StringBuilder cMicrInfo, out int iInfoLength);
IntPtr cMicrInfoTMP;
byte[] cMicrInfoTMP= new byte[96];
string cMicrInfoTMP;
StringBuilder cMicrInfoTMP;
GetMicrInfo(out cMicrInfoTMP, out iInfoLengthTMP);
IntPtr を使用すると、VS2010 でデバッグによって得られる値は、サイズが 4 の 859256727 です。
string myString = Marshal.PtrToStringAnsi(cMicrInfoTMP);
私はいつも空の文字列を取得します。
他のもの(byte []、string、StringBuilder)を試すと、
The runtime has encountered a fatal error. The address of the error was at
0x53e6716a, on thread 0x1084. The error code is 0xc0000005. This error may
be a bug in the CLR or in the unsafe or non-verifiable portions of user
code. Common sources of this bug include user marshaling errors for COM-interop
or PInvoke, which may corrupt the stack.
ここで何が欠けていますか?ありがとう