unsigned char ** equivalent in c# and have to write the return value in a file

前端 未结 2 616
一生所求
一生所求 2020-12-21 15:41

I have to call a win32 dll function

int func1( int arg1, unsigned char **arg2, int *arg3);

and i need wrapped in c# as

pu         


        
相关标签:
2条回答
  • 2020-12-21 16:00

    i have a free function in the dll to clear the memory

    Then you have a shot at making this work. The function declaration ought to look like this:

    [DllImport("foo.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern int func1(int arg1, out IntPtr arg2, ref int arg3);
    

    And you'd call it like this:

    IntPtr ptr = IntPtr.Zero;
    int dunno = 99;
    string result = null;
    
    int retval = func1(42, out ptr, ref dunno);
    if (retval == success) {
        result = Marshal.PtrToStringAnsi(ptr);
        // etc...
    }
    if (ptr != IntPtr.Zero) func1free(ptr);
    

    Where "func1free" is the otherwise undocumented function that releases the string.

    0 讨论(0)
  • 2020-12-21 16:08

    You probably need to use the MarshalAs attribute, for example:

    public static extern int func1(int arg1, [MarshalAs(UnmanagedType.LPStr)] string arg2, IntPtr arg3);
    

    Check here for documentation:

    http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute.aspx

    0 讨论(0)
提交回复
热议问题