How to run *.reg file without user intervantion?

Hi,
I need to download *.reg file and register the component without user input. As you know, we can run the file using WinExec method. However, it gives a message that the registry information has been successfully entered into registry with 'OK' button. Is there anyway I can program to run the *.reg file without user input?
EKarimAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LischkeCommented:
Hi EKarim,

there's an API to do that but I'm not sure whether you want to use a selfwritten program or just the means the system provides?

Ciao, Mike

PS: The API is RegLoadKey...
0
abancroftCommented:
regsvr32 has a /s switch that stops the confirmation message being displayed.

0
EKarimAuthor Commented:
Yes, I would like to use a self-written program. If I use the RegLoadKey, I have to know the Key(s). I would not know the key(s)/Subkey(s) in the *.reg file. Could you give an example or idea how to accomplish this?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

LischkeCommented:
In this case I better let speak MSDN instead trying to explain all the stuff. Here's an article which contains most of the stuff you need (note: the RegRestoreKey function is only supported on NT):

HOWTO: Implement a RegRestoreKey() Function for Windows 95
Last reviewed: October 20, 1997
Article ID: Q175329
 


--------------------------------------------------------------------------------

The information in this article applies to:
Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows 95


SUMMARY
This article demonstrates how to implement a RegRestoreKey() function under Windows 95. This process involves several steps that require that you delete both the descendant subkeys and their values before you add the new subkeys and values.



MORE INFORMATION


Step 1: Initialization
To avoid a conflict between two applications that are trying to restore to the same registry key, use a mutex to prevent a second restore from occurring before the first one is completed.


   hMutex = CreateMutex( NULL, TRUE, REG_RESTORE);
   if ( !hMutex )
     return GetLastError();

   if (GetLastError() == ERROR_ALREADY_EXISTS)
   {
     if ((lRet=WaitForSingleObject(hMutex, INFINITE)) != WAIT_OBJECT_0)
         return lRet;
   }


Step 2: Delete Key Values
First you must delete the specified key's values. To do this, you must first enumerate the key values using RegEnumValue() and then delete them using RegDeleteValue().


   for (;;)
   {
     cbValue = REGSTR_MAX_VALUE_LENGTH;        // reset value length
     // remove this keys old values
     lRet  = RegEnumValue(hStartKey,   // handle of key to query
                0,     // index of value to query
                szValue,  // address of buffer for value string
                &cbValue, // address for size of value buffer
                NULL,     // reserved
                NULL,     // address of buffer for type code
                NULL,     // address of buffer for value data
                NULL     // address for size of data buffer
            );

     if ( ERROR_NO_MORE_ITEMS == lRet ) // all values deleted
     {
            lRet = ERROR_SUCCESS;
            break;
     }
     else if ( ERROR_SUCCESS == lRet )
     {
        if ((lRet = RegDeleteValue(
                              hStartKey,  // handle of key
                              szValue     // address of value name
                              )) != ERROR_SUCCESS)
                return lRet;
     }
     else
       return lRet;
   }  // end for loop



Step 3: Delete Subkeys
After you have deleted the current values you must delete the specified key's subkeys. Do this by enumerating the subkeys using RegEnumKeyEx and then deleting them using RegDeleteKey. In Windows 95, this deletes the current subkeys and all descendant subkeys. After you have completed the deletion, you can add the new subkeys and values.


   // delete all subkeys
   for(;;)
   {

       dwSubKeyLength = MAX_PATH;
       lRet=RegEnumKeyEx(
                   hStartKey,
                   0,
                   szSubKey,
                       &dwSubKeyLength,
                        NULL,
                        NULL,
                        NULL,
                        NULL
                        );
        if(lRet == ERROR_NO_MORE_ITEMS)
        {
            lRet = ERROR_SUCCESS;
            break;
        }
        else if(lRet == ERROR_SUCCESS)
        {
          if((lRet = RegDeleteKey(hStartKey, szSubKey)) != ERROR_SUCCESS)
              return lRet;
        }
        else
            return lRet;
   }  // end for loop



