Windows CE: Reading a String from the Registry

Windows CE: Reading a String from the Registry
Created by samphung on 11/20/2010 10:54:27 PM

Programmatically read a string from the Windows CE registry.  


I made a change to a string value in the registry recently. That seemed like a harmless thing to do, didn’t it? But, what I did was make the string longer than it was before, again seemed harmless.  Harmless until some applications started reading the value into arrays with hard coded length, the problems began.

The problem is that RegQueryValueEx() does not have a parameter to indicate the size of the buffer that it will put data into.  This means that if the data is bigger than the buffer, RegQueryValueEx() will write past the buffer causing problems for your system.  The good news is that you can use RegQueryValueEx() to discover the size of the buffer that is required, and then allocate a buffer that is large enough.

So the following code reads a string from the registry by getting the string length first, then allocating a buffer, then reading the string:

TCHAR *EventName = NULL;

DWORD Result;

HKEY hKey;

DWORD NumBytes = 0;

DWORD Type;

HANDLE UserEvent = INVALID_HANDLE_VALUE;

 

// Open the Registry Key

Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCWSTR)GWE_REG_PATH, 0, 0, &hKey);

 

if( ERROR_SUCCESS == Result )

{

                // This is a fake read, all it does is fill in NumBytes with the number of

                // bytes in the string value plus the null character.

                Result = RegQueryValueEx( hKey, ACTIVITY_VALUE, NULL, &Type, NULL, &NumBytes );

                if( NumBytes > 0 )

                {

                                // Now we know how big the string is allocate and read it

                                EventName = (TCHAR *)malloc( NumBytes );

                                if( EventName != NULL )

                                                Result = RegQueryValueEx( hKey, ACTIVITY_VALUE, NULL, &Type,

                                                                (LPBYTE)EventName, &NumBytes );

                }

                RegCloseKey( hKey );

 

                UserEvent = CreateEvent( NULL, FALSE, FALSE, EventName );

                free( EventName );

}

 

// Do something with UserEvent

 

 

Note:  This article is written by Bruce Eitman, and is posted to the Embedded101 site with Bruce’s permission.

Copyright © 2010 – Bruce Eitman – All Rights Reserved

http://geekswithblogs.net/BruceEitman/

Embedded101 Articles

Click to Expand/Collapse Groups
Skip Navigation Links.
Collapse Windows Embedded Compact (CE)Windows Embedded Compact (CE)
Collapse Compact 7Compact 7
Collapse CE 6.0CE 6.0
Collapse CE 5.0CE 5.0
Collapse Platform Builder & OS DesignPlatform Builder & OS Design
Collapse BSP, OAL & BootloaderBSP, OAL & Bootloader
Collapse RegistryRegistry
Collapse Device DriverDevice Driver
Collapse File SystemFile System
Collapse Application DevelopmentApplication Development
IBW & ICE
Stream Driver with CEDriverWiz rev 0...
Connecting Visual Studio IDE to CE 6...
Platform Builder: Automatically Flus...
Windows CE: Save and Restore the Re...
Windows CE: Stream Interface Driver...
Windows CE: Persisting Registry Chan...
Windows CE: Enhanced BusEnum
Windows CE: Reading a String from th...
Windows CE: Displaying Disk Informa...
Windows CE: Formatting TFAT
Windows CE: C# Application to Format...
Build A Windows Network Projector wi...
Hive-Based Registry for CE 6.0
AutoLaunch for CE 6.0
Hello all
Filter Device Drivers
Asynchronous I/O request support
Configure Flash Storage to Launch Co...
CEDriverWiz Version 2.00 User Manual
CEDriverWiz V2.00: About this releas...
Installing CEDriverWiz for Visual St...
CEDriverWiz Version 2.00: An Example
CEDriverWiz Version 2.00: Using the ...
Collapse Windows Embedded StandardWindows Embedded Standard
Collapse NET Compact FrameworkNET Compact Framework
Windows Phone 7
Misc.
Turkish porno izle video site in rokettubeporno izle