|
-
Dec 24th, 2001, 01:00 PM
#1
Thread Starter
Lively Member
Reg Query Problem
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
Code:
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);
}
"Let's all join forces, rule with an iron hand...and prove to all the world, metal rules the land..."
-- Judas Priest
My email is [email protected]
-
Dec 26th, 2001, 11:24 AM
#2
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?
Code:
for(int i = 0; i <= dwLengthOfInstallDirectory; i++)
{
sInstallDirectory[i] = sInstallDirectoryUNI[i];
}
sInstallDirectory[i++] = NULL;
-
Dec 26th, 2001, 11:37 AM
#3
I went brain dead.
Convert from wide to ASCII with:
size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count );
-
Dec 27th, 2001, 06:23 AM
#4
Monday Morning Lunatic
That actually converts to MBCS.
As of today, I still haven't found any conversion from Unicode to straight ASCII.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 27th, 2001, 08:22 AM
#5
Parksie -
do you know about USES_CONVERSION, part of atl?
There are a bunch of macros. A2W, W2A, etc.
Code:
#include <atlbase.h>
USES_CONVERSION;
LPCWSTR strWide = L"unicode";
LPCSTR strASCII = W2A(strWide);
-
Dec 27th, 2001, 08:24 AM
#6
Monday Morning Lunatic
Yes but that only applies to MSVC.
Don't you need to bring COM in in order to use ATL?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 27th, 2001, 10:11 AM
#7
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.
-
Dec 28th, 2001, 08:24 AM
#8
Monday Morning Lunatic
You've confused me.
You said it would add a lot of complexity but it's the first thing on your list 
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|