Pass object type by reference

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
I am working with an unmanaged API that can receive various structures and fill them in. For simplicitys sake, I will say that it looks like this:

<div style="color:Black;background-color:White; <pre>
DWORD GetStructureInfo(VOID * pStructInfo, DWORD * pdwSize);
[/code]
The return value is an error code, pStructInfo is the structure to be filled in, and pdwSize is the size of pStructInfo. I have successfully wrapped this API in C# by doing this:<br/>



<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; struct FIRST_STRUCT
{
<span style="color:Blue; uint data1;
<span style="color:Blue; uint data2;
}

<span style="color:Blue; public <span style="color:Blue; struct SECOND_STRUCT
{
<span style="color:Blue; byte data1;
<span style="color:Blue; byte data2;
}

[DllImportAttribute(<span style="color:#A31515; "OEM_API.dll", EntryPoint = <span style="color:#A31515; "GetStructureInfo")]
<span style="color:Blue; internal <span style="color:Blue; static <span style="color:Blue; extern <span style="color:Blue; uint GetStructureInfo(<span style="color:Blue; ref FIRST_STRUCT pStructInfo, <span style="color:Blue; ref <span style="color:Blue; uint pdwSize);

[DllImportAttribute(<span style="color:#A31515; "OEM_API.dll", EntryPoint = <span style="color:#A31515; "GetStructureInfo")]
<span style="color:Blue; internal <span style="color:Blue; static <span style="color:Blue; extern <span style="color:Blue; uint GetStructureInfo(<span style="color:Blue; ref SECOND_STRUCT pStructInfo, <span style="color:Blue; ref <span style="color:Blue; uint pdwSize);

[/code]

However, it stops working when I do this instead:

<div style="color:Black;background-color:White; <pre>
[DllImportAttribute(<span style="color:#A31515; "OEM_API.dll", EntryPoint=<span style="color:#A31515; "GetStructureInfo")]
<span style="color:Blue; internal <span style="color:Blue; static <span style="color:Blue; extern <span style="color:Blue; uint GetStructureInfo(<span style="color:Blue; ref <span style="color:Blue; object, <span style="color:Blue; ref <span style="color:Blue; uint pdwSize);
[/code]


Every time that I try to feed either one of the structures to this, it fails, returning ERROR_INVALID_DATA, which the documentation indicates means a null reference was given. I tried using "object" in place of "ref object", and
I tried turning FIRST_STRUCT and SECOND_STRUCT into classes rather than structs, but all without success.
Is there any way that I can pass a structure by reference as an object type? Or perhaps as an IntPtr? I need to be able to pass in a structure of any size, and based on the pdwSize parameter that I pass in, the API call determines how to fill
the structure in.
Thanks in advance for any tips.


View the full article
 
Back
Top