Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Reading path names from the registry

Posted on 1999-09-07
Medium Priority
Last Modified: 2010-04-16
I have a setup program for an application which now stores the path name for my application in the registry.  It is found under the following key:

When running my application needs to access two other executable files and also two text files which are either stored in the same location as the main application or in subfolders.  I now know that to retrieve the path information I need to use the RegOpenKeyEx and RegqueryValueEx functions.  

Am I right in saying that the RegOpenKeyEx function simply opens the key underwhich my path information is stored?  And then the RegqueryValueEx function is used to retrieve the information?

My problem is with the RegqueryValueEx function, I'm not sure of what the parameters are, how the value is returned and then how to use it.  This is probably best illustrated with an example.

HKEY MyHKEY_Software;

//Now opened the key with MyHKEY_Software containing address of handle to open key
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Litriu.exe",0,KEY_ALL_ACCESS, &MyHKEY_Software);

\\Now need to retrieve the data for the 'path'

char MyProgramPath[4048];
unsigned long SizeOfBuf = sizeof(MyProgramPath);
unsigned long MyREG_Size;
RegQueryValueEx(MyHKEY_Software,"Path", 0, &MyREG_Size, (unsigned char *)MyProgramPath,

\\Does this mean that the path name is now stored in MyProgramPath?
How do I open a text file stored at that location?

I tried stuff like this but it wouldn't work:
ifstream InDictFile(MyProgramPath + "list.txt", ios::in);

I know this probably seems simple to most people but i need someone to explain exactly how the last three parameters work in the RegqueryValueEx function.

Question by:mcgettiu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 22

Accepted Solution

nietod earned 400 total points
ID: 2027670
>> Am I right in saying that the RegOpenKeyEx function
>> simply opens the key underwhich my path information
>> is stored?  And then the RegqueryValueEx function is
>> used to retrieve the information?
Yes.  mostly.  There can be multiple values stored under each key.  these values are identified by their names.  RegQueryValeEx() obtains the value associated with a single named value.

LVL 22

Expert Comment

ID: 2027681
You are not using the last parameters corrrectly (this you already knew).

The lpType parameter should point to a DWORD that you have allocated.  The procedure will set this DWORD to a value that indicates the type of date read by the function.  (The data might not be an ASCII string, although it should be if you are reading a path, but the function can be used to read many other data types used for other purposes.)  You have this parameter point to a size value, which suggests you missunderstood its purpose.

the lpcbData parameter should point to a value that initially specifies the size of the data buffer you specified in the lpData parameter.  This is so the function does not write data that is larger than the buffer you specified.  The function will alter this value to indicate the size of the data that was actually placed in the buffer.  In yoru case you did not initialize the value passed, so the function did not know how large the data buffer was.

LVL 22

Expert Comment

ID: 2027694
Opps, I'm wrong, you did initialize the size passed in the last parameter.  Your two sizes got me messed up.  anyways it should be like

char MyProgramPath[4048];
DWORD SizeOfBuf = sizeof(MyProgramPath);
DWORD MyREG_Type;  // type not size.

RegQueryValueEx(MyHKEY_Software,"Path", 0, &MyREG_Type, (unsigned char *)MyProgramPath,&SizeOfBuf);

When the function returns, the MyReg_Type should be set to REG_SZ to indicate a NUL terminated string was returned in the buffer.  If it is another value, you probalby can't use the data returned.  
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 2027868
Just one question, once I have altered the parameter definitions to what you explained will the following work?  Providing I first check that MyProgramPath does indeed contain a null terminated string.

ifstream InDictFile(MyProgramPath + "list.txt", ios::in);

LVL 22

Expert Comment

ID: 2027897
it should assuming MyProgramPath contains a VALID path strng.  i.e. if it specifies directories that don't exist, it won't work.  Also it must end in a backslash.  You might want to check if it ends in a backslash, and if not, append one yourself.

Author Comment

ID: 2027927
Thanks a mill
I'll try it out now


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question