Search code examples
c#cpinvoke

Using a method from C that's pointer dependent in C#


I'm making a wrapper for a C library. There is a method that changes 2 ints by the user giving 2 int pointers to the method. So if I have void changenums(int* a, int* b) what is a safe way to access this method in c#?


Solution

  • Declare the p/invoke like this:

    [DllImport(@"mydll.dll")]
    static extern void changenums(ref int a, ref int b);
    

    And call it like this:

    int a = 0;
    int b = 0;
    changenums(ref a, ref b);