com interop passing parameter (DWORD *)null

G

Guest

I've got an app that I've written using C# and some type libraries I've
generated from some of the directx .IDL files.

I have got a problem with calling some of the methods on the generated
interfaces. As an example, I've got a method I want to call that has a
parameter call reserved, that must be null or the method call fails with
unexpected parameter.

int method(int p1, int p2, DWORD *reserverd)

When I reference the TLB from my C#, it expects a call like:

int method(int p1, int p2, ref uint reserved)

How can I pass null to this method() for the reserved parameter? No,
declaring 'uint reserved=0;' and passing that in doesn't do the job.

Any thoughts?
 
M

Mattias Sjögren

How can I pass null to this method() for the reserved parameter?

You can't. If you want to do that, you have to change the parameter
type in the interop assembly to an IntPtr (native int in ILAsm syntax)
or uint*.



Mattias
 
P

Peter Koen

I've got an app that I've written using C# and some type libraries
I've generated from some of the directx .IDL files.

I have got a problem with calling some of the methods on the generated
interfaces. As an example, I've got a method I want to call that has
a parameter call reserved, that must be null or the method call fails
with unexpected parameter.

int method(int p1, int p2, DWORD *reserverd)

When I reference the TLB from my C#, it expects a call like:

int method(int p1, int p2, ref uint reserved)

How can I pass null to this method() for the reserved parameter? No,
declaring 'uint reserved=0;' and passing that in doesn't do the job.

Any thoughts?

Use the MarshalAsAttribute:

public extern static int method(int p1, int p2,
[MarshalAs(UnmanagedType.U4)] IntPtr reserved);

IntPtr ptr = new IntPtr(0);
method(1,2,ptr);

or some other UnamangedType depending what you are expection on the last
param.

If there's to be an array passed you could also use
UnmanagedType.LPArray or SafeArray ... whatever you need.
 

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