Editing the registry in c++?
Hello, I was wondering if I could get some help on this one. If anyone have some time to set of. You see I am trying to learn how to creat a file in the registry with c++.
I know the basic in c++ and some of .NET VB. And in vb is this the code that I use:
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Registry.SetValue("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Test", "Test9999", 1)
End Sub
So if any one oush that button they are going to creat a key in the registry named Test9999. So how do I do this in c++? I have been taking a look here: http://msdn.microsoft.com/library/default....gsetvalueex.asp
And tryed lots of thing like this:
int main ()
{
LONG RegSetValueEx(HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\, Test999, 0, REG_DWORD, test);
}```
our this:
```markup
#include <windows.h>
#include <iostream>
int main ()
{
HKEY hKey;
DWORD dwDisp = 0;
LPDWORD lpdwDisp = &dwDisp;
CString l_strExampleKey = "SOFTWARE\\Microsoft\\Exchange\\MSExchangeAdminCommon";
CString l_strDWordSample = "DWORDSample";
DWORD dwVal = 100;
LONG iSuccess = RegCreateKeyEx( HKEY_CURRENT_USER, l_strExampleKey, 0L,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,lpdwDisp);
if(iSuccess == ERROR_SUCCESS)
{
RegSetValueEx (hKey, l_strDWordSample, 0L, REG_DWORD,(CONST BYTE*) &dwVal, sizeof(DWORD));
}
}
But what ever I do, do I only get error when I am trying to bulid it. So can someone help me on this? Whit write me an example our something? Thanks
and for more registry fun: http://www.robvanderwoude.com/index.html that has all about doing regedit with DOS and command and batch files
Include <windows.h> and use the following example for adding to run, works with others: Code Sample
RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey2 ); RegSetValueEx(hKey2, "Name of File", 0, REG_SZ, File_Source_Path, sizeof(File_Source_Path)); RegCloseKey(hKey2);
edit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/registry_functions.asp for your registry funtions.*
Hope it helps
BC
Eh, no… BUt thanks for trying to help me :) This is the code:
#include <windows.h>
#include <iostream>
int main ()
{
RegOpenKeyEx(HKEY_CURRENT_USER,
"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
0,
KEY_SET_VALUE,
&hKey2 );
RegSetValueEx(hKey2,
"Name of File",
0,
REG_SZ,
File_Source_Path,
sizeof(File_Source_Path));
RegCloseKey(hKey2);
}
And this is the error log
------ Rebuild All started: Project: Test Prog, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'Test Prog', configuration 'Debug|Win32'
Compiling...
main.cpp
c:\documents and settings\hast5\mine dokumenter\visual studio 2005\projects\test prog\test prog\main.cpp(12) : error C2065: 'hKey2' : undeclared identifier
c:\documents and settings\hast5\mine dokumenter\visual studio 2005\projects\test prog\test prog\main.cpp(17) : error C2065: 'File_Source_Path' : undeclared identifier
c:\documents and settings\hast5\mine dokumenter\visual studio 2005\projects\test prog\test prog\main.cpp(18) : error C2070: ''unknown-type'': illegal sizeof operand
Build log was saved at "file://c:\Documents and Settings\Hast5\Mine dokumenter\Visual Studio 2005\Projects\Test Prog\Test Prog\Debug\BuildLog.htm"
Test Prog - 3 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
What am I doing wrong?
The code I posted works with Dev C++
Its the only thing I use o_O I am not the best at C++. I had found that off of http://www.rohitab.com/discuss/index.php?showtopic=8936&hl=registry
Rohitab is where I get all of my information on programming C++.
BC
BluePain wrote: Eh, no… BUt thanks for trying to help me :) This is the code:
#include <windows.h>
#include <iostream>
int main ()
{
RegOpenKeyEx(HKEY_CURRENT_USER,
"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
0,
KEY_SET_VALUE,
&hKey2 );
RegSetValueEx(hKey2,
"Name of File",
0,
REG_SZ,
File_Source_Path,
sizeof(File_Source_Path));
RegCloseKey(hKey2);
}
And this is the error log
------ Rebuild All started: Project: Test Prog, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'Test Prog', configuration 'Debug|Win32'
Compiling...
main.cpp
c:\documents and settings\hast5\mine dokumenter\visual studio 2005\projects\test prog\test prog\main.cpp(12) : error C2065: 'hKey2' : undeclared identifier
c:\documents and settings\hast5\mine dokumenter\visual studio 2005\projects\test prog\test prog\main.cpp(17) : error C2065: 'File_Source_Path' : undeclared identifier
c:\documents and settings\hast5\mine dokumenter\visual studio 2005\projects\test prog\test prog\main.cpp(18) : error C2070: ''unknown-type'': illegal sizeof operand
Build log was saved at "file://c:\Documents and Settings\Hast5\Mine dokumenter\Visual Studio 2005\Projects\Test Prog\Test Prog\Debug\BuildLog.htm"
Test Prog - 3 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
What am I doing wrong? Just read your errors. You clearly haven't declared hKey2 or File_Source_Path as identifiers, and the last error just ties in with the first. You should learn at least a little C++ before trying out code and asking about errors.