如何从Intptr-参数中获取托管委托中的整数值,该代表从本机函数中称为void *



我有本地函数

void SetValue(char *FieldName, void *pValue);

我想将其更改为较早的设置回调/委托

具有签名

void SetValueDelegate(string fieldName, IntPtr value);

我这样称呼本机SetValue:

int IntValue = 0;
SetValue("MyField", &IntValue);

现在,我认为我可以将其施加在托管代表中:

void SetValueDelegate(string fieldName, IntPtr value)
{
    if (fieldName == "MyField")
    {
        int intValue = (int)value;
    }
}

这不起作用。如果铸造长时间,则值为204790096。

应该如何完成?

在您的托管代码中,valueint变量的地址。因此,您会读到这样的变量:

int intValue = Marshal.ReadInt32(value);

您的代码只是读取地址,而不是存储在该地址的值。

最新更新