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. a prob with HKEY

a prob with HKEY

Scheduled Pinned Locked Moved C / C++ / MFC
windows-admindebugginghelp
7 Posts 4 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.
  • K Offline
    K Offline
    Krauze
    wrote on last edited by
    #1

    BOOL checkExistence(HKEY *hKey, LPCTSTR lpszAppName)
    {
    CRegKey regKey;

    if( regKey.Open(HKEY\_LOCAL\_MACHINE, \_T("SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall"),
    				KEY\_READ) != ERROR\_SUCCESS )
    	return FALSE;
    
    DWORD dwIndex = 0;
    DWORD cbName  = 128;
    TCHAR szSubkeyName\[128\];
    
    CString strSubkeyName;
    
    int nHasAllItems = 0;
    
    while( regKey.EnumKey(dwIndex, szSubkeyName, &cbName) != ERROR\_NO\_MORE\_ITEMS )
    {
    	dwIndex++;
    	cbName = 128;
    
    	// TCHAR->CString
    	strSubkeyName.Format(\_T("%s"), szSubkeyName);
    	if( strSubkeyName.Compare(lpszAppName) )
    	{
    		nHasAllItems++;
    		continue;
    	}
    	else
    		break;
    }
    
    regKey.Close();
    
    if( nHasAllItems == dwIndex )
    	return FALSE;
    else
    {
    	\*hKey = regKey.m\_hKey;// this causes debug assertion failure:m\_hKey!=0
    	return TRUE;
    }
    

    }

    I wrote this funx to check whether there's a particular installed app in the registry. After calling this funx, hKey is expected to be assigned the registry handle. But prob occurs as Ive indicated above.

    D C K 3 Replies Last reply
    0
    • K Krauze

      BOOL checkExistence(HKEY *hKey, LPCTSTR lpszAppName)
      {
      CRegKey regKey;

      if( regKey.Open(HKEY\_LOCAL\_MACHINE, \_T("SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall"),
      				KEY\_READ) != ERROR\_SUCCESS )
      	return FALSE;
      
      DWORD dwIndex = 0;
      DWORD cbName  = 128;
      TCHAR szSubkeyName\[128\];
      
      CString strSubkeyName;
      
      int nHasAllItems = 0;
      
      while( regKey.EnumKey(dwIndex, szSubkeyName, &cbName) != ERROR\_NO\_MORE\_ITEMS )
      {
      	dwIndex++;
      	cbName = 128;
      
      	// TCHAR->CString
      	strSubkeyName.Format(\_T("%s"), szSubkeyName);
      	if( strSubkeyName.Compare(lpszAppName) )
      	{
      		nHasAllItems++;
      		continue;
      	}
      	else
      		break;
      }
      
      regKey.Close();
      
      if( nHasAllItems == dwIndex )
      	return FALSE;
      else
      {
      	\*hKey = regKey.m\_hKey;// this causes debug assertion failure:m\_hKey!=0
      	return TRUE;
      }
      

      }

      I wrote this funx to check whether there's a particular installed app in the registry. After calling this funx, hKey is expected to be assigned the registry handle. But prob occurs as Ive indicated above.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      How are you calling checkExistence()?

      Krauze wrote:

      strSubkeyName.Format(_T("%s"), szSubkeyName);

      What's wrong with: strSubkeyName = szSubkeyName;

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Man who follows car will be exhausted." - Confucius

      K 1 Reply Last reply
      0
      • D David Crow

        How are you calling checkExistence()?

        Krauze wrote:

        strSubkeyName.Format(_T("%s"), szSubkeyName);

        What's wrong with: strSubkeyName = szSubkeyName;

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Man who follows car will be exhausted." - Confucius

        K Offline
        K Offline
        Krauze
        wrote on last edited by
        #3

        I mean:

        *hKey = regKey.m_hKey;

        Prob occurs here.

        D 1 Reply Last reply
        0
        • K Krauze

          BOOL checkExistence(HKEY *hKey, LPCTSTR lpszAppName)
          {
          CRegKey regKey;

          if( regKey.Open(HKEY\_LOCAL\_MACHINE, \_T("SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall"),
          				KEY\_READ) != ERROR\_SUCCESS )
          	return FALSE;
          
          DWORD dwIndex = 0;
          DWORD cbName  = 128;
          TCHAR szSubkeyName\[128\];
          
          CString strSubkeyName;
          
          int nHasAllItems = 0;
          
          while( regKey.EnumKey(dwIndex, szSubkeyName, &cbName) != ERROR\_NO\_MORE\_ITEMS )
          {
          	dwIndex++;
          	cbName = 128;
          
          	// TCHAR->CString
          	strSubkeyName.Format(\_T("%s"), szSubkeyName);
          	if( strSubkeyName.Compare(lpszAppName) )
          	{
          		nHasAllItems++;
          		continue;
          	}
          	else
          		break;
          }
          
          regKey.Close();
          
          if( nHasAllItems == dwIndex )
          	return FALSE;
          else
          {
          	\*hKey = regKey.m\_hKey;// this causes debug assertion failure:m\_hKey!=0
          	return TRUE;
          }
          

          }

          I wrote this funx to check whether there's a particular installed app in the registry. After calling this funx, hKey is expected to be assigned the registry handle. But prob occurs as Ive indicated above.

          C Offline
          C Offline
          Cool_Dev
          wrote on last edited by
          #4

          "hKey is expected to be assigned the registry handle." but how as you did regKey.Close(); before *hKey = regKey.m_hKey; ?? Try

          *hKey = regKey.Detach();

          Remeber to call RegCloseKey() on the obtained handle once finished using it.

          modified on Wednesday, July 28, 2010 1:30 AM

          1 Reply Last reply
          0
          • K Krauze

            I mean:

            *hKey = regKey.m_hKey;

            Prob occurs here.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            How are you calling checkExistence()? Aso, have you considered something a little more manageable, like:

            BOOL checkExistence( HKEY *hKey, LPCTSTR lpszAppName )
            {
            CString strKey;
            strKey.Format(_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s"), lpszAppName);

            return RegOpenKey(HKEY\_LOCAL\_MACHINE, strKey, hKey) == ERROR\_SUCCESS;
            

            }

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Man who follows car will be exhausted." - Confucius

            K 1 Reply Last reply
            0
            • K Krauze

              BOOL checkExistence(HKEY *hKey, LPCTSTR lpszAppName)
              {
              CRegKey regKey;

              if( regKey.Open(HKEY\_LOCAL\_MACHINE, \_T("SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall"),
              				KEY\_READ) != ERROR\_SUCCESS )
              	return FALSE;
              
              DWORD dwIndex = 0;
              DWORD cbName  = 128;
              TCHAR szSubkeyName\[128\];
              
              CString strSubkeyName;
              
              int nHasAllItems = 0;
              
              while( regKey.EnumKey(dwIndex, szSubkeyName, &cbName) != ERROR\_NO\_MORE\_ITEMS )
              {
              	dwIndex++;
              	cbName = 128;
              
              	// TCHAR->CString
              	strSubkeyName.Format(\_T("%s"), szSubkeyName);
              	if( strSubkeyName.Compare(lpszAppName) )
              	{
              		nHasAllItems++;
              		continue;
              	}
              	else
              		break;
              }
              
              regKey.Close();
              
              if( nHasAllItems == dwIndex )
              	return FALSE;
              else
              {
              	\*hKey = regKey.m\_hKey;// this causes debug assertion failure:m\_hKey!=0
              	return TRUE;
              }
              

              }

              I wrote this funx to check whether there's a particular installed app in the registry. After calling this funx, hKey is expected to be assigned the registry handle. But prob occurs as Ive indicated above.

              K Offline
              K Offline
              KarstenK
              wrote on last edited by
              #6

              regKey.Close(); *hKey = regKey.m_hKey; after closing the key the handle is invalid. You really shouldnt do that :doh: Press F1 for help or google it. Greetings from Germany

              1 Reply Last reply
              0
              • D David Crow

                How are you calling checkExistence()? Aso, have you considered something a little more manageable, like:

                BOOL checkExistence( HKEY *hKey, LPCTSTR lpszAppName )
                {
                CString strKey;
                strKey.Format(_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s"), lpszAppName);

                return RegOpenKey(HKEY\_LOCAL\_MACHINE, strKey, hKey) == ERROR\_SUCCESS;
                

                }

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Man who follows car will be exhausted." - Confucius

                K Offline
                K Offline
                Krauze
                wrote on last edited by
                #7

                In fact Ive tried this method later. And it works perfectly.

                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