說,我已經得到了原型的函數是一個DLL暴露:如何調用一個非託管函數,它具有char#[]作爲來自C#的OUT參數?
int CALLBACK worker (char* a_inBuf, int a_InLen,
char** a_pOutBuf, int* a_pOutLen,
char** a_pErrBuf, int* a_pErrLen)
我敢肯定,這是可笑容易調用DLL函數從我的C#代碼,但它不工作與此代碼:
[DllImport("mydll.dll")]
public static extern int worker(
[In, MarshalAs(UnmanagedType.LPArray)] byte[] inBuf,
int inputLen,
[Out, MarshalAs(UnmanagedType.LPArray)] byte[] outBuf,
out int outputLen,
[Out, MarshalAs(UnmanagedType.LPArray)] byte[] errBuf,
out int errorLen);
...
int outputXmlLength = 0;
int errorXmlLength = 0;
byte[] outputXml = null;
byte[] errorXml = null;
worker(input, input.Length, output, out outputLength, error, out errorLength);
我拿到的時候我要去找我的非託管庫中爲output
和error
內存(並因此取消引用傳遞指針)訪問衝突:
*a_ppBuffer = (char*) malloc(size*sizeof(char));
如何在我的C#代碼中爲此函數編寫
DLLIMPORT
語句?如何實際調用該函數,以便
a_pOutBuf
和a_pErrBuf
都可以訪問,並從內部worker
不null
(即使用一個真正的雙指針)?
您可以更改工作人員功能簽名嗎? –
@SimonMourier:沒有。它是一個在C++程序(建立的API)中使用的DLL,現在也應該從新開發的.net應用程序調用。因此API是固定的。 – eckes