From: daved170 on
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);
}


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'
From: ScottMcP [MVP] on
On Dec 25, 8:38 am, daved170 <daved...(a)gmail.com> wrote:
> 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.





From: daved170 on
On Dec 25, 5:49 pm, "ScottMcP [MVP]" <scott...(a)mvps.org> wrote:
> On Dec 25, 8:38 am, daved170 <daved...(a)gmail.com> wrote:
>
>
>
>
>
> > 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
From: r_z_aret on
On Fri, 25 Dec 2009 05:38:12 -0800 (PST), daved170
<daved170(a)gmail.com> wrote:

>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
From: ScottMcP [MVP] on
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.