IOCTL_POWER_XXX method calls

  • Thread starter Thread starter Mario
  • Start date Start date
M

Mario

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
 
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,

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
 
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.

Mario said:
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





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,

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


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.

Mario said:
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





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
 
These strings will be IntPtrs. You then have to marshal from the ptr to a
string.

-Chris

Mario said:
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


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.

Mario said:
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
 
Handles could be encoded as Int32's or IntPtr's. It's just sort of a opaque
32-bit integer type. For the strings, I'm not so sure. You could certainly
declare them unsafe as a Char*, but declaring them as string might even
work, since they're just string pointers, not embedded strings with fixed
lengths, as is often the case with unmanaged code.

The handles would be things returned when the drivers, bus for the parent,
child for the child, were registered/loaded. Looking at the docs on
IOCTL_POWER_CAPABILITIES, though, it looks to me like you'd want to just
pass NULL for the POWER_RELATIONSHIP. You aren't a bus or a child driver,
since you're in managed code.

Sorry, I'm not much of an expert on power-related stuff; our hardware only
has two power states, A/C power running at full speed, and off. What,
again, are you actually trying to do here? Find out what power states some
device (which one), supports?

Paul T.

Mario said:
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


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.

Mario said:
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
 
You'd want to use a StringBuilder that is initialized to long-enough for the
data if you don't manually use ptrs.

-Chris


"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> wrote in message news:%[email protected]...
Handles could be encoded as Int32's or IntPtr's. It's just sort of a
opaque 32-bit integer type. For the strings, I'm not so sure. You could
certainly declare them unsafe as a Char*, but declaring them as string
might even work, since they're just string pointers, not embedded strings
with fixed lengths, as is often the case with unmanaged code.

The handles would be things returned when the drivers, bus for the parent,
child for the child, were registered/loaded. Looking at the docs on
IOCTL_POWER_CAPABILITIES, though, it looks to me like you'd want to just
pass NULL for the POWER_RELATIONSHIP. You aren't a bus or a child driver,
since you're in managed code.

Sorry, I'm not much of an expert on power-related stuff; our hardware only
has two power states, A/C power running at full speed, and off. What,
again, are you actually trying to do here? Find out what power states
some device (which one), supports?

Paul T.

Mario said:
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


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
 
Hi,

I am trying to make the buzzer work by using the built-in PWM.

-- mario

Handles could be encoded as Int32's or IntPtr's. It's just sort of a opaque
32-bit integer type. For the strings, I'm not so sure. You could certainly
declare them unsafe as a Char*, but declaring them as string might even
work, since they're just string pointers, not embedded strings with fixed
lengths, as is often the case with unmanaged code.

The handles would be things returned when the drivers, bus for the parent,
child for the child, were registered/loaded. Looking at the docs on
IOCTL_POWER_CAPABILITIES, though, it looks to me like you'd want to just
pass NULL for the POWER_RELATIONSHIP. You aren't a bus or a child driver,
since you're in managed code.

Sorry, I'm not much of an expert on power-related stuff; our hardware only
has two power states, A/C power running at full speed, and off. What,
again, are you actually trying to do here? Find out what power states some
device (which one), supports?

Paul T.

Mario said:
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


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
 
Thank you guys for your input. It is working now, I did not have to
change the power settings after all.

cheers,

mario

You'd want to use a StringBuilder that is initialized to long-enough for the
data if you don't manually use ptrs.

-Chris


"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> wrote in message news:%[email protected]...
Handles could be encoded as Int32's or IntPtr's. It's just sort of a
opaque 32-bit integer type. For the strings, I'm not so sure. You could
certainly declare them unsafe as a Char*, but declaring them as string
might even work, since they're just string pointers, not embedded strings
with fixed lengths, as is often the case with unmanaged code.

The handles would be things returned when the drivers, bus for the parent,
child for the child, were registered/loaded. Looking at the docs on
IOCTL_POWER_CAPABILITIES, though, it looks to me like you'd want to just
pass NULL for the POWER_RELATIONSHIP. You aren't a bus or a child driver,
since you're in managed code.

Sorry, I'm not much of an expert on power-related stuff; our hardware only
has two power states, A/C power running at full speed, and off. What,
again, are you actually trying to do here? Find out what power states
some device (which one), supports?

Paul T.

Mario said:
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
 
Back
Top