Archived

This topic is now archived and is closed to further replies.

darkchrono4

Registry Fuction Questions

Recommended Posts

darkchrono4    202
In a .c file I had a function to read some registry entries and make my own. It worked good and didn''t have any problems. I needed to use that code in this other project that has to be in C++ because of the wrapper class I''m using. Now that I renamed the file a .cpp file I can get any of the query and set value functions to work. I get something like this: error C2664: ''RegQueryValueExA'' : cannot convert parameter 5 from ''char *'' to ''unsigned char *'' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast And this is my code: DWORD size; RegQueryValueEx(pHKey1, "Install Path", NULL, NULL, InstallPath, &size); I get errors when I try and use RegSetValueEx(). I get pretty much the same error on the last parameter. Which is the length of the entry to be written. I was just using stren(filename) to get that. Like I said this worked great when it was just a .c file but now that its a .cpp I don''t know what the problem is.

Share this post


Link to post
Share on other sites
Sneftel    1788
for some reason, your InstallPath has been declared as an unsigned char array, instead of a regular char array. Either change it back, or just use a typecast (try reinterpret_cast<char*>(InstallPath) for the fifth parameter)


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
darkchrono4    202
Thats the thing about it, its char InstallPath[MAX_PATH] = "". The only way I can get it to compile is by typecasting it to a (unsigned char *). I don''t have any clue.

Share this post


Link to post
Share on other sites
Xai    1848
you guys have it backwards ... your array is a char * right now, the function expects an unsigned char * ... in C this cast is automatic promotion I think ... in C++ it is definately not.

The SECOND parameter to the function MUST be a signed char * ... the FIFTH parameter to the function MUST be an unsigned char * ... that''s just how it works .. whenever you see the MSDN use LPCSTR, it is a signed char type ... whenever the MSDN uses a LPBYTE is is expecting an unsigned char type ...

here''s some of my code ... to help show that some things must be each type, and some things MUST be cast.


  
extern "C" EXPORTDLL_API HRESULT __stdcall DllRegisterServer(void)
{
HKEY clsidKey;
HKEY componentKey;
const char *componentID = "{6F4B8BCD-E5B6-4543-8CFF-1FC62E95D34A}";
const BYTE *componentName = (BYTE*)"Linear Number Generator";
HKEY inprocKey;
const char *inprocName = "InprocServer32";
const BYTE *systemLocation = (BYTE*)"C:\\Develop\\Install\\Component\\LinearGeneratorComponent.dll";

// open HKEY_CLASSES_ROOT\CLSID

RegOpenKeyEx(HKEY_CLASSES_ROOT,"CLSID",0,KEY_WRITE,&clsidKey);

// create component key

RegCreateKeyEx(clsidKey,componentID,0,"",REG_OPTION_NON_VOLATILE,KEY_WRITE
,NULL,&componentKey,NULL);

// set component value - component name

RegSetValueEx(componentKey,"
",0,REG_SZ,componentName,strlen((char*)componentName)+1);

// create inproc server subkey

RegCreateKeyEx(componentKey,inprocName,0,"
",REG_OPTION_NON_VOLATILE,KEY_WRITE
,NULL,&inprocKey,NULL);

// set inproc server value - system location

RegSetValueEx(inprocKey,"
",0,REG_SZ,systemLocation,strlen((char*)systemLocation)+1);

RegCloseKey(inprocKey);
RegCloseKey(componentKey);
RegCloseKey(clsidKey);

return S_OK;
}

Share this post


Link to post
Share on other sites
NewbieGamer    122
This might be a case where your target is UNICODE. Normally the APIs that have LPTSTR, or any "T*" datatype are "flexible" enough to be used by ANSI or UNICODE clients. Based on the Macro definitions UNICODE, _UNICODE the code will get compiled as UNICODE or non-UNICODE.
If you want to be generic enough, you should also use TCHAR types in your code. If you want to get away from that and want to address non-UNICODE Clients, turn off the UNICODE flags [i think they are in the project settings]. If you want to address UNICODE only clients, you can use wchar types..... [which is nothing but unsigned short*, in most of the cases.

HTH,

Share this post


Link to post
Share on other sites
darkchrono4    202
Sorry, been away from the computer for a few days. A bit of (BYTE *) typcasting got it to work for me. Thank you for that code sample, that was what got me to figure my problem out.

Share this post


Link to post
Share on other sites