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 7958881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:29:16+00:00 2026-06-04T04:29:16+00:00

I’m new to C++ and to WinCe developing. I want to read a string

  • 0

I’m new to C++ and to WinCe developing.

I want to read a string from the registry and display with the MessageBox(). I have tried the following.

HKEY key;
if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\GPS Intermediate Driver\\Drivers\\SiRFStar3HW"), 0, KEY_READ, &key) != ERROR_SUCCESS)
{
    MessageBox(NULL,L"Can't open the registry!",L"Error",MB_OK);
}
char value[5];
DWORD value_length=5;
DWORD type=REG_SZ;
RegQueryValueEx(key,(LPCTSTR)"Baud", NULL, &type, (LPBYTE)&value, &value_length);
wchar_t buffer[5];
_stprintf(buffer, _T("%i"), value);

::MessageBox(NULL,buffer,L"Value:",MB_OK);

::RegCloseKey(key);

So I know somethings wrong in here, but how can I solve?

  • 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-06-04T04:29:17+00:00Added an answer on June 4, 2026 at 4:29 am

    Navigating the Win32 API can be a tricky business. The registry APIs are some of the more complicated. Here’s a short program to demonstrate how to read a registry string.

    #include <Windows.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    wstring ReadRegValue(HKEY root, wstring key, wstring name)
    {
        HKEY hKey;
        if (RegOpenKeyEx(root, key.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
            throw "Could not open registry key";
    
        DWORD type;
        DWORD cbData;
        if (RegQueryValueEx(hKey, name.c_str(), NULL, &type, NULL, &cbData) != ERROR_SUCCESS)
        {
            RegCloseKey(hKey);
            throw "Could not read registry value";
        }
    
        if (type != REG_SZ)
        {
            RegCloseKey(hKey);
            throw "Incorrect registry value type";
        }
    
        wstring value(cbData/sizeof(wchar_t), L'\0');
        if (RegQueryValueEx(hKey, name.c_str(), NULL, NULL, reinterpret_cast<LPBYTE>(&value[0]), &cbData) != ERROR_SUCCESS)
        {
            RegCloseKey(hKey);
            throw "Could not read registry value";
        }
    
        RegCloseKey(hKey);
    
        size_t firstNull = value.find_first_of(L'\0');
        if (firstNull != string::npos)
            value.resize(firstNull);
    
        return value;
    }
    
    int wmain(int argc, wchar_t* argv[])
    {
        wcout << ReadRegValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion", L"CommonFilesDir");
        return 0;
    }
    

    Notes:

    1. I don’t have CE so this is a plain Win32 app, compiled for Unicode. I took that route because CE doesn’t do ANSI characters.
    2. I’ve taken advantage of a number of C++ features. Most significantly std::wstring. This makes string handling a cinch.
    3. I’ve used exceptions for error handling. You could replace that with some other mechanism, but it served my purpose of keeping the error handling issues in the background.
    4. Using exceptions makes closing the registry key slightly messy. A better solution would be to use an RAII class to wrap up the lifetime of the registry key. I’ve omitted that for simplicity, but in production code you would want to take that extra step.
    5. Usually, RegQueryValueEx returns REG_SZ data that is null-terminated. This code deals with that by truncating beyond the first null character. In case the value returned is not null-terminated, that truncation won’t happen, but the value will still be fine.
    6. I’ve just printed to my console, but it would be trivial for you to call MessageBox. Like this: MessageBox(0, value.c_str(), L"Caption", MB_OK)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
i want to parse a xhtml file and display in UITableView. what is the
Does anyone know how can I replace this 2 symbol below from the string
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.