I tried to follow their instructions. The only difference is that my
subKey and values are strings.
I attached the relevant code. I'll really appreciate if some will
explain to me what's the correct way to do that.
Thanks,
Dave.
string sSubKey = GenerateKey(); // Function that return the subkey
string from file
string sValue = GenerateValue(); // Function that return the key's
value string from file
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,TEXT(sSubKey),0,NULL,
0,0,NULL,&hKey,&dwDisp) == ERROR_SUCCESS)
{
....
RegSetValueEx(hKey,TEXT(sValue),0,dwType,(PBYTE)sValue,dwSize);
}
The RegCreateKeyEx raised the following error:
error C2664: 'RegCreateKeyExA': cannot convert parameter 2 from
'std::string' to 'LPCSTR'
The RegSetValueEx raised the following errors:
error C2440: 'type cast': cannot convert from 'std::string' to 'PBYTE'
error C2664: 'RegSetValueEx': cannot convert parameter 2 from
'std::string' to 'LPCSTR'
Do not use the TEXT prefix on variables. Use it only on string
constants like "this" changes to TEXT("this")
To pass a nul terminated C string from a string use sSubKey.c_str()
RegCreateKeyEx(HKEY_LOCAL_MACHINE, sSubKey.c_str(),...
In C++ Windows programming you may find the CString class to be much
more convenient than std::string. For example, CString has an
operator LPCTSTR built in, which would have solved this problem
automatically.
Thanks Scott!!
I wasn't familar with the CString class. I Usually use QT however I
was asked to use only built in class.
is the c_str() function will solve my second type cast error at the
RegSetValueEx? How can I send the sValue to the function?
Thanks again
Dave
>Hi everybody,
>I'm newbie in C++ Registry programming.
>My goal is to write a key and it's values to the registry. I read the
>following example that was posted on MSDN : http://msdn.microsoft.com/en-us/library/ms838625.aspx.
>
>I tried to follow their instructions. The only difference is that my
>subKey and values are strings.
>I attached the relevant code. I'll really appreciate if some will
>explain to me what's the correct way to do that.
>Thanks,
>Dave.
>
>string sSubKey = GenerateKey(); // Function that return the subkey
>string from file
>string sValue = GenerateValue(); // Function that return the key's
>value string from file
>
>if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,TEXT(sSubKey),0,NULL,
>0,0,NULL,&hKey,&dwDisp) == ERROR_SUCCESS)
>{
> ....
> RegSetValueEx(hKey,TEXT(sValue),0,dwType,(PBYTE)sValue,dwSize);
Using a cast with strings can lead to very strange symptoms. In
particular, it is _not_ a reliable way to convert between ASCII and
UNICODE. Also, the definition of the TEXT macro and its relatives
depends on whether UNICODE is defined when you compile. I sense you do
not understand these issues, and thus _strongly_ recommend taking time
to understand UNICODE to avoid wasting a lot of your time tracking
down strange symptoms. You can start by using google
(http://groups.google.com/advanced_search?q=&) to look up
unicode
in this newsgroup.
>}
>
>
>The RegCreateKeyEx raised the following error:
>error C2664: 'RegCreateKeyExA': cannot convert parameter 2 from
>'std::string' to 'LPCSTR'
>
>
>The RegSetValueEx raised the following errors:
>error C2440: 'type cast': cannot convert from 'std::string' to 'PBYTE'
>error C2664: 'RegSetValueEx': cannot convert parameter 2 from
>'std::string' to 'LPCSTR'
-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).
Robert E. Zaret, eMVP
PenFact, Inc.
20 Park Plaza, Suite 400
Boston, MA 02116
www.penfact.com
Useful reading (be sure to read its disclaimer first):
http://catb.org/~esr/faqs/smart-questions.html
You must use the c_str() function to get a pointer to the chars in a
std::string. In the second case you will need to cast the char* to
PBYTE.
std::string is for char arrays. But Windows is hostile to char arrays
since it is based on wchar_t and the default build environment assumes
wchar_t. As R. Zaret advises, figure this stuff out before going much
further or you'll make a mess.
Hi All,
Thanks! It helped and my code now is compiling. But I still have one
last problem -
I always get error code from RegSetValueEx 5 (as far as i understood
it's access denied).
I tried to write my data to key in HKLM and also HKCU and both raised
the same error. Her's my code, what I doing wrong ?
Thanks a lot
Dave
string sSubKey = GenerateKey(); // Function that return the subkey
string from file
string sValue = GenerateValue(); // Function that return the key's
value string from file
DWORD dwType,dwSize,dwRetVal;
....
dwType = REG_SZ;
dwSize = sizeof(sValue.length() + 1) * sizeof(TCHAR);
if(ERROR_SUCCESS != RegSetValueEx(hKey,sValue.c_str(),0,dwType,(PBYTE)
sValue.c_str(),dwSize))
{
if(dwRetVal == 5)
cout << "Access is denied" << endl;
else
cout << "Error Code: " << dwRetVal << endl;
}
This is wrong. Using sizeof you get the size of the integer operand,
which is usually 4 on 32bit machines. You only need .length() + 1:
dwSize = sValue.length() + 1;
The sizeof(TCHAR) is not needed and bad style, since you know that
sValue is an ANSI string. Don't use TCHAR unless you are operating on
TCHARs.
> if(ERROR_SUCCESS != RegSetValueEx(hKey,sValue.c_str(),0,dwType,(PBYTE)
> sValue.c_str(),dwSize))
You confused sValue and sSubKey here. Change the 2nd parameter of
RegSetValueEx to sSubKey.c_str().
> {
> if(dwRetVal == 5)
> cout << "Access is denied" << endl;
> else
> cout << "Error Code: " << dwRetVal << endl;
> }
To find such bugs, it is a good idea to
1) print the values which you pass to the function
or
2) use your debugger and walk through the code step by step.
--
Thomas
|string sSubKey = GenerateKey(); // Function that return the subkey
|string from file
|string sValue = GenerateValue(); // Function that return the key's
|value string from file
|DWORD dwType,dwSize,dwRetVal;
|
|....
|
|dwType = REG_SZ;
|dwSize = sizeof(sValue.length() + 1) * sizeof(TCHAR);
|
|if(ERROR_SUCCESS != RegSetValueEx(hKey,sValue.c_str(),0,dwType,(PBYTE)
|sValue.c_str(),dwSize))
|{
| if(dwRetVal == 5)
| cout << "Access is denied" << endl;
| else
| cout << "Error Code: " << dwRetVal << endl;
Above, you are not assigning a value to dwRetValue.
--
- Vince
Hi All,
I'll check your suggestions about the sizeof.
about the other 2 notes - I miscopied the code - the first param is
sKey and I did assigned the dwRetVal. Sorry for that.
Do you think that only changing the sizeof will solve my access denied
error?
Is there other permission parameter that I haven't use?
I saw examples that instead RegCreateKeyEx use RegOpenKeyEx and pass a
write permission. Is it needed here?
And other question, I read in the MSDN that RegCreateKeyEx open the
key if it's already exist. But while debugging I get error if the key
is already exist. What's the truth about that function?
Thanks again
Dave