How to interpret void* in C#?

I am writing a porgram to call function in a dll created by C++.
this is not a com dll,so I use pinvoke .
In this function,there is a parameter was the type void*
how can I translate it in C#


Answer this question

How to interpret void* in C#?

  • kzu

    It's an IntPtr, which is to say, it's a memory address that could point to anything.



  • Rik Dodsworth

    can I do not use unsafe code to interop this function contain void*

  • hengemd

    If you know that the parameter type should be a pointer to an int, just change the parameter type to ref int instead of IntPtr.



  • CE

    Inside an unsafe block you can get an int * like this

    int n = 50;

    int * p = &n;

    You can pass any pointer in place of a void *, so you could pass &n as your void *.



  • dtwilliamson

    I don't know. I played with IntPtr and can't see any way to initialise it with the address of a variable without using unsafe code.



  • Tracy - MSFT

    so if I have a Int32 value 50
    how could I use IntPtr to point memory of the Int32 value

  • How to interpret void* in C#?