Using Win32APIs that pass back a struct which contains a pointer to a struct

  • Thread starter Thread starter J
  • Start date Start date
J

J

I'm at a loss on how to accomplish one item with C# entirely in
managed code -- I'd like to make a call to a Win32 function, one of
its parameters is a structure that contains a pointer to one or more
structures. How do I setup the structure in C# to allow for the
pointer to a structure?

The structure that I'm referring to is MIXERLINECONTROLS and it's
declaration is the following:
typedef struct {
DWORD cbStruct;
DWORD dwLineID;
union {
DWORD dwControlID;
DWORD dwControlType;
};
DWORD cControls;
DWORD cbmxctrl;
LPMIXERCONTROL pamxctrl;
} MIXERLINECONTROLS;

For the API that I want to use, it requires that the pamxctrl member
must point to the first MIXERCONTROL structure to be filled.

I can map out most of the C++ struct into the C# equivalent to be able
to do this fully in managed code so it looks like this:

[ StructLayout( LayoutKind.Sequential )]
public struct MIXERLINECONTROLS
{
public uint cbStruct; /* uint is DWORD in C++ */
public uint dwLineID;
union {
public uint dwControlID;
public uint dwControlType;
};
public uint cControls;
public uint cbmxctrl;

LPMIXERCONTROL pamxctrl; //<< **HOW DO I CONVERT THIS INTO C#? >>
};

When I call the Win32API that makes use of the MIXERLINECONTROLS
struct, I'm not sure how to set up
1) the pointer to the structure that's necessary in the definition of
the struct for C#
2) the allocation of the correct amount of memory for pamxctrl before
I pass it into the call
so that I can get back the list of array items of MIXERCONTROL
3) the accessing of the list of array items of pamxctrl that I get
back from the call.

Could someone point out some suggestions for me? I'd really appreciate
it.

Thanks in advance,
J
 
1) the pointer to the structure that's necessary in the definition of
the struct for C#

Make it an IntPtr member.

2) the allocation of the correct amount of memory for pamxctrl before
I pass it into the call

With one of the Marshal.Alloc methods.

3) the accessing of the list of array items of pamxctrl that I get
back from the call.

Marshal.PtrToStructure()



Mattias
 
J,

I don't have time to test this out but since all ValueTypes have the ability
of using the IntPtr. I would first try replacing the C# struct element
LPMIXERCONTROL with an Int32(int) element. Then create the MIXERCONTROL
struct fill it in. Set the LPMIXERCONTROL Int32 element in MIXERLINECONTROLS
to the IntPtr.

Then I would test this by writing a unmanaged C++ console app and decalre
the 2 structs. The just call a managed dll method that returns it, so you
can check the return for valid values.

Hope this helps or at least gets you going in the right direction.
 
Mattias,
I'm following your suggestions. I can allocate enough memory for the
pamxtrl but I am still confused on how to extract each individual
array items that I get back from the call. When PtrToStructure is
used, I'd get the first item as a struct. How can I advance the
pointer in C# to access next set of items in the array? Am I
misunderstanding how this particular marshalling works?

Thanks for your help,
Jay
 
Back
Top