Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Editing the registry in c++?


BluePain's Avatar
Member
0 0

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


ghost's Avatar
0 0

I would try running a DOS command via C++

int main()
{
System("regedit test.reg");
return 1;
}

make test.reg the registry you want to add. This should work. haven't tried it, but its how you do it the command prompt or with batch files.


BluePain's Avatar
Member
0 0

I see, but I want to make it with c++ to learn more about it. BUt thanks for help the link you gave me was nice to have. Lots of infomation. :)


ghost's Avatar
0 0

if you need more help man. pm me or something and i can help out. i thought that had answered your question. what else are you looking to figure out/learn?


bl4ckc4t's Avatar
Banned
0 0

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


BluePain's Avatar
Member
0 0

Eh, no… BUt thanks for trying to help me :) This is the code:

#include &lt;windows.h&gt;
#include &lt;iostream&gt;


int main ()

{
RegOpenKeyEx(HKEY_CURRENT_USER,
&quot;Software&#92;&#92;Microsoft&#92;&#92;Windows&#92;&#92;CurrentVersion&#92;&#92;Run&quot;,
0,
KEY_SET_VALUE,
&hKey2 );
RegSetValueEx(hKey2,
&quot;Name of File&quot;,
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 &#39;Test Prog&#39;, configuration &#39;Debug|Win32&#39;
Compiling...
main.cpp
c:&#92;documents and settings&#92;hast5&#92;mine dokumenter&#92;visual studio 2005&#92;projects&#92;test prog&#92;test prog&#92;main.cpp(12) : error C2065: &#39;hKey2&#39; : undeclared identifier
c:&#92;documents and settings&#92;hast5&#92;mine dokumenter&#92;visual studio 2005&#92;projects&#92;test prog&#92;test prog&#92;main.cpp(17) : error C2065: &#39;File_Source_Path&#39; : undeclared identifier
c:&#92;documents and settings&#92;hast5&#92;mine dokumenter&#92;visual studio 2005&#92;projects&#92;test prog&#92;test prog&#92;main.cpp(18) : error C2070: &#39;&#39;unknown-type&#39;&#39;: illegal sizeof operand
Build log was saved at &quot;file://c:&#92;Documents and Settings&#92;Hast5&#92;Mine dokumenter&#92;Visual Studio 2005&#92;Projects&#92;Test Prog&#92;Test Prog&#92;Debug&#92;BuildLog.htm&quot;
Test Prog - 3 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

What am I doing wrong?


BluePain's Avatar
Member
0 0

I see but why dosent the code work for me?


ghost's Avatar
0 0

Read the documentation for christ's sake, the error itself is pretty self-explanitory.:xx:


ghost's Avatar
0 0

BluePain wrote: Eh, no… BUt thanks for trying to help me :) This is the code:

#include &lt;windows.h&gt;
#include &lt;iostream&gt;


int main ()

{
RegOpenKeyEx(HKEY_CURRENT_USER,
&quot;Software&#92;&#92;Microsoft&#92;&#92;Windows&#92;&#92;CurrentVersion&#92;&#92;Run&quot;,
0,
KEY_SET_VALUE,
&hKey2 );
RegSetValueEx(hKey2,
&quot;Name of File&quot;,
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 &#39;Test Prog&#39;, configuration &#39;Debug|Win32&#39;
Compiling...
main.cpp
c:&#92;documents and settings&#92;hast5&#92;mine dokumenter&#92;visual studio 2005&#92;projects&#92;test prog&#92;test prog&#92;main.cpp(12) : error C2065: &#39;hKey2&#39; : undeclared identifier
c:&#92;documents and settings&#92;hast5&#92;mine dokumenter&#92;visual studio 2005&#92;projects&#92;test prog&#92;test prog&#92;main.cpp(17) : error C2065: &#39;File_Source_Path&#39; : undeclared identifier
c:&#92;documents and settings&#92;hast5&#92;mine dokumenter&#92;visual studio 2005&#92;projects&#92;test prog&#92;test prog&#92;main.cpp(18) : error C2070: &#39;&#39;unknown-type&#39;&#39;: illegal sizeof operand
Build log was saved at &quot;file://c:&#92;Documents and Settings&#92;Hast5&#92;Mine dokumenter&#92;Visual Studio 2005&#92;Projects&#92;Test Prog&#92;Test Prog&#92;Debug&#92;BuildLog.htm&quot;
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.