Step 4: Restore New Subkeys
To add the new values and subkeys, use RegLoadKey to duplicate and load the registry hive into the registry. Once the hive has been loaded, all values and subkeys are enumerated and copied to the specified restore key.


   // Load new hive
   lRet = RegLoadKey(HKEY_USERS, "TEMP_HIVE", szNewHive );

   DWORD RegCreateTree(HKEY hTree, HKEY hReplacement)
   {
     DWORD   cdwClass, dwSubKeyLength, dwDisposition, dwKeyIndex = 0;
     LPTSTR  pSubKey = NULL;
     TCHAR   szSubKey[REGSTR_MAX_VALUE_LENGTH]; // this should be dynamic.
     TCHAR   szClass[REGSTR_MAX_VALUE_LENGTH]; // this should be dynamic.
     HKEY    hNewKey, hKey;
     DWORD   lRet;

     for(;;)
     {
       dwSubKeyLength = REGSTR_MAX_VALUE_LENGTH;
       cdwClass = REGSTR_MAX_VALUE_LENGTH;
       lRet=RegEnumKeyEx(
                   hReplacement,
                   dwKeyIndex,
                   szSubKey,
                   &dwSubKeyLength,
                   NULL,
                   szClass,
                   &cdwClass,
                   NULL
                   );
       if(lRet == ERROR_NO_MORE_ITEMS)
       {
          lRet = ERROR_SUCCESS;
          break;
       }
       else if(lRet == ERROR_SUCCESS)
       {
          if ((lRet=RegCreateKeyEx(hTree, szSubKey,0, szClass,
                      REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
                      &hNewKey, &dwDisposition)) != ERROR_SUCCESS )
                break;
          else  // add key values and recurse
          {
            if ((lRet=RegCreateValues( hReplacement, szSubKey, hNewKey))
                    != ERROR_SUCCESS)
            {
                CloseHandle(hNewKey);
                break;
            }
            if ( (lRet=RegOpenKeyEx(hReplacement, szSubKey, 0,
                              KEY_ALL_ACCESS, &hKey )) == ERROR_SUCCESS )
            {
                lRet=RegCreateTree(hNewKey, hKey);
                CloseHandle(hKey);
                CloseHandle(hNewKey);
                if ( lRet != ERROR_SUCCESS )
                        break;
             }
             else
             {
                CloseHandle(hNewKey);
                break;
             }
          }
       }
       else
         break;
       ++dwKeyIndex;
     } // end for loop
    return lRet;
   }  // end RegCreateTree function

   DWORD RegCreateValues(HKEY hReplacement, LPCTSTR lpSubKey, HKEY hNewKey)
   {
     DWORD    cbValue, dwSubKeyIndex=0, dwType, cdwBuf;
     DWORD    dwValues, cbMaxValueData, i;
     LPTSTR   pSubKey = NULL;
     TCHAR    szValue[REGSTR_MAX_VALUE_LENGTH]; // this should be dynamic.
     HKEY     hKey;
     DWORD    lRet = ERROR_SUCCESS;
     LPBYTE   pBuf;

     if ((lRet=RegOpenKeyEx(hReplacement, lpSubKey, 0,
                    KEY_ALL_ACCESS, &hKey )) == ERROR_SUCCESS)
     {
        if ((lRet=RegQueryInfoKey (hKey, NULL, NULL, NULL, NULL, NULL,
                       NULL, &dwValues,NULL, &cbMaxValueData,
                       NULL, NULL)) == ERROR_SUCCESS)
        {
            if ( dwValues )
            {
                if ((pBuf=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                              cbMaxValueData )))
                {
                    for (i = 0; i < dwValues ; i++)
                    {
                       //  get values to create
                       cbValue = REGSTR_MAX_VALUE_LENGTH;
                       cdwBuf = cbMaxValueData;
                       lRet = RegEnumValue(
                                hKey,     // handle of key to query
                                i,        // index of value to query
                                szValue,  // buffer for value string
                                &cbValue, // address for size of buffer
                                NULL,     // reserved
                                &dwType,  // buffer address for type code
                                pBuf,   // address of buffer for value data
                                &cdwBuf   // address for size of buffer
                                );

                        if ( ERROR_SUCCESS == lRet )
                        {
                            if( (lRet=RegSetValueEx(hNewKey, szValue, 0,
                                       dwType, (CONST BYTE *)pBuf,
                                       cdwBuf))!= ERROR_SUCCESS)
                                break;
                        }
                        else
                            break;

                    }  // for loop
                }
                HeapFree(GetProcessHeap(), 0, pBuf);
            }
        }
        CloseHandle(hKey);
     }
     return lRet;
   }  // end of RegCreateValues function



Step 5: Clean Up
When all operations have been completed, the registry hive that was loaded is unloaded and the mutex is released to allow other restores to begin.


   // UnLoad user hive
   lRet = RegUnLoadKey(HKEY_USERS, "TEMP_HIVE");
   ReleaseMutex(hMutex);

 
0
EKarimAuthor Commented:
Hi Lischke,
OK, I got the idea. Is there an easy way to use the system function? I just want to run the *.reg file as if you double click the *.reg file. There is no user involves in the download process. Thanks,

EKarim
0
dewsburyCommented:
why not...

#include <stdlib.h>
#include <process.h>

system( "regsvr32 /s  myfile.reg" );
0
EKarimAuthor Commented:
I have a problem with regsvr32. When I run regsvr32 filename.reg, I received the following error message:

Load Library(filename.reg) Failed. Get Last Error =0X0000001F.

I went ahead and check the following files in the Windows/System directory:

mfc30.dll(3.2.000)
mfc40.dll(4.1.6140)
mvcrt20.dll(2.11.000)
mvcrt40.dll(4.11.000)
olepro32.dll(5.0.4275)
regsvr32.exe(5.00.1641.1)

Could anyone explain to me what the problem is?
0
EKarimAuthor Commented:
I have a problem with regsvr32. When I run regsvr32 filename.reg, I received the following error message:

Load Library(filename.reg) Failed. Get Last Error =0X0000001F.

I went ahead and check the following files in the Windows/System directory:

mfc30.dll(3.2.000)
mfc40.dll(4.1.6140)
mvcrt20.dll(2.11.000)
mvcrt40.dll(4.11.000)
olepro32.dll(5.0.4275)
regsvr32.exe(5.00.1641.1)

Could anyone explain to me what the problem is?
0
LischkeCommented:
regsvr32 is a program to register COM classes not to import reg files. You need to call "regedit /s yourfile.reg"

Ciao, Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LischkeCommented:
EKarim, are you still listening? What about this question?

Ciao, Mike
0
EKarimAuthor Commented:
Mike,
"regedit /s filename.reg" will wook for me. Thanks for your help.

EKarim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.