Hi all,

I am trying to optimize one of my applications by cutting down on the number of function calls I am making. I have decided to do this by reading definitions out of a file and using them as the argument for the function.

char registryHive[MAX_PATH];
                        ZeroMemory ( registryHive, MAX_PATH );
                        
                        FILE * ep = fopen( "test.txt", "r" );
                        
                        while ( !feof(ep) ) 
                        {
                              fgets(registryHive, MAX_PATH, ep);  
                              
                              HKEY regHandle; 
                              
                              char* pHive = registryHive;
                              char* pKey = registryHive; 
                              
                              while( *pKey != '\\' ) 
                              {
                                     pKey++; 
                                     if ( *pKey == '\0' )
                                     break; 
                              }
                              *(pKey++) = '\0';
                              
                              if ( strcmp(pHive, "HKEY_LOCAL_MACHINE") == 0  ) 
                                 regHandle = HKEY_LOCAL_MACHINE;
                              else if ( strcmp(pHive, "HKEY_CURRENT_USER") == 0 ) 
                                 regHandle = HKEY_CURRENT_USER; 
                              
                              RemoveRegKeys(regHandle, pKey, false); 
                        }
                        
                        fclose(ep);

For example, if I wanted to remove HKEY_LOCAL_MACHINE\Test, I would put that in my file and the code should get HKEY_LOCAL_MACHINE for pHive and Test for pKey.

Here is the definition of RemoveRegKeys:

int RemoveRegKeys(HKEY regAddressOne, char *regAddressTwo, bool silent)
{
    int doesExist; 
    char buffer[255];
    char output[255];
    
    ZeroMemory(buffer, 255);
    ZeroMemory(output, 255); 
    
    char logname[100];
                       
    GetEnvironmentVariable("HOMEDRIVE", logname, 100);
    strcat(logname, "\\JavaRa.log");
                       
    // FILE *fp;
    // fp = fopen(logname, "a");
    
    HKEY hkey;
    int check = RegOpenKeyEx(regAddressOne, regAddressTwo, 0, KEY_SET_VALUE, &hkey);
    doesExist = SHDeleteKey(regAddressOne, regAddressTwo);
    RegCloseKey(hkey);
    
    char test[80];
    sprintf(test, "key: %s\ncheck: %i\ndoesExist: %i", regAddressTwo, check, doesExist); 
    MessageBox(0, test, 0, 0); 
    
    if (doesExist == 0)
    {
       TCHAR szOne[MAX_PATH];
       ZeroMemory(szOne, MAX_PATH); 
       
       switch(globalLanguageChosen)
       {
            // ...
       }
       
       // fprintf(fp, "Found and removed: %s\r\n", regAddressTwo);
       sprintf(buffer, "%s %s\r\n", szOne, regAddressTwo);
       lstrcpy(output, buffer);
       
       if (silent != true)
       {
           MessageBox(0, output, "Output", MB_OK | MB_ICONINFORMATION); 
       }
    
    }
    // fclose(fp); 
}

My problem is, RegOpenKeyEx and SHDeleteKey always return 2 when I call them through RemoveRegKeys in the way that I have in the first code snippet. If I manually define a string, the function works fine. Is this a problem with the way I am reading the string out of the file, or am I corrupting the string somehow?

Thanks for your help.

If you need me to explain further, let me know.

Regards,
Paul

I am trying to optimize one of my applications by cutting down on the number of function calls I am making. I have decided to do this by reading definitions out of a file and using them as the argument for the function.

You've probably already considered this, but file operations are notorious for being very slow. Going by what you've shown, I don't think replacing a few function calls with an I/O bound performance drain will be an improvement.

while ( !feof(ep) ) 
                        {
                              fgets(registryHive, MAX_PATH, ep);

Note that this is not a good idea.

My suggestion:

while ( fgets(registryHive, MAX_PATH, ep) ) {

Yes, it does the same thing. :)

The last char in the registryHive buffer after fgets() call is '\n' if it was a well-formed text file.
Of course, it's not a part of registry key...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.