|
J_E_D_I wrote: Where are the they?
Directly below the orange bar at the top of your browser window is a edit field labeled "Search". Entering "registry C++" there yields a whole lot of code snippets showing you how to do it. There are even beginners tutorials...
J_E_D_I wrote: I am new to the community
You must be new to this world if you never have heard about search facilities on Websites.
Sorry for being so blunt, but that is no rocket science, you know?
Tampering with the registry, on the other hand, is.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal. George Orwell, "Keep the Aspidistra Flying", Opening words
|
|
|
|
|
There's stuff on CP, and this might help too:
http://vcf.svn.sourceforge.net/viewvc/vcf/trunk/vcf/src/vcf/FoundationKit/Win32Registry.cpp?revision=2805&view=markup
This is a class that wraps the Win32 interface to the registry. The use the "String" class you can consider as a typedef of std::basic_string<unsigned short> (though that's not exactly accurate, it's close enough for this discussion).
Basically the problem you're having is passing an ansi string to a wide string function.
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
VCF Blog
|
|
|
|
|
There IS a shorter way of doing that...
#include <windows.h>
HKEY temporarykey;
CString value;
DWORD lpcbData = 64;
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\XXX\License",0,KEY_QUERY_VALUE,&temporarykey);
RegQueryValueEx(temporarykey,"License",NULL,NULL,(unsigned char*)value.GetBuffer(0),&lpcbData);
RegCloseKey(temporarykey);
It looks a lot simpler than what you are doing
|
|
|
|
|
Hi there, thanks for your advice. I've tried your code but it gives me a series of errors. What am I doing wrong? Many thanks.
error C2065: 'CString' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'value'
error C2065: 'value' : undeclared identifier
error C2664: 'RegOpenKeyExW' : cannot convert parameter 2 from 'const char [21]' to 'LPCWSTR'
error C2228: left of '.GetBuffer' must have class/struct/union
error C2664: 'RegQueryValueExW' : cannot convert parameter 2 from 'const char [8]' to 'LPCWSTR'
|
|
|
|
|
For errors 4 and 6, put enclose the 2nd parameter in _T(). That should help. And as for CString, I forgot you're using C++, sorry. CString is in MFC. What you need to store the value is a buffer, so I think you can use char* or just std::string.
Read this: http://www.codeguru.com/forum/showthread.php?t=231164[^]
|
|
|
|
|
Thanks, now it ALMOST works! The code (reported below) however still returns one error (error C2228: left of '.GetBuffer' must have class/struct/union). Final advice please!
#include "stdafx.h"
#include <string>
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
HKEY temporarykey;
unsigned char* value;
DWORD lpcbData = 64;
RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\XXX\\License"),0,KEY_QUERY_VALUE,&temporarykey);
RegQueryValueEx(temporarykey,_T("License"),NULL,NULL,(unsigned char*)value.GetBuffer(0),&lpcbData);
RegCloseKey(temporarykey);
return 0;
}
|
|
|
|
|
Try removing the .GetBuffer(0)
|
|
|
|
|
It results in Run-Time Check Failure #3 - The variable "value" in being used without being defined.
|
|
|
|
|
Hmm...well, you'll need to know how long your registry value is when you define the char*...if you don't, have you tried using string value ? I believe there's something like GetBuffer in the string class.
Oh yes, I just looked at the topic below this one and it's pretty relevant to your problem here.
|
|
|
|
|
Thanks for the advice. Now it doesn't return any error but it doesn't show the value of the registry key...
Thanks
#include "stdafx.h"
#include <string>
#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HKEY temporarykey;
char* value;
value = (char *)malloc(31); //the value of the key has 31 characters
DWORD lpcbData = 64;
RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\XXX\\License"),0,KEY_QUERY_VALUE,&temporarykey);
RegQueryValueEx(temporarykey,_T("License"),NULL,NULL,(unsigned char*)value,&lpcbData);
RegCloseKey(temporarykey);
char i;
cin >> i;
return 0;
}
|
|
|
|
|
You've got me stumped there...I'm not quite sure why it doesn't show the value, but here are a few suggestions that might help: instead of malloc(31), why don't you just do char* value=new char[31] ? And check if the registry key exists first. I'd also recommend using RegCreateKeyEx() instead of RegOpenKeyEx(). CreateKey creates the key if it doesn't exist and opens the key if it does. That way, you can prevent an error from opening a non-existant key from happening.
For me, the code you have works fine when I try to use it to read a value from the registry...What do you mean by it doesn't "show" the value? You debugged it and when you got past the RegQueryValueEx function the value of value didn't change?
|
|
|
|
|
Using RegCreateKeyEx() rather than ..OpenKey.. seems to be a great advice and I will certainly try it.
I was incorrect, the code does actually show a value but since my key value is a long hexadecimal number rather than a single char, using char just returns the first digit of such value, whereas I would need it all. I've tried to use string rather than char but I am clearly making some syntax mistakes since it returns an error... Please help me, I feel we are close to the solution!! Thanks
|
|
|
|
|
I just tested out your code, it returns a hexadecimal value of type REG_SZ with 31 characters fine. You allocated 31 bytes to your char* value, so now value can store 31 chars. What type is your value? Assuming it's a REG_SZ (string), it should work fine. Don't bother changing to string, char* works fine.
|
|
|
|
|
Correct, the value is type REG_SZ. I've tried to make the code read values of different lengths but when I do
cout << value;
it always shows on the console only the 1st character of the value! It's driving me crazy!
|
|
|
|
|
What is the value of your value? It works fine for me. I put a random hexadecimal number in the registry with 31 chars and it reads and outputs fine...
Edit: BTW, you should really free(value) . It's a good habit. Free what you allocate. Also, try null-terminating your char*: allocate 32 chars instead and set value[31] to \0.
-- modified at 12:11 Thursday 30th August, 2007
|
|
|
|
|
My values is A86B88365187E92EAC995E6F6F08763
free(value) unfortunately doesn't solve the problem.
Could you please elaborate on what you mean by "try null-terminating your char*: allocate 32 chars instead and set value[31] to \0".
|
|
|
|
|
You have an array of 31 chars right now. What I mean is you should null-terminate your char* by allocating 32 chars to your char* value , and then after retrieving the value, set value[31]=0;
Are you doing something like this right now?:
HKEY temporarykey;
char* value;
value = (char*)malloc(32);
DWORD lpcbData = 32;
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\test",0,KEY_QUERY_VALUE,&temporarykey);
RegQueryValueEx(temporarykey,"License",NULL,NULL,(unsigned char*)value,&lpcbData);
RegCloseKey(temporarykey);
value[31]=0;
cout<<value;
free(value);
For me, that chunk of code works fine. I am a bit puzzled on why you set lpcdData to 64 at the start...you only need 32.
|
|
|
|
|
If have tried your code but it results in 2 errors:
error C2664: 'RegOpenKeyExW' : cannot convert parameter 2 from 'const char [14]' to 'LPCWSTR'
error C2664: 'RegQueryValueExW' : cannot convert parameter 2 from 'const char [8]' to 'LPCWSTR'
That's the complete code:
#include "stdafx.h"<br />
#include <string><br />
#include <windows.h><br />
#include <iostream><br />
using namespace std;<br />
<br />
int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
HKEY temporarykey;<br />
char* value;<br />
value = (char*)malloc(32);
DWORD lpcbData = 32;<br />
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\xxx",0,KEY_QUERY_VALUE,&temporarykey);<br />
RegQueryValueEx(temporarykey,"License",NULL,NULL,(unsigned char*)value,&lpcbData);<br />
RegCloseKey(temporarykey);<br />
value[31]=0;<br />
cout<<value;<br />
free(value);<br />
char i;<br />
cin >> i;<br />
return 0;<br />
}
Sorry for being such a pest! I owe you at least a beer for your efforts. Where are you based by the way?
|
|
|
|
|
I'm sorry, I forgot to put the _T() macro in. For some reason, my compiler (VC++ 6.0) accepts a string for LPCWSTR. Just replace "SOFTWARE\\xxx" with _T("SOFTWARE\\xxx") and "License" with _T("License") to fix that problem. If it STILL doesn't show the correct value, I'm stumped.
BTW, I know how annoying it is when the reg functions just won't do what you want them to do. I've had problems like this before. (Although I've never seen something like this...). And you're not, surprisingly, being much of a pest. I've had people ask me why the compiler doesn't let them use variables, and I told them to define the variables first, but they say that that's not the problem when each and every one of their "variables" are undefined. **sigh**. I hope I never meet programmers like that again.
|
|
|
|
|
Getting closer... Now no errors but it displays a sequence of 31 ===============================
whereas the key value is a hexadecimal number. Also tried to change the value to decimal numbers but it display the same sequence. Unsolvable mysteries of the registry keys....
|
|
|
|
|
Try changing lpcdData to a larger value. Or just change it back to 64, which is what you started with.
|
|
|
|
|
Nope. Changing the value slighlty outputs the same weird = signs. Increasing the value to 64 triggers a breakpoint in the executable. Arghhhhh!
|
|
|
|
|
No luck. I always get an error. I am starting wondering whether the fact that it is not working on my pc is becasue me and you are using different compilers... I am using Visual Studio 2005. Could that be the reason?
|
|
|
|
|
Well, I'm using Visual Studio 6.0. Isn't that older than 2005? I assume that all the functions in VC++ 6.0 would be incorporated into VC++ 2005...Is there any place where you can get a trial version of 2005?
|
|
|
|
|
How to initialize a string.. so that it can hold any size..
char str[100] i dont want to do this way...str[] should be able to support as many characters as input by user..
|
|
|
|