?
Solved

Writing To registry

Posted on 2003-02-21
2
Medium Priority
?
273 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
[X]
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
  • 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 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.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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