Hi,
Paul, I understand how to implement your suggestions if the parameter
is an output. However, I am not sure how to convert the
POWER_RELATIONSHIP structure to the format as suggested as I do not
know the value of the strings nor the handles. The structure found in
source code is below:
//structure as defined in dll
typedef struct _POWER_RELATIONSHIP {
HANDLE hParent;
LPCWSTR pwsParent;
HANDLE hChild;
LPCWSTR pwsChild;
} POWER_RELATIONSHIP, *PPOWER_RELATIONSHIP;
The other question that I have is how do I find what the handles are
and what are the names for the child and parent? Thanks
Paul G. Tobey [eMVP] wrote:
Those embedded arrays have to have some length to them! You can't
declare
them as managed (unspecified length), arrays and expect them to end up
in
the right place inside the structure when you pass it to unmanaged code.
I'm partial to doing that by declaring the 'structure' as a class,
somewhat
like this:
public class POWER_CAPABILITIES
{
// And declaring a byte array, which is what will actually be passed
to
things like
// DeviceIoControl().
public const int size = length of the power capabilities structure
in
unmanaged code;
public byte[] data = new byte[ size ];
// And creating 'properties' which map the names of fields in the
structure to the right offsets
// in the managed array of bytes.
internal const int DeviceDxOffset = 0;
public byte DeviceDx
{
get
{
return byte[ DeviceDxOffset ];
}
set
{
byte[ DeviceDxOffset ] = value;
}
}
...
internal const int FlagsOffset = ????;
public int Flags
{
get
{
return BitConverter.ToUInt32(data, FlagsOffset);
}
set
{
byte[] bint = BitConverter.GetBytes(value);
Array.Copy(bint, 0, data, FlagsOffset, bint.Length);
}
}
// Finally, you'll have to figure out how best to allow access to
the
arrays. You might
// declare the accessor as an array and create and return an
embedded
array in the get
// operation and copy the incoming array using System.Array.Copy()
on
the set operation.
}
If you do this, the DeviceIoControl() function would be declared as
taking a
byte[], not a structure of a particular type. This scheme for building
a
wrapper around an array of bytes is used all over the place in
OpenNETCF.Net, so you can find many examples of the various types that
you
might have convert pieces of the byte[] into and out of (32-bit
integers,
etc., etc.).
Paul T.
Chris,
Thanks for the response. I am still having difficulties to make this
P/Invoke work.
The C/C++ signature of the method is
BOOL PWM_IOControl (DWORD hOpenContext, DWORD pwCode, PBYTE pBufIn,
DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualAout)
Where hOpenContext -- handle of the open context of the device.
pwCode -- I/O control operations to perform. I have
determined what these constants are
pBufIn -- for IOCTL_POWER_SET and IOCTL_POWER_CAPABILITIES,
this is a pointer to the POWER_RELATIONSHIP structure.
dwLenIn - number of bytes in pBufIn.
pBufOut - poiner to the buffer used to transfer spcecified
output data. For IOCTL_POWER_CAPABILITIES, it is a pointer for the
POWER_CAPABILITIES strucure. And for IOCT_POWER_GET, it should point
to CEDEVICE_POWER_STATE
dwLenOut - number of bytes in pBufOut.
pdwActualOut -- the number of actual bytes received from th
device.
//struc defs found in pm.h
public unsafe struct POWER_RELATIONSHIP
{
IntPtr hParent;
string pwsParent,
IntPtr hChild;
string pwsChild;
}
public unsafe struct POWER_CAPABILITIES
{
char DeviceDx;
char WakeFromDx;
char InrushDx;
Int32[] Power; // 5 elements?? from pm.h
Int32[] Latency; // 5 elements??
Int32 Flags
}
//to get the device power capabilities
[Dllimport("PWM.dll")]
public static extern bool PWM_IOControl(IntPtr hOpenContext, UInt32
dwCode, ref POWER_RELATIONSHIP pBuffIn, UInt32 dwLenIn, ref
POWER_CAPABILITIES pBufOut, UInt32 dwLenOut, byte[] pdwActualOut);
What I can not figure out is how to get the size of the structure used
since sizeof does not work. Also, two different strcutures need to be
passed depending if I want to exercise IOCTL_POWER_GET or
IOCTL_POWER_CAPABILITIES, how can I derive two different method calls
based on the same method in the dll (PWM_IOControl)? Any comments is
welcomed. Thanks.
Regards,
mario
<ctacke/> wrote:
From pm.h, line 188:
typedef struct _POWER_RELATIONSHIP {
HANDLE hParent; // Handle to parent node
LPCWSTR pwsParent; // Named parent node, e.g. "NDS0:"
HANDLE hChild; // Handle to child node, returned from
RegisterPowerRelationship
LPCWSTR pwsChild; // Named child node, e.g. "NE20001"
} POWER_RELATIONSHIP, *PPOWER_RELATIONSHIP;
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Hi,
I am trying to call IOCTL_POWER_GET and IOCTL_POWER_CAPABILITIES
methods through P/Invoke. Both methods have one of the input
parameters as POWER_RELATIONSHIP structure. How or where can I
find
what are the values of these members? Thanks.
Regards,
mario