Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++ registry string value


  • Please log in to reply
45 replies to this topic

#1 BaTTy55

BaTTy55

  • Members
  • 36 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:56 AM

Posted 28 February 2009 - 10:14 AM

can anyone tell me how to set a string value in c++??
i need to set one in = HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
someone help?

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:06:56 AM

Posted 28 February 2009 - 10:22 AM

Hello :flowers:

Why are you writing in there :thumbsup:

See RegOpenKeyEx:
http://msdn.microsoft.com/en-us/library/ms724897(VS.85).aspx

And RegSetValueEx:
http://msdn.microsoft.com/en-us/library/ms724923(VS.85).aspx

And don't forget to RegCloseKey() when you're done with it. :trumpet:
http://msdn.microsoft.com/en-us/library/ms724837(VS.85).aspx

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:07:56 AM

Posted 28 February 2009 - 10:36 AM

And of course before actually testing your app, backup your registry.

#4 BaTTy55

BaTTy55
  • Topic Starter

  • Members
  • 36 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:56 AM

Posted 28 February 2009 - 10:41 AM

i can writeing there becuse i want my app to run on startup, so i dont have to click on it everytime i reboot

#5 BaTTy55

BaTTy55
  • Topic Starter

  • Members
  • 36 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:56 AM

Posted 28 February 2009 - 10:42 AM

anything i need to include?
#include<winreg.h>
anything other than that?

#6 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:06:56 AM

Posted 28 February 2009 - 10:46 AM

Actually, you should probably #include <windows.h> instead.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#7 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:07:56 AM

Posted 28 February 2009 - 11:02 AM

i can writeing there becuse i want my app to run on startup, so i dont have to click on it everytime i reboot


Is there any reason then why you just wouldn't add it to your startup folder?

#8 BaTTy55

BaTTy55
  • Topic Starter

  • Members
  • 36 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:56 AM

Posted 28 February 2009 - 11:03 AM

compile errors galore...

#include <string>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <shlobj.h>




LONG WINAPI RegOpenKeyEx(
__in HKEY hKey,
__in_opt LPCTSTR lpSubKey,
__reserved DWORD ulOptions,
__in REGSAM samDesired,
__out PHKEY phkResult
);
LONG WINAPI RegSetValueEx(
__in HKEY hKey,
__in_opt LPCTSTR lpValueName,
__reserved DWORD Reserved,
__in DWORD dwType,
__in_opt const BYTE *lpData,
__in DWORD cbData
);
LONG WINAPI RegCloseKey(
__in HKEY hKey
);

void setReg(){
RegOpenKeyEx(HKEY_CURRENT_USER ,"Software\\Microsoft\\Windows\\CurrentVersion\\Run" , 0 , KEY_SET_VALUE , &hKey );
RegSetValueEx(hKey , "startup" , 0 , REG_SZ ,"C:\sApp.exe" );
RegCloseKey();
}

int main (){
return 0;
}

i must seem like an idoit..

#9 BaTTy55

BaTTy55
  • Topic Starter

  • Members
  • 36 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:56 AM

Posted 28 February 2009 - 11:08 AM

on anyother computer the startup folder would work but on my laptop
it wonk work for some reson..

#10 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:06:56 AM

Posted 28 February 2009 - 12:04 PM

LONG WINAPI RegOpenKeyEx(
__in HKEY hKey,
__in_opt LPCTSTR lpSubKey,
__reserved DWORD ulOptions,
__in REGSAM samDesired,
__out PHKEY phkResult
);
LONG WINAPI RegSetValueEx(
__in HKEY hKey,
__in_opt LPCTSTR lpValueName,
__reserved DWORD Reserved,
__in DWORD dwType,
__in_opt const BYTE *lpData,
__in DWORD cbData
);
LONG WINAPI RegCloseKey(
__in HKEY hKey
)


Don't redeclare these. They are forward-declared in Windows.h.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#11 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:08:56 AM

Posted 28 February 2009 - 02:19 PM

You are using both <stdio.h> and <iostream> ? Its certainly a mess up

All you have to include is <windows.h>, for Registry API. ALso link with advapi32.lib when compiling and linking.

RegOpenKeyEx returns a value ERROR_SUCCESS if all goes well and registry key is opened. Its a good idea to use this value before proceeding further.

if(RegOpenKeyEx(....)==ERROR_SUCCESS){
//Do your work
//close key
}

#12 BaTTy55

BaTTy55
  • Topic Starter

  • Members
  • 36 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:56 AM

Posted 28 February 2009 - 02:56 PM

ok i will try this then

#13 BaTTy55

BaTTy55
  • Topic Starter

  • Members
  • 36 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:56 AM

Posted 28 February 2009 - 02:59 PM

#include <windows.h>

void setReg(){
RegOpenKeyEx(HKEY_CURRENT_USER ,"Software\\Microsoft\\Windows\\CurrentVersion\\Run" , 0 , KEY_SET_VALUE , &hKey );
if(RegOpenKeyEx(HKEY_CURRENT_USER ,"Software\\Microsoft\\Windows\\CurrentVersion\\Run" , 0 , KEY_SET_VALUE , &hKey )==ERROR_SUCCESS){
RegSetValueEx(hKey , "startup" , 0 , REG_SZ ,"C:\sApp.exe" );
RegCloseKey();
}
}

int main (){
return 0;
}

am i doing it right?

#14 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:06:56 AM

Posted 28 February 2009 - 04:23 PM

Remove the first call to regopenkeyex....

Also please try to use the codebox... otherwise the forum software destroys tabs :thumbsup:
[ code] and [ /code], looks like
This is some code with			   space
Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#15 BaTTy55

BaTTy55
  • Topic Starter

  • Members
  • 36 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:56 AM

Posted 28 February 2009 - 05:38 PM

#include <windows.h>

void setReg(){
if(RegOpenKeyEx(HKEY_CURRENT_USER ,"Software\\Microsoft\\Windows\\CurrentVersion\\Run" , 0 , KEY_SET_VALUE , &hKey )==ERROR_SUCCESS){
RegSetValueEx(hKey , "startup" , 0 , REG_SZ ,"C:\sApp.exe" );
RegCloseKey();
} 
}

int main (){
	return 0;
}

still shows a compile error "hkey undeclared(first use this function)"
and a warrning "unknown escape sequence /s "

it looks fine to me..




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users