RegOpenKeyEx and _UNICODE
-
I have this piece of code:
result = RegOpenKeyEx(hkey, registrykey, 0,KEY_SET_VALUE ,&key);
where registrykey is a _TCHAR *. This line compiles fine with _MBCS defined, but when I change it to _UNICODE, I get this error:error C2664: 'RegOpenKeyExA' : cannot convert parameter 2 from 'unsigned short [512]' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style castAny idea why 'RegOpenKeyEx' is getting resolved to 'RegOpenKeyExA' when _UNICODE is defined? Can we fix this problem without explicit casting? Thanks in advance.
Where did you define _UNICODE? It needs to be defined for all the files (at the project level, for example) Or maybe you didn't re-create your pre-compiled headers? 'RegOpenKeyEx' getting resolved to 'RegOpenKeyExA' means _UNICODE wasn't defined at the time the compiler found its declaration. -- jlr http://jlamas.blogspot.com/[^]
-
Where did you define _UNICODE? It needs to be defined for all the files (at the project level, for example) Or maybe you didn't re-create your pre-compiled headers? 'RegOpenKeyEx' getting resolved to 'RegOpenKeyExA' means _UNICODE wasn't defined at the time the compiler found its declaration. -- jlr http://jlamas.blogspot.com/[^]
Jose Lamas Rios wrote: Where did you define _UNICODE? It needs to be defined for all the files (at the project level, for example) Yeah, I defined it in the project settings. I just changed _MBCS to _UNICODE. Jose Lamas Rios wrote: Or maybe you didn't re-create your pre-compiled headers? I changed 'Automatic use of precompiled headers' to 'dont use precompiled headers', and got the same errors. Jose Lamas Rios wrote: 'RegOpenKeyEx' getting resolved to 'RegOpenKeyExA' means _UNICODE wasn't defined at the time the compiler found its declaration. Note that _TCHAR is getting resolved to unsigned short, I am wondering whats going wrong with RegOpenKeyEx...
-
Jose Lamas Rios wrote: Where did you define _UNICODE? It needs to be defined for all the files (at the project level, for example) Yeah, I defined it in the project settings. I just changed _MBCS to _UNICODE. Jose Lamas Rios wrote: Or maybe you didn't re-create your pre-compiled headers? I changed 'Automatic use of precompiled headers' to 'dont use precompiled headers', and got the same errors. Jose Lamas Rios wrote: 'RegOpenKeyEx' getting resolved to 'RegOpenKeyExA' means _UNICODE wasn't defined at the time the compiler found its declaration. Note that _TCHAR is getting resolved to unsigned short, I am wondering whats going wrong with RegOpenKeyEx...
Flace wrote: Note that _TCHAR is getting resolved to unsigned short, I am wondering whats going wrong with RegOpenKeyEx... Try defining UNICODE (with no leading underscore) too. It seems like _UNICODE affects MFC headers, while UNICODE affects Win32 API headers. -- jlr http://jlamas.blogspot.com/[^]
-
Flace wrote: Note that _TCHAR is getting resolved to unsigned short, I am wondering whats going wrong with RegOpenKeyEx... Try defining UNICODE (with no leading underscore) too. It seems like _UNICODE affects MFC headers, while UNICODE affects Win32 API headers. -- jlr http://jlamas.blogspot.com/[^]
Jose Lamas Rios wrote: Try defining UNICODE (with no leading underscore too). It seems like _UNICODE affects MFC headers, while UNICODE affects Win32 API headers Right, I just noticed that that RegOpenKeyEx resolves to RegOpenKeyExW when UNICODE is defined, and not when _UNICODE is defined. I couldnt locate if UNICODE is automatically defined when _UNICODE is defined. So I now changed _UNICODE to UNICODE in the project settings, now tchar.h doesnt seem to know about UNICODE:
error C2664: 'RegOpenKeyExW' : cannot convert parameter 2 from 'char [512]' to 'const unsigned short *'
-
Jose Lamas Rios wrote: Try defining UNICODE (with no leading underscore too). It seems like _UNICODE affects MFC headers, while UNICODE affects Win32 API headers Right, I just noticed that that RegOpenKeyEx resolves to RegOpenKeyExW when UNICODE is defined, and not when _UNICODE is defined. I couldnt locate if UNICODE is automatically defined when _UNICODE is defined. So I now changed _UNICODE to UNICODE in the project settings, now tchar.h doesnt seem to know about UNICODE:
error C2664: 'RegOpenKeyExW' : cannot convert parameter 2 from 'char [512]' to 'const unsigned short *'
Ok, the problem goes away if I define both UNICODE as well as _UNICODE. Is this how it is supposed to be?
-
Jose Lamas Rios wrote: Try defining UNICODE (with no leading underscore too). It seems like _UNICODE affects MFC headers, while UNICODE affects Win32 API headers Right, I just noticed that that RegOpenKeyEx resolves to RegOpenKeyExW when UNICODE is defined, and not when _UNICODE is defined. I couldnt locate if UNICODE is automatically defined when _UNICODE is defined. So I now changed _UNICODE to UNICODE in the project settings, now tchar.h doesnt seem to know about UNICODE:
error C2664: 'RegOpenKeyExW' : cannot convert parameter 2 from 'char [512]' to 'const unsigned short *'
-
Get rid of the _MBCS and define both UNICODE and _UNICODE. #include #include const TCHAR *mykeyname = "Software\MyCompany"; Then call RegOpenKeyEx...
-
Ok, the problem goes away if I define both UNICODE as well as _UNICODE. Is this how it is supposed to be?
Flace wrote: Ok, the problem goes away if I define both UNICODE as well as _UNICODE. Is this how it is supposed to be? It does seem so :) -- jlr http://jlamas.blogspot.com/[^]
-
Anonymous wrote: static const TCHAR *mykeyname = "Software\\MyCompany"; Small Question, Why are you using static with const or vice versa :confused:
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta
-
Anonymous wrote: static const TCHAR *mykeyname = "Software\\MyCompany"; Small Question, Why are you using static with const or vice versa :confused:
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta
-
Crap everything, he forgot the _T macro. Also, you don't need the static. Correctly, it should read: const TCHAR *mykeyname = _T("Software\\MyCompany");
One Stone wrote: Correctly, it should read: Actually My Question is:- if he want to make any thing static then why he making it const or vice versa.as after making const we cannot change it value, so no use of static. Secondly :- One Stone wrote: const TCHAR *mykeyname = _T("Software\\MyCompany");
const TCHAR *mykeyname = "Software\\MyCompany";
This will compile too, but that wouldnot be right approach :)."Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta