managed C# to unmanaged DLL void*

  • Thread starter Thread starter Mr.Tickle
  • Start date Start date
M

Mr.Tickle

Hi,

I have a C function in a DLL that takes a void* that is passing back some
other type, what is the general way to do this with object rather than
declaring the actual type that its returning.


ATM I am using the function that has the following signiture


long someFn(long l, void* data);

I am calling it in C# as follows...

[DllImport("someDll.dll", EntryPoint="someFn"]
public static extern int someFn(int l, ref double);

double d = 0.0;

int l = someFn(10, ref d);

Is there a way I can call it with the following type reference pased in?

object obj;

someFn(10, ref obj);
 
What makes you think the C function could return a pointer to an .NET object type?

Willy.
 
Just wanted a generic way of coding a void* instead of having to DllImport
them to every type I need.


Willy Denoyette said:
What makes you think the C function could return a pointer to an .NET object type?

Willy.


Hi,

I have a C function in a DLL that takes a void* that is passing back some
other type, what is the general way to do this with object rather than
declaring the actual type that its returning.


ATM I am using the function that has the following signiture


long someFn(long l, void* data);

I am calling it in C# as follows...

[DllImport("someDll.dll", EntryPoint="someFn"]
public static extern int someFn(int l, ref double);

double d = 0.0;

int l = someFn(10, ref d);

Is there a way I can call it with the following type reference pased in?

object obj;

someFn(10, ref obj);
 
Well I have it as...

[DllImport("someDll.dll", EntryPoint="someFn")]
public static extern int someFn(int l, [MarshalAs(UnmanagedType.AsAny)]ref
object);

[DllImport("someDll.dll", EntryPoint="someFn")]
public static extern int someFn(int l, ref double);


But I dont want to have X number of DllImport lines overloaded for every
type I have to pass and get back. There has got to be an easier less
verbose way surely.



Mr.Tickle said:
Just wanted a generic way of coding a void* instead of having to DllImport
them to every type I need.


Willy Denoyette said:
What makes you think the C function could return a pointer to an .NET object type?

Willy.
back
some
other type, what is the general way to do this with object rather than
declaring the actual type that its returning.


ATM I am using the function that has the following signiture


long someFn(long l, void* data);

I am calling it in C# as follows...

[DllImport("someDll.dll", EntryPoint="someFn"]
public static extern int someFn(int l, ref double);

double d = 0.0;

int l = someFn(10, ref d);

Is there a way I can call it with the following type reference pased in?

object obj;

someFn(10, ref obj);
 
But I dont want to have X number of DllImport lines overloaded for every
type I have to pass and get back.

How many overloads are we talking about?

There has got to be an easier less verbose way surely.

If you don't care about type safety, you could declare the method as
unsafe and actually type the parameter as void*.



Mattias
 
I dont know how many yet but quite a few as I am wrapping a huge amount of
DLL functions.
 
How come [MarshalAs(UnmanagedType.AsAny)] on the ref object parameter cant
resolve the type at runtime for every known type where possible?

I have one marshalled .AsAny but what purpose will that serve if I have to
overload them with the types as it seems I have to do.
 
Back
Top