-
Working with registry
Hey guys,
I'm new here and I'm pretty new to c++ too. I was searching on internet about registry and C++ but I couldn't find anything good. Everything was bad written and got lots of errors and there was nothing explained in there.
I'm trying to figure out how could I make a program which can read binary file registry and store it somewhere in program and show it.
Please help me cause I can't find it anywhere!
-
Re: Working with registry
Have you tried plain win32 API? I know is hard to get this to work for the first time, but i'm pretty sure you'll get a best result if you write your own code.
Check the MSDN (or the VS docs if you have them installed) for RegOpenKeyEx, this is your key to the windows registry and there are some samples on these docs. If you need further assistance, please reply.
-
Re: Working with registry
Quote:
Originally Posted by
bioHzrdmX
Have you tried plain win32 API?
.
I read somewhere about it but I couldn't get it work.
Check the MSDN (or the VS docs if you have them installed) for RegOpenKeyEx, this is your key to
Quote:
Originally Posted by
bioHzrdmX
Check the MSDN (or the VS docs if you have them installed) for RegOpenKeyEx, this is your key to
.
Can you please explain me how can I check if i got this installated?
I have Microsoft Visual C++ 2010 as compiler.
thanks
-
Re: Working with registry
"Check the MSDN (or the VS docs if you have them installed) for RegOpenKeyEx, this is your key to "
sorry its my mistake while i was copy/pasting, forgot to erase it
-
Re: Working with registry
This should help you understand
This is for creating a registery key with REG_SZ value.
Code:
HKEY hKey;
DWORD buffersize = 8000;
char* lpData = new char [buffersize];
LPDWORD lpdwDisp = &buffersize;
LONG i = RegCreateKeyEx( HKEY_CURRENT_USER,"Folder Name", 0,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,lpdwDisp);
LPCSTR abv = "Value";
if(i == ERROR_SUCCESS)
{
RegSetValueEx(hKey,"Reg Key Name", 0, REG_SZ,LPBYTE (abv),sizeof(abv) );
}
return 0;
}
This is for deleting a reg key
Code:
int _tmain(int argc, _TCHAR* argv[])
{
RegDeleteKey(HKEY_CURRENT_USER,"Folder");//*Deletes entire folder with key in it
return 0;
}
This is for querying a reg key
Code:
HKEY hKey;
DWORD buffersize = 1024;
char* lpData = new char [buffersize];
if(RegOpenKeyEx(HKEY_CURRENT_USER,"Folder",NULL,KEY_READ,&hKey) ==ERROR_SUCCESS)
{
RegQueryValueEx(hKey,"Folder",0,0,(LPBYTE) lpData,&buffersize);
RegCloseKey(hKey);
}
-
Re: Working with registry
To check whether you have MSDN installed or not, simply hit F1 from inside the VS IDE, the help system will popup, just go to the Index tab and write RegOpenKeyEx, if the entry is found within the index, then you have access to the MSDN (it may be online or local). That's all you need to get started with Win32 programming.
Now let me explain something about API: If you want to make cool apps that make use of windows-specific technologies (registry, directx, etc) you must learn Win32 API. That is just a bunch of functions that the OS provides to let developers interact with it.
So, when it comes to registry, you must use the OS routines to interact with him.
Please tell me if you want some example code, but first, please tell me how skilled you are in C++, Win32 API and if you are using MFC/ATL or just plain C++.
-
Re: Working with registry
Quote:
Originally Posted by
bioHzrdmX
To check whether you have MSDN installed or not, simply hit F1 from inside the VS IDE, the help system will popup, just go to the Index tab and write RegOpenKeyEx, if the entry is found within the index, then you have access to the MSDN (it may be online or local). That's all you need to get started with Win32 programming.
Now let me explain something about API: If you want to make cool apps that make use of windows-specific technologies (registry, directx, etc) you must learn Win32 API. That is just a bunch of functions that the OS provides to let developers interact with it.
So, when it comes to registry, you must use the OS routines to interact with him.
Please tell me if you want some example code, but first, please tell me how skilled you are in C++, Win32 API and if you are using MFC/ATL or just plain C++.
Well i found this MSDN and I got lot of informations about RegOpenKeyEx, thanks for that.
Well it would be very nice if you could post some example code cause I learn it best from examples.
Im not skilled in C++, I started learning about a week ago and I've been throughout a lot of tutorials, I mean a LOT. I decided to skip a bit to the registry cause I got and idea to work something with it but all basic stuff doesen't have much in common.
And I'm not skilled in Win32 API, I don't know anything about it.
Also can you pelase tell me which header files should i load to get all this working?
-
Re: Working with registry
You can Search google for tutorials
-
Re: Working with registry
-
Re: Working with registry
As I said, I did but there was like 3 kinds of tutorials and every one had different code and everything was bad explained, they even got errors in their code...
-
Re: Working with registry
Everytime you want to get something from windows, you should add <windows.h> as it automatically adds all the headers from the sdk folder (installed with the vc++ compiler).
For registry functions to get linked, you must add the Advapi32.lib library in project settings -> linker -> aditional dependencies or by using the #pragma comment directive:
Code:
#pragma comment(lib, "Advapi32.lib.")
The code posted is a good start, just check the documentation and you'll be ready to play with API, if you have more questions, post them :-)
Regards.
-
Re: Working with registry
Okay, thanks a lot for making things clear to me!
Cheers.
-
Re: Working with registry
Just an small example, this will retrieve your windows product name from the registry, should work with XP/Vista/7, just check how you must open the key and then read the value you want, i don't know if you're using Unicode, but this should compile without it.
Code:
BOOL bGetBinary = FALSE;
HKEY hKey = NULL;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), NULL, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
DWORD dwType = 0, dwBufferSize = 0;
if (!bGetBinary)
{
dwBufferSize = 260;
TCHAR tData[260];
if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, &dwType, (LPBYTE)&tData, &dwBufferSize) == ERROR_SUCCESS)
{
MessageBox(tData);
}
else
MessageBox(TEXT("Error!"));
}
else
{
dwBufferSize = 1024;
BYTE bBuffer[1024];
if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, &dwType, (LPBYTE)&bBuffer, &dwBufferSize) == ERROR_SUCCESS)
{
// Do what you want with the raw data here :-)
}
else
MessageBox(TEXT("Error!"));
}
}
Just paste it inside your main procedure and remember the #include <windows.h> and the #pragma comment(lib, "Advapi32.lib.") line before the include.
-
Re: Working with registry
I tried this code, but I'm getting an error.
Code:
LONG i = RegCreateKeyEx( HKEY_CURRENT_USER,"Folder name", 0,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,lpdwDisp);
At this point where i need to put folder name i got this error. I tried to locate my folder multiple times but Im always getting this.
1 IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
-
Re: Working with registry
It's because you're using Unicode:
Windows has two ways of managing characters, the simple, common form (char type) and another, more 'advanced' that supports foreign-language symbols, that used TWO bytes per character.
Code:
LONG i = RegCreateKeyEx( HKEY_CURRENT_USER,"Folder name", 0,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,lpdwDisp);
Here you're using chars, but in order for the compiler to use the WCHAR type (wide-char) you must use a macro:
Code:
LONG i = RegCreateKeyEx( HKEY_CURRENT_USER,TEXT("Folder name"), 0,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,lpdwDisp);
That will work in Unicode builds converting your string to WCHAR-string and in normal builds leaving it untouched (just chars).
About the Intellisense thing: LPCWSTR is an long-pointer to constant WIDE string, a type windows uses to pass strings, is the same as *WCHAR, the Unicode equivalent to *char and LPCSTR.
Hope this helps you.
-
Re: Working with registry
Thanks again, now i tried to put your code into my and everything seems to be fine expect few little errors which i can't make right =(.
Here is my whole code:
Code:
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#pragma comment(lib, "Advapi32.lib.")
using namespace std;
int main ()
{
BOOL bGetBinary = FALSE;
HKEY hKey = NULL;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), NULL, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
DWORD dwType = 0, dwBufferSize = 0;
if (!bGetBinary)
{
dwBufferSize = 260;
TCHAR tData[260];
if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, &dwType, (LPBYTE)&tData, &dwBufferSize) == ERROR_SUCCESS)
{
MessageBox(tData);
}
else
MessageBox(TEXT("Error!"));
}
else
{
dwBufferSize = 1024;
BYTE bBuffer[1024];
if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, &dwType, (LPBYTE)&bBuffer, &dwBufferSize) == ERROR_SUCCESS)
{
}
else
MessageBox(TEXT("Error!"));
}
}
I got errors here:
1 IntelliSense: argument of type "TCHAR *" is incompatible with parameter of type "HWND"
Code:
MessageBox(TEXT("Error!"));
3 IntelliSense: argument of type "const wchar_t *" is incompatible with parameter of type "HWND"
Code:
MessageBox(TEXT("Error!"));
5 IntelliSense: argument of type "const wchar_t *" is incompatible with parameter of type "HWND"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I got following error for all of this lines:
2 IntelliSense: too few arguments in function call
Also 1 more question, after program opens registry value how can I make the program write it's value on the screen? I tried cin << ProductName; but it won't work.
PS. program worked after i erased those messagebox, as it was erased so were the errors.
Code:
dwBufferSize = 1024;
BYTE bBuffer[1024];
if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, &dwType, (LPBYTE)&bBuffer, &dwBufferSize) == ERROR_SUCCESS)
{
cout << ProductName;
}
I'm really sorry for being such a pain in the *** but I really don't understand what went wrong now. =(
Any conclusion?
Thanks
-
Re: Working with registry
-
Re: Working with registry
Sorry, i forgot the HWND parameter, i wrote that code on an MFC app so it just needed the string parameter (as Dadidum said).
To get the code working just change:
Code:
MessageBox(tData);
MessageBox(TEXT("Error!"));
to
Code:
MessageBox(NULL, tData);
MessageBox(NULL, TEXT("Error!"));
The NULL instead of a window handle (HWND-type parameter, a long-type value) is because MessageBox requires an owner window, and since you don't have any, you can pass a NULL value. Modify the other MessageBox'es and you're done.
If you want to display the value on the screen by using std::cout then you should use the buffer where the values are stored, in this case, the tData variable of the first RegQueryValue:
Code:
dwBufferSize = 260;
TCHAR tData[260];
if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, &dwType, (LPBYTE)&tData, &dwBufferSize) == ERROR_SUCCESS)
{
wcout << tData;
}
else
wcout << TEXT("Error!");
The second RegQueryValue gets the raw binary data from the registry in a BYTE array (BYTE equals to unsigned char for windows) and i only included that because you said you wanted to get some binary data, but the info we're getting from the registry in this example is a REG_SZ so the first options is a better choice since it gets the string directly. Play with the code, and again, check the msdn docs (you may use your VS help or try at www.msdn.com) because windows uses a lot of strange data types that you should know about, if you have more questions, please let us know.
And another advice: i used wcout instead of cout because your'e using Unicode, the 'w' before some functions means that it is the 'wide-char' version of the function. Check this post for more advice on cout and wcout http://www.codeguru.com/forum/showthread.php?t=500451
And don't worry, this forum was made to allow us to share our knowledge with others, sometimes we need a little help, sometimes we can give a little help :-)
-
Re: Working with registry
Thanks alot mate, you really helped me!!!
I made it work finally. Btw I tried to send you an private message but you got disabled private messaging. =(
Only thing that confuses me is that "unicode". I guess I'm too new to c++ to understand it right now atm, so im going to study more basics :D
My FINAL questiond is; xD
Is it possible to send that value (tData) to my website?
-
Re: Working with registry
Hi, sorry about the PM thing, i just forgot to turn it on :-)
Unicode may be a bit confussing for some people (it was for me) but once you learn how to interact with it, you'll be ready to work with Unicode and non-Unicode apps.
It is not more than a type-change: instead of char you'll be using WCHAR, you'll need some new functions (strcpy, printf, etc. will not longer work for you) that are included with the VC++ compiler, the documentation is your best friend. I will also suggest you to read a bit on Windows data types, to learn what is an HWND, an LPTSTR, a DWORD, etc. If you're gonna programming for windows, this is a must. Try to play with API, play a sound, show a message box, create a window, and you'll be above the rest of the coders in a couple of months. I'm a third-year university student and none of my classmates know how to create a window! Imagine the great work opportunities they'll miss!
About your final question: yep, it is possible, the easiest way (i think) is using an API call to launch the browser (ShellExecute) with some parameters on the address (you know something like 'http://www.mysite.com/receive.php?data=something' where 'something' is the value of tData 'printed-out' to the string) but this may be a bit tricky and you will need to write the 'receive.php' code to store or show the value on the web page (requirements: php knowledge and php support on your host) so i think you should try to get better programming skills before trying that :-)
So if you want to become a great coder, you must make a little research, but before trying to run tutorial code, you must learn the essentials about windows, its API and improve your C++ skills, because once you've learned API you will want to make the move to MFC.
Keep the hard work!!
Regards.
-
Re: Working with registry
Just to give you help RegOpenKeyEx does not work in XP
-
Re: Working with registry
Quote:
Originally Posted by
gaar321
Just to give you help RegOpenKeyEx does not work in XP
Are you sure? :confused:
-
Re: Working with registry
Lots of old timers prefer the use the brute force method of reading and writing the registry. Me, I'm always looking for the easier approach, yet one that still gets the work done in a reliable and robust manner.
So consider using the ATL CRegKey class. All you need to do for this code to work is #include <atlbase.h>. No other libs or dlls are required.
Here's how easy it is...
Code:
CString sProductName;
ULONG ulChars = MAX_PATH + 1;
CRegKey rk;
rk.Open( HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), KEY_READ );
rk.QueryStringValue( _T("ProductName"), sProductName.GetBuffer( MAX_PATH ), &ulChars );
sProductName.ReleaseBuffer( );
In terms of reading the registry, a good general rule is to only read the registry if the data isn't available via an api.
You can retrieve the ProductName from the GetVersionEx api. No need to read the registry at all for this info. In fact, depending on the OS, the ProductName value may not be present.
-
Re: Working with registry
Thanks for making me know that but could you please explain me how can I make this work:
#include <atlbase.h>
I put that in my code and i got error that it could not get opened.
-
Re: Working with registry
Quote:
Originally Posted by
harkoslav
Thanks for making me know that but could you please explain me how can I make this work:
#include <atlbase.h>
I put that in my code and i got error that it could not get opened.
What version of Visual C++ are you using?
-
Re: Working with registry
-
Re: Working with registry
Quote:
Originally Posted by
harkoslav
Visual Studio C++ 2010
What edition?
-
Re: Working with registry
Microsoft Visual Studio C++ 2010 Express
Version 10.0.30319.1 RTMRel
-
Re: Working with registry
AFIK C++ 2010 Express doesn't contain ATL/MFC. :sick:
-
Re: Working with registry
Is there a way to upgrade it or something?
-
Re: Working with registry
Can you please tell me some compiler which has that?
thanks
-
Re: Working with registry
Quote:
Originally Posted by
harkoslav
Can you please tell me some compiler which has that?
thanks
If 'has that' means ATL, then any non-version of Visual C++ includes ATL (and MFC).
-
Re: Working with registry
Quote:
Originally Posted by
harkoslav
Is there a way to upgrade it or something?
Since the express versions are free, there isn't an upgrade path per se.
If you have an older non-express version (say VC2005 Professional for example), then you could upgrade it to a 2010 pro version.
Do a search on Microsoft.com for Visual C++ pricing.