Reputation: 8336
I have native function
void SetValue(char *FieldName, void *pValue);
and I want to change it to call earlier set callback/delegate
that has signature
void SetValueDelegate(string fieldName, IntPtr value);
I call native SetValue like this:
int IntValue = 0;
SetValue("MyField", &IntValue);
Now i thought that I can just cast it in managed delegate:
void SetValueDelegate(string fieldName, IntPtr value)
{
if (fieldName == "MyField")
{
int intValue = (int)value;
}
}
this is not working. If cast to long it's value is 204790096.
How this should be done?
Upvotes: 4
Views: 11575
Reputation: 613013
In your managed code, value
is the address of the int
variable. Therefore, you read that variable like this:
int intValue = Marshal.ReadInt32(value);
Your code is simply reading the address, rather than the value stored at that address.
Upvotes: 12