void* interop?
-
how do I translate into C#: extern "C" int f(void*); I know about "unsafe", but I'd like it to be a normal DLLImport
The IntPtr type can handle void pointers. In fact, it has static methods specifically for dealing with them. Incidentally, "unsafe" doesn't mean "not safe to do", as the name implies. It just means the runtime cannot verify the types and such for us. I read somewhere that the name was chosen to scare unseasoned programmers away from using it. Internally, the CLR uses unsafe all the time. Cheers
-
The IntPtr type can handle void pointers. In fact, it has static methods specifically for dealing with them. Incidentally, "unsafe" doesn't mean "not safe to do", as the name implies. It just means the runtime cannot verify the types and such for us. I read somewhere that the name was chosen to scare unseasoned programmers away from using it. Internally, the CLR uses unsafe all the time. Cheers
Jeff J wrote: it has static methods specifically for dealing with which one? forgot the 2nd signature I am using extern "C" int f2(void**); I managed to make it work witch "ref uint" unfortunatly the first function does not take return value. DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f(uint s); DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f2(ref uint s); After I use return value for "f()" in "f2()" I got something like "'System.Runtime.InteropServices.MarshalDirectiveException' Can not marshal parameter #1: Invalid managed/unmanaged type combination (Int/UInt must be paired with I or U)" Any suggestions are welcome (I'll definitely try unsafe)
-
Jeff J wrote: it has static methods specifically for dealing with which one? forgot the 2nd signature I am using extern "C" int f2(void**); I managed to make it work witch "ref uint" unfortunatly the first function does not take return value. DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f(uint s); DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f2(ref uint s); After I use return value for "f()" in "f2()" I got something like "'System.Runtime.InteropServices.MarshalDirectiveException' Can not marshal parameter #1: Invalid managed/unmanaged type combination (Int/UInt must be paired with I or U)" Any suggestions are welcome (I'll definitely try unsafe)
-
Jeff J wrote: it has static methods specifically for dealing with which one? forgot the 2nd signature I am using extern "C" int f2(void**); I managed to make it work witch "ref uint" unfortunatly the first function does not take return value. DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f(uint s); DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f2(ref uint s); After I use return value for "f()" in "f2()" I got something like "'System.Runtime.InteropServices.MarshalDirectiveException' Can not marshal parameter #1: Invalid managed/unmanaged type combination (Int/UInt must be paired with I or U)" Any suggestions are welcome (I'll definitely try unsafe)
As leppie posted, there are many useful functions in the Marshal class, which is good to become familiar with if you are interfacing with external C or C++ functions. IntPtr itself also has useful methods, such as ToPointer() and ToInt32(), along with the statics Zero and operator overloads that can convert between void pointers and IntPtr types. Which ones to use depends on how you plan on calling the extern functions. It is hard to explain the usage details without seeing a code example. If you could post one with how the calls are intended to be made, and how the parameters are to be passed, I'm sure I could explain a solution. Regarding using the extern funcs you currently have, if the calling C# func is declared unsafe, then you can just cast your "ints" as in regular C code (including pointers and taking addresses). Or you can prototype the externs as unsafe from the start like: DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern unsafe int f(void *pVoid); DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern unsafe int f2(void **ppVoid); If you are looking to do this using the non-unsafe runtime interop features, you could do: DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f(IntPtr pVoid); DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f2(IntPtr ppVoid);