PDA

Click to See Complete Forum and Search --> : Reg Query Problem


pskyboy
Dec 24th, 2001, 12:00 PM
Hey Guys

Can anyone see a problem with this code its meant to read a value out of a string in teh registry and there is definately a value there. All this does though is display 3 random characters. The dword returns the right length of the string though.

Thanks for the help
Peter


TCHAR sInstallDirectory[MAX_PATH];
BYTE sInstallDirectoryUNI[MAX_PATH];
DWORD dwLengthOfInstallDirectory = NULL;

HKEY hUninstallInfo;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\RCAS\\UninstallInfo", 0, KEY_ALL_ACCESS, &hUninstallInfo);

if(RegQueryValueEx(hUninstallInfo, "InstallDirectory", NULL, NULL, &sInstallDirectoryUNI[0], &dwLengthOfInstallDirectory) == ERROR_SUCCESS)
{
TCHAR sTest[10];
ultoa(dwLengthOfInstallDirectory, sTest, 10);
AfxMessageBox(sTest);

for(int i = 0; i <= dwLengthOfInstallDirectory; i++)
{
sInstallDirectory[i] = sInstallDirectoryUNI[i];
}
sInstallDirectory[i++] = NULL;

AfxMessageBox(sInstallDirectory);
}
else
{
AfxMessageBox("Read Reg Entry Failed");
CErrorHandler Error1;

DWORD dwLastError;
dwLastError = GetLastError();
Error1.ResolveError(dwLastError);
}

jim mcnamara
Dec 26th, 2001, 10:24 AM
Your loop doesn't translate unicode, if in fact, it just copies
it. sIntallDirectory has a lot of 'junk' bytes in it. Unicode is a two-byte character. This is the reason you're getting garbage. At the moment, I don't remember how to convert unicode to standard ASCII.

Anyone else know?


for(int i = 0; i <= dwLengthOfInstallDirectory; i++)
{
sInstallDirectory[i] = sInstallDirectoryUNI[i];
}
sInstallDirectory[i++] = NULL;

jim mcnamara
Dec 26th, 2001, 10:37 AM
I went brain dead.

Convert from wide to ASCII with:

size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count );

parksie
Dec 27th, 2001, 05:23 AM
That actually converts to MBCS.

As of today, I still haven't found any conversion from Unicode to straight ASCII.

jim mcnamara
Dec 27th, 2001, 07:22 AM
Parksie -
do you know about USES_CONVERSION, part of atl?
There are a bunch of macros. A2W, W2A, etc.

#include <atlbase.h>

USES_CONVERSION;

LPCWSTR strWide = L"unicode";
LPCSTR strASCII = W2A(strWide);

parksie
Dec 27th, 2001, 07:24 AM
Yes but that only applies to MSVC.

Don't you need to bring COM in in order to use ATL?

jim mcnamara
Dec 27th, 2001, 09:11 AM
I think so.

Anyway, it would add a lot of complexity to what the pskboy is trying to do. Those macros are the first thing I think about when I run into wide chars. wcstombs is at the bottom of the list.

parksie
Dec 28th, 2001, 07:24 AM
You've confused me.

You said it would add a lot of complexity but it's the first thing on your list :confused:

I wrote a couple of operators for the strings (i.e. you could implicitly create a string from a wstring and vice versa) but I forgot which bit of code I put them in. The main problem with that is that it's not pure Unicode->ASCII - it does MBCS since that's all that I could find that was supported.