Register Login
ArticlesEmbedded101 Article    May 20, 2012
Click to Expand/Collapse Groups
Skip Navigation Links.
Collapse Windows Embedded Compact (CE)Windows Embedded Compact (CE)
Collapse Compact 7Compact 7
Build A Windows Network Projector wi...
Filter Device Drivers
Asynchronous I/O request support
Configure Flash Storage to Launch Co...
Collapse CE 6.0CE 6.0
Stream Driver with CEDriverWiz rev 0...
Connecting Visual Studio IDE to CE 6...
Windows CE: Save and Restore the Re...
Windows CE: Stream Interface Driver...
Windows CE: Persisting Registry Chan...
Windows CE: Enhanced BusEnum
Windows CE: Soft Reset
Windows CE: Reading a String from th...
Windows CE: Displaying Disk Informa...
Windows CE: Formatting TFAT
Windows CE: C# Application to Format...
Hive-Based Registry for CE 6.0
AutoLaunch for CE 6.0
Configure Flash Storage to Launch Co...
Collapse CE 5.0CE 5.0
Configure Flash Storage to Launch Co...
Collapse Platform Builder & OS DesignPlatform Builder & OS Design
Platform Builder: Automatically Flus...
Windows CE: Enhanced BusEnum
Windows CE: Soft Reset
Windows CE: Displaying Disk Informa...
Build A Windows Network Projector wi...
Collapse BSP, OAL & BootloaderBSP, OAL & Bootloader
Windows CE 6.0: User Mode KernelIoC...
Windows CE: Displaying Disk Informa...
Collapse RegistryRegistry
Platform Builder: Automatically Flus...
Windows CE: Save and Restore the Re...
Windows CE: Stream Interface Driver...
Windows CE: Persisting Registry Chan...
Windows CE: Reading a String from th...
Hive-Based Registry for CE 6.0
Collapse Device DriverDevice Driver
Stream Driver with CEDriverWiz rev 0...
Windows CE: Stream Interface Driver...
Windows CE: Enhanced BusEnum
Collapse File SystemFile System
Windows CE: Formatting TFAT
Windows CE: C# Application to Format...
Collapse Application DevelopmentApplication Development
Connecting Visual Studio IDE to CE 6...
Windows CE: Persisting Registry Chan...
Windows CE: Reading a String from th...
Windows CE: Formatting TFAT
Windows CE: C# Application to Format...
AutoLaunch for CE 6.0
Windows CE: Stream Interface Driver ...
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
Test entry
Asynchronous I/O request support
Configure Flash Storage to Launch Co...
Collapse Windows Embedded StandardWindows Embedded Standard
WES7
WES-2009
Windows XP Embedded
Target Designer
Enhanced Write Filter
Collapse NET Compact FrameworkNET Compact Framework
Windows CE: C# Application to Format...
Windows Phone 7
Misc.
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/

print



rating
  Comments

There is no comments. To be the first to make a comment...

Your Name
Email
Website
Title
Comment
CAPTCHA image
Enter the code
 Copyright 2010 by Embedded101   Terms Of Use  Privacy Statement