Battery status for PocketPC

Thanks for your previous reply...

I am now tryin to use GetSystemPowerStatusEx in a C# form application for PocketPC and while importing the dll and declaring the extern function it throws me an error. Please help.

CODE: form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

[DllImport("coredll.dll")] *** dis and next line flash an error

extern bool GetSystemPowerStatusEx(out SYSTEM_POWER_STATUS_EX pStatus, bool fUpdate);

namespace battapp

{ public partial class Form1 : Form

{public Form1()

{InitializeComponent();}

private void button1_Click(object sender, EventArgs e)

{SYSTEM_POWER_STATUS_EX powerStatus;

GetSystemPowerStatusEx(powerStatus, true);

percent.Text=powerStatus.BackupBatteryLifePercent;}

} }

ERROR:

error CS1518: Expected class, delegate, enum, interface, or struct

error CS0106: The modifier 'extern' is not valid for this item

Thanks




Answer this question

Battery status for PocketPC

  • John007

    Hi Bruce,

    Thanks for the suggestion but i tried that as well and it is giving me 1 error

    Here's my code for that

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using System.Runtime.InteropServices;

    // Type to hold data returned from the unmanaged

    // GetSystemPowerStatusEx function

    internal class SYSTEM_POWER_STATUS_EX

    {

    public byte ACLineStatus;

    public byte BatteryFlag;

    public byte BatteryLifePercent;

    public byte Reserved1;

    public uint BatteryLifeTime;

    public uint BatteryFullLifeTime;

    public byte Reserved2;

    public byte BackupBatteryFlag;

    public byte BackupBatteryLifePercent;

    public byte Reserved3;

    public uint BackupBatteryLifeTime;

    public uint BackupBatteryFullLifeTime;

    }

    [DllImport("coredll.dll", EntryPoint = "GetSystemPowerStatusEx", SetLastError = true)]

    static extern bool GetSystemPowerStatusEx(out SYSTEM_POWER_STATUS_EX lpSystemPowerStatus, bool fUpdate); /***** this line flashes an error at bool(even tried replacing that with uint - it says Expected class, delegate, enum, interface...) PLEASE HELP ******/

    namespace battapp

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    SYSTEM_POWER_STATUS_EX status = new SYSTEM_POWER_STATUS_EX();

    if(GetSystemPowerStatusEx(status, true))

    percent.Text=status.BackupBatteryLifePercent;

    else

    percent.Text="error";

    }

    }

    }



  • LGuy

    Use the OpenNETCF.Windows.Forms.BatteryLife class in the SDF: http://opennetcf.org/sdf2

    Cheers

    Daniel



  • Whisk

    Try placing the p/invoke declaration inside the definition for Form1.

    public partial class Form1 : Form

    {

    // ...

    [DllImport("coredll", EntryPoint = "GetSystemPowerStatusEx", SetLastError = true)]

    private static extern bool GetSystemPowerStatusEx(SYSTEM_POWER_STATUS_EX lpSystemPowerStatus, bool fUpdate);

    // ...

    }



  • Ven

    Try something like:

    struct SYSTEM_POWER_STATUS_EX
    {
    byte ACLineStatus;
    byte BatteryFlag;
    byte BatteryLifePercent;
    byte Reserved1;
    uint BatteryLifeTime;
    uint BatteryFullLifeTime;
    byte Reserved2;
    byte BackupBatteryFlag;
    byte BackupBatteryLifePercent;
    byte Reserved3;
    uint BackupBatteryLifeTime;
    uint BackupBatteryFullLifeTime;
    }


    [DllImport("coredll")]
    static extern bool
    GetSystemPowerStatusEx(out SYSTEM_POWER_STATUS_EX pstatus, bool fUpdate);

    SYSTEM_POWER_STATUS_EX pwr_status = new SYSTEM_POWER_STATUS_EX();
    bool success = GetSystemPowerStatusEx(out pwr_status, false);

    ...

  • Battery status for PocketPC