Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with an unicode String returned by DeviceIoControl

Help with an unicode String returned by DeviceIoControl

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelplearning
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabbana
    wrote on last edited by
    #1

    Hello, i have the following problem (i think it is a beginner question but i hope to get an informative answer!) Actually i try to get the BatteryDeviceName of my notebook. I use the code snipped from the MSDN. But at these step iam hanging because the DeviceIoControl returns a BATTERY_INFORMATION structure, because the InformationLevel (bqi) is set to BatteryInformation. BUT I want the BatteryDeviceName, so I changed the Information Level. Then the IOCTL_BATTERY_QUERY_INFORMATION should return a null terminated unicode string. My question is how can i store this ?string? Here is my code:

    BATTERY_INFORMATION bi = {0};
    bqi.InformationLevel = BatteryDeviceName;

    if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),&bi,sizeof(bi),&dwOut,NULL))
    {
    MessageBox(NULL,L"#8 - IOCTL_BATTERY_QUERY_INFORMATION ... OK",L"",MB_ICONINFORMATION);
    MessageBox(NULL,L"[BatteryDeviceName should be here !!!]",L"",MB_ICONINFORMATION);
    }

    As you can see i need to replace the bold line or more to store the String. I hope someone can help me. Thanks, gabbana

    M 1 Reply Last reply
    0
    • G gabbana

      Hello, i have the following problem (i think it is a beginner question but i hope to get an informative answer!) Actually i try to get the BatteryDeviceName of my notebook. I use the code snipped from the MSDN. But at these step iam hanging because the DeviceIoControl returns a BATTERY_INFORMATION structure, because the InformationLevel (bqi) is set to BatteryInformation. BUT I want the BatteryDeviceName, so I changed the Information Level. Then the IOCTL_BATTERY_QUERY_INFORMATION should return a null terminated unicode string. My question is how can i store this ?string? Here is my code:

      BATTERY_INFORMATION bi = {0};
      bqi.InformationLevel = BatteryDeviceName;

      if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),&bi,sizeof(bi),&dwOut,NULL))
      {
      MessageBox(NULL,L"#8 - IOCTL_BATTERY_QUERY_INFORMATION ... OK",L"",MB_ICONINFORMATION);
      MessageBox(NULL,L"[BatteryDeviceName should be here !!!]",L"",MB_ICONINFORMATION);
      }

      As you can see i need to replace the bold line or more to store the String. I hope someone can help me. Thanks, gabbana

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Maybe something like this:

      wchar_t szBatteryDeviceName[260];
      bqi.InformationLevel = BatteryDeviceName;

      if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),szBatteryDeviceName,sizeof(szBatteryDeviceName),&dwOut,NULL))
      {
      ...
      }

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      G 1 Reply Last reply
      0
      • M Mark Salsbery

        Maybe something like this:

        wchar_t szBatteryDeviceName[260];
        bqi.InformationLevel = BatteryDeviceName;

        if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),szBatteryDeviceName,sizeof(szBatteryDeviceName),&dwOut,NULL))
        {
        ...
        }

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        G Offline
        G Offline
        gabbana
        wrote on last edited by
        #3

        Great thank you, but i have one problem: If I use the szBatteryDeviceName in the MessageBox, he prints the correct batterydevicename but adds a part of the application path, so i think it ignores the null terminated string ?

        M 1 Reply Last reply
        0
        • G gabbana

          Great thank you, but i have one problem: If I use the szBatteryDeviceName in the MessageBox, he prints the correct batterydevicename but adds a part of the application path, so i think it ignores the null terminated string ?

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Interesting - I wouldn't expect that. I guess you could add the null terminator yourself:

          wchar_t szBatteryDeviceName[260];
          bqi.InformationLevel = BatteryDeviceName;

          if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),szBatteryDeviceName,sizeof(szBatteryDeviceName),&dwOut,NULL))
          {
          szBatteryDeviceName[dwOut/sizeof(wchar_t)] = 0;
          ...
          }

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          G 1 Reply Last reply
          0
          • M Mark Salsbery

            Interesting - I wouldn't expect that. I guess you could add the null terminator yourself:

            wchar_t szBatteryDeviceName[260];
            bqi.InformationLevel = BatteryDeviceName;

            if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),szBatteryDeviceName,sizeof(szBatteryDeviceName),&dwOut,NULL))
            {
            szBatteryDeviceName[dwOut/sizeof(wchar_t)] = 0;
            ...
            }

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            G Offline
            G Offline
            gabbana
            wrote on last edited by
            #5

            Ahh now it works fine. Great help, thanks! bye, gabbana

            G 1 Reply Last reply
            0
            • G gabbana

              Ahh now it works fine. Great help, thanks! bye, gabbana

              G Offline
              G Offline
              gabbana
              wrote on last edited by
              #6

              Okay, now i tried this with another type. I only get -1 as seconds, i am not sure if this is for a constant that the estimatedtime value is not available, but is the way correct for getting them: estimated time should return as an ULONG.

              ULONG estimatedtime;
              bqi.InformationLevel = BatteryEstimatedTime;
              DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),&estimatedtime,sizeof(ULONG),&dwOut,NULL);
              wchar_t szestimatedtime[260];
              wsprintf(szestimatedtime,L"%d seconds",estimatedtime);
              MessageBox(NULL,szestimatedtime,L"",0);

              Thanks for help! bye, gabbana

              M D 2 Replies Last reply
              0
              • G gabbana

                Okay, now i tried this with another type. I only get -1 as seconds, i am not sure if this is for a constant that the estimatedtime value is not available, but is the way correct for getting them: estimated time should return as an ULONG.

                ULONG estimatedtime;
                bqi.InformationLevel = BatteryEstimatedTime;
                DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),&estimatedtime,sizeof(ULONG),&dwOut,NULL);
                wchar_t szestimatedtime[260];
                wsprintf(szestimatedtime,L"%d seconds",estimatedtime);
                MessageBox(NULL,szestimatedtime,L"",0);

                Thanks for help! bye, gabbana

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                gabbana wrote:

                I only get -1 as seconds

                -1 == 0xFFFFFFFF == BATTERY_UNKNOWN_TIME (BatClass.h)

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                • G gabbana

                  Okay, now i tried this with another type. I only get -1 as seconds, i am not sure if this is for a constant that the estimatedtime value is not available, but is the way correct for getting them: estimated time should return as an ULONG.

                  ULONG estimatedtime;
                  bqi.InformationLevel = BatteryEstimatedTime;
                  DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),&estimatedtime,sizeof(ULONG),&dwOut,NULL);
                  wchar_t szestimatedtime[260];
                  wsprintf(szestimatedtime,L"%d seconds",estimatedtime);
                  MessageBox(NULL,szestimatedtime,L"",0);

                  Thanks for help! bye, gabbana

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  From MSDN, if the estimated time is unknown (for example, the battery is not discharging and the AtRate specified was 0) this will return BATTERY_UNKNOWN_TIME.

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups