Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. RegOpenKeyEx and _UNICODE

RegOpenKeyEx and _UNICODE

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Chintoo723

    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 cast

    Any idea why 'RegOpenKeyEx' is getting resolved to 'RegOpenKeyExA' when _UNICODE is defined? Can we fix this problem without explicit casting? Thanks in advance.

    J Offline
    J Offline
    Jose Lamas Rios
    wrote on last edited by
    #2

    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/[^]

    C 1 Reply Last reply
    0
    • J Jose Lamas Rios

      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/[^]

      C Offline
      C Offline
      Chintoo723
      wrote on last edited by
      #3

      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...

      J 1 Reply Last reply
      0
      • C Chintoo723

        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...

        J Offline
        J Offline
        Jose Lamas Rios
        wrote on last edited by
        #4

        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/[^]

        C 1 Reply Last reply
        0
        • J Jose Lamas Rios

          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/[^]

          C Offline
          C Offline
          Chintoo723
          wrote on last edited by
          #5

          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 *'

          C A 2 Replies Last reply
          0
          • C Chintoo723

            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 *'

            C Offline
            C Offline
            Chintoo723
            wrote on last edited by
            #6

            Ok, the problem goes away if I define both UNICODE as well as _UNICODE. Is this how it is supposed to be?

            J 1 Reply Last reply
            0
            • C Chintoo723

              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 *'

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #7

              Get rid of the _MBCS and define both UNICODE and _UNICODE. #include #include const TCHAR *mykeyname = "Software\MyCompany"; Then call RegOpenKeyEx...

              A 1 Reply Last reply
              0
              • A Anonymous

                Get rid of the _MBCS and define both UNICODE and _UNICODE. #include #include const TCHAR *mykeyname = "Software\MyCompany"; Then call RegOpenKeyEx...

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #8

                Oops this should read... static const TCHAR *mykeyname = "Software\\MyCompany";

                T 1 Reply Last reply
                0
                • C Chintoo723

                  Ok, the problem goes away if I define both UNICODE as well as _UNICODE. Is this how it is supposed to be?

                  J Offline
                  J Offline
                  Jose Lamas Rios
                  wrote on last edited by
                  #9

                  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/[^]

                  1 Reply Last reply
                  0
                  • A Anonymous

                    Oops this should read... static const TCHAR *mykeyname = "Software\\MyCompany";

                    T Offline
                    T Offline
                    ThatsAlok
                    wrote on last edited by
                    #10

                    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

                    O 1 Reply Last reply
                    0
                    • T ThatsAlok

                      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

                      O Offline
                      O Offline
                      One Stone
                      wrote on last edited by
                      #11

                      Crap everything, he forgot the _T macro. Also, you don't need the static. Correctly, it should read: const TCHAR *mykeyname = _T("Software\\MyCompany");

                      T 1 Reply Last reply
                      0
                      • O One Stone

                        Crap everything, he forgot the _T macro. Also, you don't need the static. Correctly, it should read: const TCHAR *mykeyname = _T("Software\\MyCompany");

                        T Offline
                        T Offline
                        ThatsAlok
                        wrote on last edited by
                        #12

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups