嗨,我有一个DLL,我需要调用一个函数.签名是:
const char* callMethod(const char* key, const char* inParams);
Run Code Online (Sandbox Code Playgroud)
如果我使用ruby一切正常:
attach_function :callMethod, [:string, :string], :string
Run Code Online (Sandbox Code Playgroud)
如果我使用C++或C#,我会得到堆栈溢出!
C#:
[DllImport("DeviceHub.dll", CallingConvention = CallingConvention.Cdecl)]
private unsafe static extern IntPtr callMethod(
[MarshalAs(UnmanagedType.LPArray)] byte[] key,
[MarshalAs(UnmanagedType.LPArray)] byte[] inParams
);
System.Text.UTF8Encoding encoding = new UTF8Encoding();
IntPtr p = callMethod(encoding.GetBytes(key), encoding.GetBytes(args)); // <- stack overflow here
Run Code Online (Sandbox Code Playgroud)
C++:
extern "C"
{
typedef DllImport const char* ( *pICFUNC) (const char*, const char*);
}
HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("C:\\JOAO\\Temp\\testedll\\Debug\\DeviceHub.dll"));
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"callMethod");* pICFUNC callMethod;
callMethod = (pICFUNC) …Run Code Online (Sandbox Code Playgroud) 我需要遍历任意排名的数组.这适用于阅读和写作,所以GetEnumerator不起作用.
Array.SetValue(object, int)不适用于多维数组.
Array.SetValue(object, params int[])需要过多的算术来迭代多维空间.它还需要动态调用来绕过params签名的一部分.
我很想固定数组并用指针迭代它,但我找不到任何文档说多维数组保证是连续的.如果他们在维度的末尾有填充,那么这将无效.我也更愿意避免不安全的代码.
是否有一种简单的方法来仅使用单个索引顺序寻址多维数组?
我有一个在c++ Win32 DLL 中定义的结构,如下所示:
typedef struct matrix
{
double** data;
int m;
int n;
} Matrix;
Run Code Online (Sandbox Code Playgroud)
还有一个功能:
Matrix getMatrix(void);
Matrix getMatrix()
{
Matrix mat;
mat.m = 2;
mat.n = 2;
mat.data = (double**) malloc (sizeof(double*) * 4);
mat.data[0] = (double* ) malloc (sizeof(double) * 2);
mat.data[1] = (double* ) malloc (sizeof(double) * 2);
mat.data [0][0]=1;
mat.data [0][1]=2;
mat.data [1][0]=3;
mat.data [1][1]=4;
return mat;
}
Run Code Online (Sandbox Code Playgroud)
如果我P/Invoke从 C# 应用程序使用,如何捕获此函数的返回值