Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi to everyone,

I have an application that needs to access a file. To know here this file was installed I place a PATH value in the Application Key at the Windows Registry.

Ussualy I use SHGetValue() and it works, however I was using it inside of a class which uses Gtkmm library. And I dont know wy but the ShlwApi.h stoped working inside this class (linking errors to the shlwapi.lib ... unresolved external...bla bla bla). So I instead of finding a solution for the lib I studied how to do without it.

I've created manually a Key at HKEY_LOCAL_MACHINE\SOFTWARE\MYAPP
and created a PATH value REG_SZ type the value at this moment has HEllo WOrld.

At this point I'm doing the following:

HKEY   hkey;
 if  (RegOpenKeyEx (HKEY_LOCAL_MACHINE,TEXT  ("SOFTWARE\\MYAPP"),
	 0,
	 KEY_ALL_ACCESS, 
	 &hkey) == ERROR_SUCCESS)
 {

	 unsigned char rgValue [1024];
	 unsigned char fnlRes [1024];
	 DWORD size1;
	 DWORD type = REG_SZ;
        
         DWORD dwRet;

	 size1=1023;

	 dwRet = RegQueryValueEx( hkey, L"PATH", NULL, &type, (unsigned char*)&rgValue, &size1); // Here I tried with and without reference at rgValue

	 switch (dwRet)
	 {
	 case ERROR_SUCCESS:
		 std::cout << "OK" << std::endl;
		 std::cout << rgValue << std::endl;
		 break;
	 case ERROR_MORE_DATA:
        std::cout << "MORE DATA Error" << std::endl;
		 break;
	 case ERROR_FILE_NOT_FOUND:
		 std::cout << "File Not Found" << std::endl;
		 break;
	 default:
		 
		 std::cout << "OTHER system error code" << std::endl;
		 break;
	 }


 }    


 RegCloseKey(hkey);



-----The Output of this code is:
---- OK -> this means ERROR SUCCESS
---- H -> returns me only the first character of the string in the PATH -> should be Hello WOrld

I'm tring convertions, casts, changing type etc.. but without success.

Can you tell me what I'm doing wrong?

Thanks Apostolo
Posted
Updated 25-Jan-11 6:33am
v2
Comments
Emilio Garavaglia 25-Jan-11 12:33pm    
added <pre> tags

Instead of using char use wchar_t. I had the same problem and it is resolved when i use wchar_t.
 
Share this answer
 
Comments
Apstolo 25-Jan-11 6:50am    
It solved. I've done like this:

wchar_t rgValue [1024];
DWORD size1;
DWORD Type = REG_SZ;
DWORD dwRet;

size1=1023;

dwRet =RegQueryValueEx( hkey, L"PATH", NULL, &Type,(LPBYTE) &rgValue, &size1);

// and finally
std::wcout << rgValue << std::endl;
CPallini 25-Jan-11 6:54am    
Note that now you're passing the wrong size to RegQueryValueEx (you pass almost the half of the array size, it shouldn't harm).
ShilpiP 25-Jan-11 6:52am    
Good :)
Thanks,

But as I've commented I tried with and without the reference, but the result was exactly the same.
I tried now without the cast and without the reference as you told me. The result is the same.

Any clue?
 
Share this answer
 
Registry strings use wide chars, change
std::cout << rgValue << std::endl;

to

std::wcout << (const wchar_t * ) rgValue << std::endl;

:)
 
Share this answer
 
v2
--------------
Registry strings use wide chars, change
Collapse

std::cout << rgValue << std::endl;

to

Collapse

std::wcout << rgValue << std::endl;

Smile
link |
Posted 7 mins ago
CPallini105K
-------------

Sory I didn't mentioned before, but I have tried both. The outcome using wcout was

---- OK
---- 0012E5B8 -> using std::wcout -> expeting wide char, I was surprised.

Thats why I writhed the Question with std::cout

I'm wandering if it is a problem of Type and the fact I created it manually through regedit.
 
Share this answer
 
v2
Comments
ShilpiP 25-Jan-11 6:44am    
Please don't write your comment in answer section.
CPallini 25-Jan-11 6:51am    
I forgot you declared rgValue as array of char: now I've updated my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900