传递非托管 C++ 通过引用托管 C++ 的结构 引用结构时方法会导致访问冲突
我试图传递以下结构:
#pragma unmanaged
typedef struct
{
void* data;
unsigned nlen;
unsigned int type;
} PARAMETER;
到此类静态方法:
#pragma managed
private class __gc GlobalFunctions
{
static void WriteField(Object* object, PARAMTER& par, unsigned dec)
{
switch (par.type)
{
....
}
}
};
从该函数:
public class __gc WorkerClass
{
void SetValueAt(long index, Object* value)
{
PARAMETER aux;
aux.type = 3;
GlobalFunctions::WriteField(value, aux, 0);
}
};
在 64 位系统上,我收到访问冲突,指出无法读取地址“0x000c”。
现在,在 64 位系统上,如果 par 的引用是空指针,则 par.type 的取消引用将是 0x0c 的地址。 除了 par 在堆栈上 - 我没有将空指针传递到 WriteField 中,但我似乎正在取出一个空指针。
现在,在托管 C++ 中,当从一个托管类实例方法调用另一个静态方法时,我通过引用传递一个非托管结构是否容易受到某种编组问题的影响?
是否有任何网络文档解释托管代码如何处理非托管结构?
I'm trying to pass this structure:
#pragma unmanaged
typedef struct
{
void* data;
unsigned nlen;
unsigned int type;
} PARAMETER;
To this class static method:
#pragma managed
private class __gc GlobalFunctions
{
static void WriteField(Object* object, PARAMTER& par, unsigned dec)
{
switch (par.type)
{
....
}
}
};
From this function:
public class __gc WorkerClass
{
void SetValueAt(long index, Object* value)
{
PARAMETER aux;
aux.type = 3;
GlobalFunctions::WriteField(value, aux, 0);
}
};
On a 64-bit system, I get an access violation saying that the address '0x000c' cannot be read.
Now, on a 64-bit system, the dereference of par.type would be address of 0x0c if the reference of par was a null pointer. Except par is on the stack - I'm not passing a null pointer into WriteField, but I seem to be getting one out.
Now, in Managed C++, when calling from one managed class instance method to another static method, is the fact that I'm passing an unmanaged structure by reference vulnerable to some sort of marshalling issue?
Is there any web documentation explaining how unmanaged structures are treated by managed code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将托管和非托管代码显式编译为托管/非托管。 如果托管代码和非托管代码混合在同一个文件中,则需要使用托管编译指示和非托管编译指示。 使用包含文件时还要记住这一点。
Have the managed and the un-managed code be explicitly compiled as managed/un-managed. If the managed and un-managed code is mixed in the same file the pragma managed and pragma unmanaged needs to be used. Also keep that in mind when you use include files.