C++ DLL Parameter Conversion

D

DelGator

I know I'm going to feel like a fool when this is answered, but I am
importing a C++ DLL that has an out parameter that is an "unsigned
long *". What do I declare in my import to represent that parameter?

C++ sig:

HRESULT SomeFunction(/*[out]*/ unsigned long * aVariable)

C# code:

[DllImport("file.dll")]
public static extern int SomeFunction(ref ?)

What goes in place of the "?"?

Thanks in advance...
 
A

Arne Vajhøj

I know I'm going to feel like a fool when this is answered, but I am
importing a C++ DLL that has an out parameter that is an "unsigned
long *". What do I declare in my import to represent that parameter?

C++ sig:

HRESULT SomeFunction(/*[out]*/ unsigned long * aVariable)

C# code:

[DllImport("file.dll")]
public static extern int SomeFunction(ref ?)

What goes in place of the "?"?

I would say uint.

Arne
 
J

Jeroen Mostert

I know I'm going to feel like a fool when this is answered, but I am
importing a C++ DLL that has an out parameter that is an "unsigned
long *". What do I declare in my import to represent that parameter?

C++ sig:

HRESULT SomeFunction(/*[out]*/ unsigned long * aVariable)

C# code:

[DllImport("file.dll")]
public static extern int SomeFunction(ref ?)

What goes in place of the "?"?

Thanks in advance...

This question is not so foolish at all, because the an "unsigned long"
doesn't have an exact size specified by the C++ standard, merely that it's
at least 32 bits long. .NET, on the other hand, only has exactly sized
types, with the exception of IntPtr, which will change size depending on the
platform.

On Windows, it happens to be so that "long" is always the same size as
"int", which in turn is always 32 bits. Therefore, either "int" or "uint"
will do to marshal longs. On .NET, it's convention to use signed types even
for unsigned quantities for ease of use interoperability, since not all
languages have unsigned types. Only if you only use the value internally and
you're expecting large values often (or you have to do arithmetic on the
result) is it more sensible to use the unsigned types.

Your prototype isn't quite right, though. This function follows the standard
COM pattern, in which the return value is stored in an output parameter (not
an input/output parameter) and success or failure is indicated by an
HRESULT. This is equivalent to this in .NET:

[DllImport("file.dll")]
private static extern int SomeFunction(out int aVariable);

// Managed wrapper.
public static int SomeFunction() {
int aVariable;
int hresult = SomeFunction(out aVariable);
Marshal.ThrowExceptionForHR(hresult);
return aVariable;
}

The managed equivalent is a lot clearer and saves you from checking error
values.

If you import a COM type library containing such functions, such wrappers
are automatically generated.
 

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