Results 1 to 8 of 8

Thread: Reg Query Problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    England
    Posts
    94

    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]

  2. #2
    jim mcnamara
    Guest
    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;

  3. #3
    jim mcnamara
    Guest
    I went brain dead.

    Convert from wide to ASCII with:

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

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    jim mcnamara
    Guest
    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);

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7
    jim mcnamara
    Guest
    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.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width