Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 596997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:13:56+00:00 2026-05-13T16:13:56+00:00

I am trying to write the C# equivalent to the following: typedef struct BATT_ID

  • 0

I am trying to write the C# equivalent to the following:

typedef struct BATT_ID
{
    UINT8       nBattID[8];
} BATT_ID, *PBATT_ID;

HANDLE  g_hDevice;

// Connect to the driver
g_hDevice = CreateFile(L"BAT1:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

void GetBattID(PBATT_ID pBattId)
{
    // ... snipped code to check g_hDevice is valid ...

    DeviceIoControl(g_hDevice, SOMO650_PWR_GET_BATT_ID, NULL, 0, pBattId, sizeof(BATT_ID),  dwByteReturn, NULL))
}

// once BATT_ID has been filled it can be formatted as follows
wsprintf(strInfo, TEXT("%02X:%02X:%02X:%02X:%02X:%02X"), BattID.nBattID[6], BattID.nBattID[5], BattID.nBattID[4], BattID.nBattID[3], BattID.nBattID[2], BattID.nBattID[1]);

The code is connecting to the power driver of a Windows Mobile device and attempting to retrieve the battery Id.
This is for the latest ROM version of the SoMo650 and Socket are only able to provide sample code in C.

I can successfully do everything (as best as I can tell) apart from calling DeviceIoControl as I don’t know how to translate the BATT_ID structure into C#.

I’m guessing that as it’s a structure and DeviceIoControl expects a pointer I shoould be looking at Marshal.PtrToStructure() but I have very little C experience and feel very out of my depth.

Any assitance would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:13:57+00:00Added an answer on May 13, 2026 at 4:13 pm

    You might be better to use the Smart Device Framework which has a battery control in place..see here for the download link for the community edition.

    Edit: If you still want the pinvoke equivalent of the structure look here:

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct BATT_ID
    {
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 8, ArraySubType = System.Runtime.InteropServices.UnmanagedType.I8)]
          public int[] nBattId;
    };
    

    Then prior to p/invoking, you need the signature for the ‘DeviceIoControl‘ as shown:

    [DllImport("coredll.dll", EntryPoint="DeviceIoControl", SetLastError=true)]
            internal static extern int DeviceIoControlCE(
                int hDevice, 
                int dwIoControlCode, 
                byte[] lpInBuffer, 
                int nInBufferSize, 
                byte[] lpOutBuffer, 
                int nOutBufferSize, 
                ref int lpBytesReturned, 
                IntPtr lpOverlapped);
    

    The call would look like this:

    IntPtr ptr = IntPtr.Zero;
    BATT_ID battId;
    int sz = Marshal.SizeOf(battId.GetType());
    ptr = Marshal.AllocHGlobal(sz);
    Marshal.StructureToPtr((BATT_ID)battId, ptr, false);
    byte[] pBattId = ptr.ToPointer();
    out int bytesReturned = 0;
    DeviceIoControl(handle, IOCONTROL_ID, null, 0, pBattId, sz, ref bytesReturned, IntPtr.Zero);
    battId = Marshal.PtrToStructure(ptr, battId.GetType());
    Marshal.FreeHGlobal(ptr);
    

    I hope I have this right…

    Edit#2: As ctacke (thanks!) pointed out my code sample is wrong…

    unsigned byte[8] battId;
    DeviceIoControl(g_hDevice, SOMO650_PWR_GET_BATT_ID, null, 0, battId, Marshal.SizeOf(battId), ref bytesReturned, IntPtr.Zero);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write the equivalent of strchr, but with NSStrings... I've currently got
I am trying to write a Java equivalent for a function in C#. The
I'm trying to write the equivalent of $password = Security::hash('text pw', null, true) In
I'm trying to write an equivalent of Rails data model evolution/rollback mechanism using Spring
I'm trying to write the VBScript equivalent of a function similar to what's below:
I'm currently trying to write a Dump() method from LinqPad equivalent iin C# for
I'm trying to write the equivalent of an enum in C++ going in steps
I'm trying to write a function to get the Windows-equivalent of HOME. My C
I'm trying to write up the LINQ statement that is equivalent to: select e.EmployeeID,
I'm trying to write some code that sets a property on a struct (important

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.