- Home /
Equivalent of a void ** in from external library?
I'm working with an external library where a function has a void ** in its signature as an argument. How would one match this signature in c# without using unsafe code? Any help would be greatly appreciated!
What function are you trying to work with. If it is an API call there is a possibility that this void** is actually a pointer to a structure, or a pointer to an array. $$anonymous$$nowing the api call would help in producing C# code for it.
Answer by chase cobb · Aug 09, 2013 at 11:21 PM
This is what I was looking for.
http://msdn.microsoft.com/en-us/library/aa328695(v=vs.71).aspx
http://stackoverflow.com/questions/359541/interoping-between-c-sharp-and-an-unmanaged-c-library
There is also a tool to handle the data-type conversions automatically. PInvoke Signature Toolkit - > http://clrinterop.codeplex.com/releases/view/14120
Yea. I totally forgot that. I'm sorry. I'd like to see your code block which is consu$$anonymous$$g this function and passing the parameter with IntPtr.
Answer by Xtro · Aug 06, 2013 at 08:21 PM
Pointers are unsafe. End of story.
You can write a wrapper function which is handling the memory allocations and data movement but it has to be in C++/CLI. Not C#
I know that pointers are unsafe, but cant you normally pass a value in by ref to get the desired functionality from a native function looking for a pointer? For example:
c++:
void Cube(int * x);
c#:
void Cube(ref int x);
No. References are not the same thing. References are referencing a memory block in managed memory. Pointers may or may not be referencing the managed memory or unsafe memory.
You can't cast a pointer into a reference or viceversa.
.Net Wrapper classes and methods for a unmanaged dll gets implemented for this very reason. Wrapper method must create a memory block in the managed memory and copy the data from unsafe memory to managed memory. Then it can pass the newly created reference for the managed memory to the caller method. Wrappers are written in C++/CLI
I'm very familiar with the way that references and pointers are handled and their differences in c++, it's the managed code that I'm not familiar with. Thanks for the info!
If the info was sufficient please don't forget the mark my post as answer. Thank you :)