?
Solved

Writing To registry

Posted on 2003-02-21
2
Medium Priority
?
278 Views
Last Modified: 2010-04-01
Is it possible in c++ or Vc++ to write directly in to Windows registry? plz post sample code to add a key in registry.
I know that this is possible in Vb and C-sharp but problem in both case is that they required Dll and Sdk respectively.
I want to just make a file which when clicked , just add key into registry, with out looking for DLL or Stuff like sdk before executing.
Means there must be less dependency on other shared routines ,and result of execution must be confident like C-program.(Not like Vb, when Dll Found on system every thing is Ok, but there is no Dll, No Execution).
Suggest me any other language,in this scenario, if C++ or vc didnt fit.
0
Comment
Question by:ammad
  • 2
2 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 7996477
You can use the CRegKey class to write to the registry.  It's used like:
CRegKey rk;
if(rk.Open(HKEY_LOCAL_MACHINE, "Software\\MyCompany\\Stuff", KEY_ALL_ACCESS) != ERROR_SUCCESS)
{
// Error out
  return;
}
if(rk.SetDWORDValue("mynumber", 42) != ERROR_SUCCESS)
{
  // Error out
  return;
}
if(rk.SetStringValue("mystring", "beer") != ERROR_SUCCESS)
{
  // Error out
  return;
}
rk.Close();

There's also a create function if you need to create a new key.
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 100 total points
ID: 7996499
If you want to use the API calls directly, you can use RegOpenKeyEx, RegSetValueEx, and RegCloseKey, like:

HKEY odbc_key;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\ODBC\\ODBC.INI", 0, KEY_READ | KEY_WRITE, &odbc_key) != ERROR_SUCCESS)
  {
    MessageBox("Could not open the ODBC.INI key", "ERROR", MB_ICONSTOP);
    return;
  }

  DWORD type = REG_SZ;
 
  // Store the new values
  RegSetValueEx(odbc_key, "ArrayBufferSize", NULL, type, reinterpret_cast<unsigned char *>("8"), strlen("8"));
 
  RegCloseKey(odbc_key);
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

621 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