Discussion:
C++ unicode char's and int's...
(too old to reply)
Daniel Bass
2003-08-07 07:51:46 UTC
Permalink
I've got a integer height for a window stored in the registry, but as a
string (REG_SZ)... I want to retrieve this unicode string value, and convert
it into an int, which is then in the form i can use...

below is the code, your help would be deeply appreciated!!!



//
// access the height from the registry
//
int nWindowHeight = 160; // in pixels

HKEY hKey;
TCHAR* regKey = _T("SOFTWARE\\KHI-RO");

LONG result = RegOpenKeyEx( HKEY_CURRENT_USER, regKey, 0, KEY_READ,
&hKey );

if ( result == ERROR_SUCCESS )
{
//
// the reg open succeeded
//
DWORD dwDataSize = 256;
DWORD dwDataType = REG_SZ;
BYTE szData[256];
TCHAR* subKey = _T("KbdHeight");
result = RegQueryValueEx( hKey,
subKey,
0,
&dwDataType,
szData,
&dwDataSize );

if ( result == ERROR_SUCCESS )
{
//
// the reg value retrieval was a success
//
BYTE szData[256];

MessageBox( hwndSip, (TCHAR*)szData, _T("RESULT value"),
MB_OK );
/*** according to the message box szData contains 250, which is
correct... ***/

LONG nRegValue = atol((char*)szData); /*** <<-- I think the
problem's here ***/

//
// some value range checking
//
if ( nRegValue > 20 && nRegValue <= 320 )
{
nWindowHeight = nRegValue;

// doesn't get hit..., so loop doesn't get entered,
therefore atol is the problem...
MessageBox( hwndSip, _T("We got within the required
range..."), _T("DID IT"), MB_OK );
}

}

}

RegCloseKey( hKey );
Daniel Bass
2003-08-07 08:13:07 UTC
Permalink
cus i'm thick and don't know how to get it out again...

if i said :

DWORD dwDataType = REG_DWORD;

then I think to myself, well that means that

DWORD szData;


but it doesn't, because the RegQueryValueEx only accepts a BYTE* so I'd have
to convert it again anyway??

unless you've a suggestion, cus, like i said, i'm thick! ;o)
LONG nRegValue = _ttol(szData);
BTW why not store this value in registry as integer, not string?
--
Sincerely,
Alexander
http://www.RSDN.ru - Russian Software Developer Network
Post by Daniel Bass
I've got a integer height for a window stored in the registry, but as a
string (REG_SZ)... I want to retrieve this unicode string value, and
convert
Post by Daniel Bass
it into an int, which is then in the form i can use...
below is the code, your help would be deeply appreciated!!!
//
// access the height from the registry
//
int nWindowHeight = 160; // in pixels
HKEY hKey;
TCHAR* regKey = _T("SOFTWARE\\KHI-RO");
LONG result = RegOpenKeyEx( HKEY_CURRENT_USER, regKey, 0, KEY_READ,
&hKey );
if ( result == ERROR_SUCCESS )
{
//
// the reg open succeeded
//
DWORD dwDataSize = 256;
DWORD dwDataType = REG_SZ;
BYTE szData[256];
TCHAR* subKey = _T("KbdHeight");
result = RegQueryValueEx( hKey,
subKey,
0,
&dwDataType,
szData,
&dwDataSize );
if ( result == ERROR_SUCCESS )
{
//
// the reg value retrieval was a success
//
BYTE szData[256];
MessageBox( hwndSip, (TCHAR*)szData, _T("RESULT value"),
MB_OK );
/*** according to the message box szData contains 250, which
is
Post by Daniel Bass
correct... ***/
LONG nRegValue = atol((char*)szData); /*** <<-- I think the
problem's here ***/
//
// some value range checking
//
if ( nRegValue > 20 && nRegValue <= 320 )
{
nWindowHeight = nRegValue;
// doesn't get hit..., so loop doesn't get entered,
therefore atol is the problem...
MessageBox( hwndSip, _T("We got within the required
range..."), _T("DID IT"), MB_OK );
}
}
}
RegCloseKey( hKey );
Daniel Bass
2003-08-07 08:17:22 UTC
Permalink
thanks for your help, but _ttol looks for a TCHAR, and hence needs casting
to unsigned short * ?
Post by Daniel Bass
cus i'm thick and don't know how to get it out again...
DWORD dwDataType = REG_DWORD;
then I think to myself, well that means that
DWORD szData;
but it doesn't, because the RegQueryValueEx only accepts a BYTE* so I'd have
to convert it again anyway??
unless you've a suggestion, cus, like i said, i'm thick! ;o)
LONG nRegValue = _ttol(szData);
BTW why not store this value in registry as integer, not string?
--
Sincerely,
Alexander
http://www.RSDN.ru - Russian Software Developer Network
Post by Daniel Bass
I've got a integer height for a window stored in the registry, but as a
string (REG_SZ)... I want to retrieve this unicode string value, and
convert
Post by Daniel Bass
it into an int, which is then in the form i can use...
below is the code, your help would be deeply appreciated!!!
//
// access the height from the registry
//
int nWindowHeight = 160; // in pixels
HKEY hKey;
TCHAR* regKey = _T("SOFTWARE\\KHI-RO");
LONG result = RegOpenKeyEx( HKEY_CURRENT_USER, regKey, 0, KEY_READ,
&hKey );
if ( result == ERROR_SUCCESS )
{
//
// the reg open succeeded
//
DWORD dwDataSize = 256;
DWORD dwDataType = REG_SZ;
BYTE szData[256];
TCHAR* subKey = _T("KbdHeight");
result = RegQueryValueEx( hKey,
subKey,
0,
&dwDataType,
szData,
&dwDataSize );
if ( result == ERROR_SUCCESS )
{
//
// the reg value retrieval was a success
//
BYTE szData[256];
MessageBox( hwndSip, (TCHAR*)szData, _T("RESULT value"),
MB_OK );
/*** according to the message box szData contains 250, which
is
Post by Daniel Bass
correct... ***/
LONG nRegValue = atol((char*)szData); /*** <<-- I think
the
Post by Daniel Bass
problem's here ***/
//
// some value range checking
//
if ( nRegValue > 20 && nRegValue <= 320 )
{
nWindowHeight = nRegValue;
// doesn't get hit..., so loop doesn't get entered,
therefore atol is the problem...
MessageBox( hwndSip, _T("We got within the required
range..."), _T("DID IT"), MB_OK );
}
}
}
RegCloseKey( hKey );
Alexander Shargin
2003-08-07 10:47:36 UTC
Permalink
Since you are getting a string (UNICODE string), you should allocate buffer
as TCHAR szData[256], not BYTE szData[256]. Then, just cast ot BYTE* as I've
shown in the previous posting.

--
Sincerely,
Alexander

mailto:***@rsdn.ru
http://www.RSDN.ru - Russian Software Developer Network
Post by Daniel Bass
thanks for your help, but _ttol looks for a TCHAR, and hence needs casting
to unsigned short * ?
Post by Daniel Bass
cus i'm thick and don't know how to get it out again...
DWORD dwDataType = REG_DWORD;
then I think to myself, well that means that
DWORD szData;
but it doesn't, because the RegQueryValueEx only accepts a BYTE* so I'd
have
Post by Daniel Bass
to convert it again anyway??
unless you've a suggestion, cus, like i said, i'm thick! ;o)
LONG nRegValue = _ttol(szData);
BTW why not store this value in registry as integer, not string?
--
Sincerely,
Alexander
http://www.RSDN.ru - Russian Software Developer Network
Post by Daniel Bass
I've got a integer height for a window stored in the registry, but
as
Post by Daniel Bass
a
Post by Daniel Bass
Post by Daniel Bass
string (REG_SZ)... I want to retrieve this unicode string value, and
convert
Post by Daniel Bass
it into an int, which is then in the form i can use...
below is the code, your help would be deeply appreciated!!!
//
// access the height from the registry
//
int nWindowHeight = 160; // in pixels
HKEY hKey;
TCHAR* regKey = _T("SOFTWARE\\KHI-RO");
LONG result = RegOpenKeyEx( HKEY_CURRENT_USER, regKey, 0, KEY_READ,
&hKey );
if ( result == ERROR_SUCCESS )
{
//
// the reg open succeeded
//
DWORD dwDataSize = 256;
DWORD dwDataType = REG_SZ;
BYTE szData[256];
TCHAR* subKey = _T("KbdHeight");
result = RegQueryValueEx( hKey,
subKey,
0,
&dwDataType,
szData,
&dwDataSize );
if ( result == ERROR_SUCCESS )
{
//
// the reg value retrieval was a success
//
BYTE szData[256];
MessageBox( hwndSip, (TCHAR*)szData, _T("RESULT value"),
MB_OK );
/*** according to the message box szData contains 250,
which
Post by Daniel Bass
is
Post by Daniel Bass
correct... ***/
LONG nRegValue = atol((char*)szData); /*** <<-- I think
the
Post by Daniel Bass
problem's here ***/
//
// some value range checking
//
if ( nRegValue > 20 && nRegValue <= 320 )
{
nWindowHeight = nRegValue;
// doesn't get hit..., so loop doesn't get entered,
therefore atol is the problem...
MessageBox( hwndSip, _T("We got within the required
range..."), _T("DID IT"), MB_OK );
}
}
}
RegCloseKey( hKey );
Alexander Shargin
2003-08-07 10:46:40 UTC
Permalink
RegQueryValueEx works with ANY type as an array of bytes, so just cast your
pointer to DWORD variable to BYTE*:

DWORD dwValue;
DWORD dwDataSize = sizeof(dwValue);
DWORD dwDataType = REG_DWORD;
TCHAR* subKey = _T("KbdHeight");

result = RegQueryValueEx( hKey,
subKey,
0,
&dwDataType,
(BYTE*)&dwValue,
&dwDataSize );

--
Sincerely,
Alexander

mailto:***@rsdn.ru
http://www.RSDN.ru - Russian Software Developer Network
Post by Daniel Bass
cus i'm thick and don't know how to get it out again...
DWORD dwDataType = REG_DWORD;
then I think to myself, well that means that
DWORD szData;
but it doesn't, because the RegQueryValueEx only accepts a BYTE* so I'd have
to convert it again anyway??
unless you've a suggestion, cus, like i said, i'm thick! ;o)
LONG nRegValue = _ttol(szData);
BTW why not store this value in registry as integer, not string?
--
Sincerely,
Alexander
http://www.RSDN.ru - Russian Software Developer Network
Post by Daniel Bass
I've got a integer height for a window stored in the registry, but as a
string (REG_SZ)... I want to retrieve this unicode string value, and
convert
Post by Daniel Bass
it into an int, which is then in the form i can use...
below is the code, your help would be deeply appreciated!!!
//
// access the height from the registry
//
int nWindowHeight = 160; // in pixels
HKEY hKey;
TCHAR* regKey = _T("SOFTWARE\\KHI-RO");
LONG result = RegOpenKeyEx( HKEY_CURRENT_USER, regKey, 0, KEY_READ,
&hKey );
if ( result == ERROR_SUCCESS )
{
//
// the reg open succeeded
//
DWORD dwDataSize = 256;
DWORD dwDataType = REG_SZ;
BYTE szData[256];
TCHAR* subKey = _T("KbdHeight");
result = RegQueryValueEx( hKey,
subKey,
0,
&dwDataType,
szData,
&dwDataSize );
if ( result == ERROR_SUCCESS )
{
//
// the reg value retrieval was a success
//
BYTE szData[256];
MessageBox( hwndSip, (TCHAR*)szData, _T("RESULT value"),
MB_OK );
/*** according to the message box szData contains 250, which
is
Post by Daniel Bass
correct... ***/
LONG nRegValue = atol((char*)szData); /*** <<-- I think
the
Post by Daniel Bass
problem's here ***/
//
// some value range checking
//
if ( nRegValue > 20 && nRegValue <= 320 )
{
nWindowHeight = nRegValue;
// doesn't get hit..., so loop doesn't get entered,
therefore atol is the problem...
MessageBox( hwndSip, _T("We got within the required
range..."), _T("DID IT"), MB_OK );
}
}
}
RegCloseKey( hKey );
Almon B. Strowger
2003-08-08 09:25:58 UTC
Permalink
Hi,

Well if you want to keep it a string in the registry
(and don't want to simply cast), probably _wtoi()
is a better choice.

Hope this helps...

Almon B. Strowger
KOOK Pocket Software
Post by Daniel Bass
cus i'm thick and don't know how to get it out again...
DWORD dwDataType = REG_DWORD;
then I think to myself, well that means that
DWORD szData;
but it doesn't, because the RegQueryValueEx only accepts a BYTE* so I'd have
to convert it again anyway??
unless you've a suggestion, cus, like i said, i'm thick! ;o)
Continue reading on narkive:
Loading...