P/Invoke problem

S

Stefano Marzani

Dear all,
this code gives me a lot of problems:

private void btnCheck_Click(object sender, System.EventArgs e)
{
SystemPowerStatus SPS = new SystemPowerStatus();
GetSystemPowerStatus(SPS);
MessageBox.Show(Convert.ToString(SPS.ACLineStatus));
}

If I execute it in the emulator no problems. If I execute it on a device
(h5450) with a breakpoint, no problems. If I execute freely on the device
the application completely exits without prompting errors or warnings.

Someone could help me? Thanks,
Stefano
 
A

Alex Feinman [MVP]

You need to provide the P/Invoke definition for GetSystemPowerStatus and
SystemPowerStatus. The problem is between those two
 
S

Stefano Marzani

Here them are:

[StructLayout (LayoutKind.Sequential)]
public class SystemPowerStatus
{
public byte ACLineStatus;
public byte BatteryFlag;
public byte BatteryLifePercent;
public byte Reserved1;
public long BatteryLifeTime;
public long BatteryFullLifeTime;
}

[DllImport("coredll.dll", EntryPoint = "GetSystemPowerStatusEx")]
public static extern bool GetSystemPowerStatus(SystemPowerStatus
PowerStatus);

I hope this would help to resolve the problem. If it can, a dinner here in
Italy is offered by me ;-)))

Thanks,
Stefano
 
S

Stefano Marzani

No, unfortunately It doesnt't works.

The system gives me now a StackOverflowException both with int and long.
 
G

Guest

Ex uses the Ex structure, which is much larger than yours.

SM [Tue, 22 Jul 2003 09:29:44 +0200]:
 
A

Alex Feinman [MVP]

[DllImport("coredll")]
extern static public bool GetSystemPowerStatusEx(SYSTEM_POWER_STATUS_EX sps, bool fUpdate);

public class SYSTEM_POWER_STATUS_EX
{
public byte ACLineStatus { get { return (byte)(Block1 & 0xff); } }
public byte BatteryFlag { get { return (byte)((Block1 >> 8) & 0xff); } }
public byte BatteryLifePercent { get { return (byte)((Block1 >> 16) & 0xff); } }
public byte Reserved1 { get { return (byte)((Block1 >> 24) & 0xff); } }
private uint Block1;
public uint BatteryLifeTime;
public uint BatteryFullLifeTime;
public byte Reserved2 { get { return (byte)(Block2 & 0xff); } }
public byte BackupBatteryFlag { get { return (byte)((Block2 >> 8) & 0xff); } }
public byte BackupBatteryLifePercent { get { return (byte)((Block2 >> 16) & 0xff); } }
public byte Reserved3 { get { return (byte)((Block2 >> 24) & 0xff); } }
private uint Block2;
public uint BackupBatteryLifeTime;
public uint BackupBatteryFullLifeTime;
}

bool bRet = GetSystemPowerStatusEx(sps, true);



HTH

Alex
 
S

Stefano Marzani

Yes, it works fine ;-.)

Thanks to all.

Stefano
"Alex Feinman [MVP]" <[email protected]> ha scritto nel messaggio [DllImport("coredll")]
extern static public bool GetSystemPowerStatusEx(SYSTEM_POWER_STATUS_EX sps, bool fUpdate);

public class SYSTEM_POWER_STATUS_EX
{
public byte ACLineStatus { get { return (byte)(Block1 & 0xff); } }
public byte BatteryFlag { get { return (byte)((Block1 >> 8) & 0xff); } }
public byte BatteryLifePercent { get { return (byte)((Block1 >> 16) & 0xff); } }
public byte Reserved1 { get { return (byte)((Block1 >> 24) & 0xff); } }
private uint Block1;
public uint BatteryLifeTime;
public uint BatteryFullLifeTime;
public byte Reserved2 { get { return (byte)(Block2 & 0xff); } }
public byte BackupBatteryFlag { get { return (byte)((Block2 >> 8) & 0xff); } }
public byte BackupBatteryLifePercent { get { return (byte)((Block2 >> 16) & 0xff); } }
public byte Reserved3 { get { return (byte)((Block2 >> 24) & 0xff); } }
private uint Block2;
public uint BackupBatteryLifeTime;
public uint BackupBatteryFullLifeTime;
}

bool bRet = GetSystemPowerStatusEx(sps, true);



HTH

Alex
 

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