managed C# to unmanaged DLL void*

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);
 
W

Willy Denoyette [MVP]

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

Willy.
 
M

Mr.Tickle

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);
 
M

Mr.Tickle

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);
 
M

Mattias Sjögren

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
 
M

Mr.Tickle

I dont know how many yet but quite a few as I am wrapping a huge amount of
DLL functions.
 
M

Mr.Tickle

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top