From: daved170 on
On Dec 26, 5:11 pm, "ScottMcP [MVP]" <scott...(a)mvps.org> wrote:
> On Dec 26, 3:21 am, daved170 <daved...(a)gmail.com> wrote:
>
> > 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
>
> 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;
}
From: Thomas J. Gritzan on
Am 27.12.2009 16:26, schrieb daved170:
> On Dec 26, 5:11 pm, "ScottMcP [MVP]" <scott...(a)mvps.org> wrote:
>> On Dec 26, 3:21 am, daved170 <daved...(a)gmail.com> wrote:
>>
>>> 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
>>
>> 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);

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
From: Vincent Fatica on
On Sun, 27 Dec 2009 07:26:57 -0800 (PST), daved170 <daved170(a)gmail.com> wrote:

|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
From: daved170 on
On Dec 27, 7:03 pm, Vincent Fatica <vi...(a)blackholespam.net> wrote:
> On Sun, 27 Dec 2009 07:26:57 -0800 (PST), daved170 <daved...(a)gmail.com> wrote:
>
> |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