-
February 16th, 2011, 10:49 AM
#1
Writting to registry
I'm trying to set a registry value with the file path of the program using the following code...
Code:
int main(char *argv,int argc){
HKEY hkey;
DWORD dwDisposition, lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Testing",0,KEY_WRITE,&hkey);
if(lRes != ERROR_SUCCESS){
lRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Testing",0,0,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,0,&hkey,&dwDisposition);
RegSetValueEx(hkey,"YourProgName",0,REG_SZ,(unsigned char*)argv,sizeof(argv));}
else{
cout << GetLastError() << endl;}
return 0;
}
I first thought of passing argv but that doesn't seem to work too well. Also RegSetValueEx's 5th parameter must be REG_SZ
From MSDN..
For string-based types, such as REG_SZ, the string must be null-terminated. With the REG_MULTI_SZ data type, the string must be terminated with two null characters. A backslash must be preceded by another backslash as an escape character. For example, specify "C:\\mydir\\myfile" to store the string "C:\mydir\myfile".
Except I don't know where to begin looking to find a solution that returns a file path like so "C:\\mydir\\file.exe" - Any suggestions? Thanx.
-
February 16th, 2011, 11:16 AM
#2
Re: Writting to registry
This will return the size of a (char *) not the length of the string.
Viggy
-
February 16th, 2011, 11:16 AM
#3
Re: Writting to registry
And, it should be:
Code:
int main (char **argv, int argc)
Viggy
-
February 16th, 2011, 11:58 AM
#4
Re: Writting to registry
Understood, but that still doesn't set a value. Can I have a hint on how I should go about doing this?
-
February 16th, 2011, 12:08 PM
#5
Re: Writting to registry
Too many problems here in your code:
- sizeof(argv) is always in Win32 equal 4 (it is the size of a pointer)
- To write to HKEY_LOCAL_MACHINE this code must be run with Admin rights
- The check (lRes != ERROR_SUCCESS) is not enough (it may be "Access denied" in which case you won't be able to create the subkey either). So you have to check the exact error code (for example to be ERROR_FILE_NOT_FOUND)
- You must not call RegSetValueEx unless RegCreateKeyEx returned ERROR_SUCCESS
- You must call RegCloseKey after the key returned by RegOpenKeyEx or RegCreateKeyEx is no more needed.
Victor Nijegorodov
-
February 16th, 2011, 12:35 PM
#6
Re: Writting to registry
>4. You must not call RegSetValueEx unless RegCreateKeyEx returned ERROR_SUCCESS
Isn't that what I was doing? Also this works fine if I defined a null terminated string like this ..
Code:
char buffer[50]="C:\\mydir\\myfile.exe";
But the idea here is to return the path and pass it in the same format as buffer. That is what I'm trying to figure out.
-
February 16th, 2011, 12:43 PM
#7
Re: Writting to registry
You're not checking the return value of the call to RegCreateKeyEx. Also, have you fixed all the other errors?
Viggy
-
February 16th, 2011, 01:19 PM
#8
Re: Writting to registry
Originally Posted by MrViggy
Also, have you fixed all the other errors?
For the most part.
Code:
int main(char **argv,int argc){
HKEY hkey;
DWORD dwDisposition, lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\MICROSOFT\\Testing",0,KEY_WRITE,&hkey);
if(lRes != ERROR_SUCCESS){
DWORD rRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\MICROSOFT\\Testing",0,0,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,0,&hkey,&dwDisposition);
if(rRes == ERROR_SUCCESS){
RegSetValueEx(hkey,"YourProgName",0,REG_SZ,(unsigned char*)argv,sizeof(argv));
RegCloseKey(hkey);}}
else{
cout << GetLastError() << endl;}
return 0;
}
I'm having trouble understanding argv fully. Should I even be looking into it? What size should I be passing?
-
February 16th, 2011, 01:33 PM
#9
Re: Writting to registry
Originally Posted by dellthinker
For the most part.
No, unfortunately only for the "less" part!
1. You cannot simply cast char** to char* and then hope that the correct text will be passed into RegSetValueEx.
2. sizeof(argv) is still equal 4, not as you would assume to be the length of the text.
3. The check (lRes != ERROR_SUCCESS) after calling RegOpenKeyEx is not enough (it may be "Access denied" in which case you won't be able to create the subkey either). So you have to check the exact error code (for example to be ERROR_FILE_NOT_FOUND)
Victor Nijegorodov
-
February 16th, 2011, 03:38 PM
#10
[solved]
I have found the solution...
Code:
int main(){
HKEY hkey;
DWORD dwDisposition, lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\MICROSOFT\\Testing",0,KEY_WRITE,&hkey);
char path[MAX_PATH];
GetModuleFileName(NULL, path,sizeof(path));
if(lRes == ERROR_FILE_NOT_FOUND){
DWORD rRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\MICROSOFT\\Testing",0,0,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,0,&hkey,&dwDisposition);
if(rRes == ERROR_SUCCESS){
RegSetValueEx(hkey,"YourProgName",0,REG_SZ,(unsigned char*)path,sizeof(path));
RegCloseKey(hkey);}}
else{
cout << GetLastError() << endl;}
return 0;
}
That's about the jist of what I was trying to accomplish.
>1. You cannot simply cast char** to char* and then hope that the correct text will be passed into RegSetValueEx.
I was taking counsel from MrViggy, though it compiled correctly it still didn't run using that method.
-
February 16th, 2011, 03:49 PM
#11
Re: [solved]
Originally Posted by dellthinker
I was taking counsel from MrViggy, though it compiled correctly it still didn't run using that method.
The fact that some wrong written code good compiles never meant, never means and never will mean that this code can "good" run!
Victor Nijegorodov
-
February 17th, 2011, 09:26 AM
#12
Re: Writting to registry
Originally Posted by MrViggy
And, it should be:
Code:
int main (char **argv, int argc)
Viggy
You swapped the arguments.
It should be:
or
Code:
int main(int argc, char* argv[])
which is the same as:
Code:
int main(int argc, char** argv)
-
February 17th, 2011, 10:53 AM
#13
Re: Writting to registry
Whoops! I was going off the first post, and hadn't even realized that!
Thanks!
Viggy
-
February 17th, 2011, 11:16 AM
#14
Re: Writting to registry
Why anyone would want to do registry work using the Win32 api's is beyond me.
Code:
#include <atlbase.h>
CRegKey rk;
if( ERROR_SUCCESS == rk.Open( HKEY_LOCAL_MACHINE, _T("SOFTWARE\\MICROSOFT\\Testing") ) )
{
rk.SetStringValue( _T("YourProgName"), szPath );
}
-
February 17th, 2011, 05:21 PM
#15
Re: Writting to registry
Originally Posted by Arjay
Why anyone would want to do registry work using the Win32 api's is beyond me.
[/CODE]
I guess it's because this is a C++ and WinAPI forum...
Isn't it?
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|