|
What I've got in my code is:
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\XXX\\License",0,
KEY_QUERY_VALUE ,&temporarykey);
RegQueryValueEx(temporarykey,"License", NULL, NULL,
(LPBYTE)License, &lpcbData);
Where should I add _T(SOFTWARE\\XXX\\License")
and _T("License")?
Sorry to be a pain!
|
|
|
|
|
Where you now have "SOFTWARE\\XXX\\License" and "License", you should have _T(SOFTWARE\\XXX\\License") and _T("License").
Do you notice the difference with "_T" and the round braces?
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
|
|
|
|
|
Bingo. Now it doesn't return any error anymore but it doesn't seem to visualize the value of the registry key correctly. It should be 1234 but it shows 0012FED4. Why??
HKEY temporarykey;
TCHAR License[64];
DWORD lpcbData = 64;
RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\XXX\\License"),0,
KEY_QUERY_VALUE ,&temporarykey);
RegQueryValueEx(temporarykey,_T("License"), NULL, NULL,
(LPBYTE)License, &lpcbData);
cout << License;
RegCloseKey(temporarykey);
|
|
|
|
|
Try
...
DWORD dwType;
if( !(RegQueryValueEx(temporarykey,_T("License"), NULL, &dwType, (LPBYTE)License, sizeof(License)/sizeof(License[0])) == Error_SUCCESS)
{
AfxMessageBox("Error!")
}
...
(As you see, I like to determine the sizes where they are needed.)
And look what dwType is. If its REG_SZ, be sure to read the RegQueryValueEx entry in the MSDN. It tells you how to handle String queries.
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{
}
|
|
|
|
|
That's correct, it's a REG_SZ type.
I had a look at: http://msdn2.microsoft.com/en-us/library/ms724911.aspx#Mtps_DropDownFilterText
and it looks like I've got to make LPBYTE lpData non-zero but I'm not sure if I'm right and I don't know how to do it..Help!
Thank you for your help, I really appreciate it.
Ed
|
|
|
|
|
It would be something along this lines:
HKEY temporarykey;
if( !::RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\XXX\\License"), 0, KEY_QUERY_VALUE ,&temporarykey) == ERROR_SUCCESS)
{
std::cout << "Prblm" << std::endl;
}
DWORD dwType = 0;
DWORD dwDataSize = 0;
LONG result = ::RegQueryValueEx( temporarykey, _T("License"), NULL, &dwType, NULL, &dwDataSize);
if( (ERROR_SUCCESS != result) || (0 == dwDataSize) || (dwType != REG_SZ))
{
}
dwDataSize += 1;
BYTE* pBuffer = new BYTE[dwDataSize];
result = ::RegQueryValueEx( temporarykey, _T("InstallRootSubDir"), NULL, &dwType, pBuffer, &dwDataSize);
if( result != ERROR_SUCCESS)
{
}
You need to add the error decoding (via FormatError() and GetLastError()) yourself. This is extremly complicated but quite well documented in the MSDN.
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
|
|
|
|
|
Thank you for showing me the code. I'll try and let you know. An example is by far the best way for me to understand.
|
|
|
|
|
jhwurmbach wrote: Do you notice the difference with "_T" and the round braces?
It took you three tries to say, "Use the _T() macro for string literals."
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
And you think he would have known what "string literals" are?
I was reluctant to simply show him the code - maybe too reluctant. I have to admit that.
But he must be able to do some know-how transfer anyway.
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
|
|
|
|
|
RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\XXX\\License"),0,
KEY_QUERY_VALUE ,&temporarykey);
RegQueryValueEx(temporarykey,_T("License"), NULL, NULL,
(LPBYTE)License, &lpcbData);
|
|
|
|
|
J_E_D_I wrote: C++...read and create Registry Keys
Have you looked at the classes available for that here on Codeproject?
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
|
|
|
|
|
:(Where are the they? I am new to the community but as far as I can see there is a Message Board and some articles for beginners but I cannot find any section with classes...
|
|
|
|
|
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.
|
|
|
|
|