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. Insert value from GUI CString into Registry

Insert value from GUI CString into Registry

Scheduled Pinned Locked Moved C / C++ / MFC
windows-adminhelp
7 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.
  • T Offline
    T Offline
    trumper
    wrote on last edited by
    #1

    Hi, So I have this code to open a handle hKey which works fine. RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyProgram", 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); Then I try to set a value from a GUI CString member variable m_license_key into registry. RegSetValueEx(hKey, "PROGRAM_KEY", 0L, REG_DWORD,(LPBYTE)&m_license_key, sizeof(DWORD)); The set function also works fine other than the fact that "rubbish" gets inserted as value for my registry key. In other words, HKEY_LOCAL_MACHINE\\SOFTWARE\\MyProgram\\PROGRAM_KEY is added, but the value is not what I keyed into m_license_key (it is just some strange text or some 0xHex numbers) To be honest, I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx. Any help would be greatly appreciated. Thanks.

    J L M 3 Replies Last reply
    0
    • T trumper

      Hi, So I have this code to open a handle hKey which works fine. RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyProgram", 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); Then I try to set a value from a GUI CString member variable m_license_key into registry. RegSetValueEx(hKey, "PROGRAM_KEY", 0L, REG_DWORD,(LPBYTE)&m_license_key, sizeof(DWORD)); The set function also works fine other than the fact that "rubbish" gets inserted as value for my registry key. In other words, HKEY_LOCAL_MACHINE\\SOFTWARE\\MyProgram\\PROGRAM_KEY is added, but the value is not what I keyed into m_license_key (it is just some strange text or some 0xHex numbers) To be honest, I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx. Any help would be greatly appreciated. Thanks.

      J Offline
      J Offline
      Joan M
      wrote on last edited by
      #2

      I use this code...

      bool CRegistry::SetKey(HKEY hKey, CString csSubkey, CString csValName, CString csVal)
      {
      if(hKey != HKEY_CURRENT_USER)
      return false;

      if(csSubkey.IsEmpty() || csValName.IsEmpty() || csVal.IsEmpty())
      	return false;
      
      HKEY hWkKey;
      LONG lRetorn = RegOpenKeyEx(hKey,csSubkey,0,KEY\_ALL\_ACCESS,&hWkKey);
      if(lRetorn == ERROR\_SUCCESS)
      {
      	int iLength = csVal.GetLength();
      	lRetorn = RegSetValueEx(hWkKey,
      								csValName,
      								0, 
      								REG\_SZ, 
      								(BYTE \*)csVal.GetBuffer(0),
      								iLength);
      
      	RegFlushKey(hWkKey);
      	RegCloseKey(hWkKey);
      
      	if( lRetorn == ERROR\_SUCCESS)
      		return true;
      }
      return false;
      

      }

      Hope this helps.

      https://www.robotecnik.com freelance robots, PLC and CNC programmer.

      1 Reply Last reply
      0
      • T trumper

        Hi, So I have this code to open a handle hKey which works fine. RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyProgram", 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); Then I try to set a value from a GUI CString member variable m_license_key into registry. RegSetValueEx(hKey, "PROGRAM_KEY", 0L, REG_DWORD,(LPBYTE)&m_license_key, sizeof(DWORD)); The set function also works fine other than the fact that "rubbish" gets inserted as value for my registry key. In other words, HKEY_LOCAL_MACHINE\\SOFTWARE\\MyProgram\\PROGRAM_KEY is added, but the value is not what I keyed into m_license_key (it is just some strange text or some 0xHex numbers) To be honest, I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx. Any help would be greatly appreciated. Thanks.

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        trumper wrote:

        I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx.

        They hide that information in the documentation[^] :rolleyes:

        led mike

        M R 2 Replies Last reply
        0
        • T trumper

          Hi, So I have this code to open a handle hKey which works fine. RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyProgram", 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); Then I try to set a value from a GUI CString member variable m_license_key into registry. RegSetValueEx(hKey, "PROGRAM_KEY", 0L, REG_DWORD,(LPBYTE)&m_license_key, sizeof(DWORD)); The set function also works fine other than the fact that "rubbish" gets inserted as value for my registry key. In other words, HKEY_LOCAL_MACHINE\\SOFTWARE\\MyProgram\\PROGRAM_KEY is added, but the value is not what I keyed into m_license_key (it is just some strange text or some 0xHex numbers) To be honest, I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx. Any help would be greatly appreciated. Thanks.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          You can't cast a CString to a LPBYTE. If you want the text in the CString saved then use the REG_SZ type... RegSetValueEx(hKey, "PROGRAM_KEY", 0L, REG_SZ,(LPBYTE)(LPCTSTR)m_license_key, m_license_key.GetLength() + 1);

          1 Reply Last reply
          0
          • L led mike

            trumper wrote:

            I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx.

            They hide that information in the documentation[^] :rolleyes:

            led mike

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            led mike wrote:

            They hide that information

            bastards

            T 1 Reply Last reply
            0
            • M Mark Salsbery

              led mike wrote:

              They hide that information

              bastards

              T Offline
              T Offline
              trumper
              wrote on last edited by
              #6

              Thanks guys for all the help. (BYTE *)m_license_key.GetBuffer(0) worked best for me =) Really appreciate it.

              1 Reply Last reply
              0
              • L led mike

                trumper wrote:

                I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx.

                They hide that information in the documentation[^] :rolleyes:

                led mike

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                How mean they do it to us :-D


                Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா

                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