home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_DAO.SDK / DISK4 / DAOSDK.1 / regadll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-08  |  5.0 KB  |  235 lines

  1. //
  2. //
  3. //    File:        RegADLL.c
  4. //
  5. //    Purpose:    Call DLLRegisterServer/UnRegisterServer in a 
  6. //                specified DLL.
  7. //                Needed as a buffer between InstallShield and the
  8. //                DLL itself
  9. //
  10. //
  11.  
  12. #include  <windows.h>
  13. #include  <assert.h>
  14. #include  <VER.H>
  15. #include  "RegADLL.h"
  16.  
  17. BOOL WINAPI LibMain ( HINSTANCE hInstDLL, DWORD dwReason, LPVOID lpvReserved )
  18. {
  19.         return (TRUE);
  20.         UNREFERENCED_PARAMETER( hInstDLL );
  21.         UNREFERENCED_PARAMETER( dwReason );
  22.         UNREFERENCED_PARAMETER( lpvReserved );
  23. }
  24.  
  25.  
  26. INT WINAPI RegisterADLL( HWND hwndMain, LPLONG lplValue, LPSTR pszPath )
  27. {
  28.     HINSTANCE    hModule;
  29.     CTLREGPROC    DLLRegisterServer;
  30.     HRESULT        regResult;
  31.     BOOL        bResult = FALSE;
  32.  
  33.     //Gotta have good pointers
  34.     assert(pszPath != NULL) ;
  35.     assert(lplValue != NULL) ;
  36.     
  37.     //Load the DLL into memory
  38.     hModule = LoadLibrary(pszPath) ;
  39.     
  40.     if (hModule == NULL) // Load failed, couldn't get the DLL
  41.         {
  42.         *lplValue = 0L;
  43.         MessageBox(NULL, "Couldn't find DLL", pszPath, MB_ICONEXCLAMATION);
  44.         return 1;
  45.         }
  46.             
  47.     //Get a pointer to the Register function
  48.     DLLRegisterServer = (CTLREGPROC)GetProcAddress(hModule,"DllRegisterServer" ) ;
  49.     
  50.     if (DLLRegisterServer != NULL)
  51.         {
  52.         regResult = DLLRegisterServer() ;
  53.         bResult = (regResult == NOERROR) ; 
  54.         }        
  55.     else
  56.         {
  57.         MessageBox(NULL, "Missing server registration function", pszPath, MB_ICONEXCLAMATION);
  58.         }
  59.     FreeLibrary(hModule) ;          
  60.  
  61.     //Everything went OK?
  62.     *lplValue = bResult ? 1L:0L;
  63.     
  64.  
  65.     return 1;    
  66. }
  67.  
  68.  
  69. INT WINAPI UnRegisterADLL( HWND hwndMain, LPLONG lplValue, LPSTR pszPath )
  70. {
  71.     HINSTANCE    hModule;
  72.     CTLREGPROC    DLLUnregisterServer;
  73.     HRESULT        regResult;
  74.     BOOL        bResult = FALSE;
  75.     CHAR        szBuf[256];
  76.  
  77.     //Gotta have good pointers
  78.     assert(pszPath != NULL) ;
  79.     assert(lplValue != NULL) ;
  80.     
  81.     //Load the DLL into memory
  82.     hModule = LoadLibrary((LPCSTR)pszPath) ;
  83.     
  84.     if (hModule == NULL) // Load failed, couldn't get the DLL
  85.         {
  86.         *lplValue = 0L;
  87.         MessageBox(NULL, "Can't find DLL", pszPath, MB_ICONEXCLAMATION);
  88.         return 1;
  89.         }
  90.             
  91.     //Get a pointer to the UNRegister function
  92.     DLLUnregisterServer = (CTLREGPROC)GetProcAddress(hModule,"DllUnregisterServer" ) ;
  93.             
  94.     if (DLLUnregisterServer != NULL)
  95.         {
  96.         regResult = DLLUnregisterServer() ;
  97.         bResult = (regResult == NOERROR) ; 
  98.         }        
  99.     else
  100.         {
  101.         MessageBox(NULL, "Missing server Unregistration function", pszPath, MB_ICONEXCLAMATION);
  102.         }
  103.     FreeLibrary(hModule) ;          
  104.  
  105.     //Everything went OK?
  106.     *lplValue = bResult ? 1L:0L;
  107.     
  108.     if (!bResult)
  109.         {
  110.         wsprintf(szBuf, "Unregistration failed - DLLUnregisterServer returned %d", regResult);
  111.         MessageBox(NULL, szBuf, pszPath, MB_OK);
  112.         }
  113.     
  114.     return 1;    
  115.  
  116.  
  117. //
  118. // Opens the specified DLL to determine if 
  119. // if the DLL is currently being used by another process
  120. // 
  121.  
  122. BOOL WINAPI IsDLLRunning( HWND hwndMain, LPLONG lplValue, LPSTR pszPath )
  123. {
  124.     HANDLE hFile;
  125.     
  126.     assert(pszPath != NULL);
  127.  
  128.     //Try to open the file - exclusively. Assume that a failure
  129.     //indicates that the file is currently running
  130.     hFile =  CreateFile(
  131.         pszPath,    // address of name of the file 
  132.         GENERIC_READ | GENERIC_WRITE,    // access (read-write) mode 
  133.         0,    // share mode (none)
  134.         NULL,    // address of security descriptor 
  135.         OPEN_EXISTING,    // how to create 
  136.         FILE_ATTRIBUTE_NORMAL,    // file attributes 
  137.         NULL     // handle of file with attributes to copy  
  138.            );    
  139.      
  140.      //If couldn't get a handle...
  141.      if(hFile == INVALID_HANDLE_VALUE)
  142.          {
  143.         //If the file exists then it must be locked by another process
  144.         if(GetFileAttributes(pszPath) != 0xFFFFFFFF)
  145.             {
  146.             CloseHandle(hFile);
  147.             *lplValue = 1L;
  148.             return TRUE;
  149.             }
  150.         }
  151.  
  152.     //It's not in use
  153.     CloseHandle(hFile);
  154.     *lplValue = 0L;
  155.     return FALSE;
  156. }
  157.  
  158.  
  159. //
  160. // Runs the specified executable
  161. // 
  162. // 
  163.  
  164. BOOL WINAPI RunProcess( HWND hwndMain, LPLONG lplValue, LPSTR pszPath )
  165. {
  166.     BOOL                bLaunched;
  167.     PROCESS_INFORMATION    pi;
  168.     STARTUPINFO            si;    
  169.  
  170.     assert(pszPath != NULL);
  171.  
  172.     //Fill in the startup info
  173.     si.cb = sizeof(si);
  174.     si.lpReserved = NULL;
  175.     si.lpDesktop = NULL;
  176.     si.lpTitle = NULL;
  177.     si.dwX = 0;
  178.     si.dwY = 0;
  179.     si.dwXSize = 0;
  180.     si.dwYSize = 0;
  181.     si.dwXCountChars = 0;
  182.     si.dwYCountChars = 0;
  183.     si.dwFillAttribute = 0;
  184.     si.dwFlags = STARTF_FORCEOFFFEEDBACK;
  185.     si.wShowWindow = 0;
  186.     si.cbReserved2 = 0;
  187.     si.lpReserved2 = NULL;
  188.     si.hStdInput = 0;
  189.     si.hStdOutput = 0;
  190.     si.hStdError = 0;
  191.     
  192.     //Run the specified process
  193.     bLaunched = CreateProcess(pszPath,    // pointer to name of executable module 
  194.             NULL,
  195.             NULL,
  196.             NULL,
  197.             FALSE,    
  198.             CREATE_DEFAULT_ERROR_MODE | HIGH_PRIORITY_CLASS,
  199.             NULL,
  200.             NULL,            
  201.             &si,
  202.             &pi);
  203.  
  204.     if(bLaunched)
  205.         {
  206.         //Give the process 10 seconds to complete
  207.         WaitForInputIdle(pi.hProcess, 10000);
  208.         }
  209.  
  210.     return bLaunched;
  211. }
  212.  
  213.  
  214.  
  215. //
  216. // Is this Win95 J (japan)
  217. // 
  218. // 
  219. #define    MS_LANGID_JPN    0x0411
  220.  
  221. BOOL WINAPI IsWinJ( HWND hwndMain, LPLONG lplValue, LPSTR pszPath )
  222. {
  223.  
  224. if(GetSystemDefaultLangID() == MS_LANGID_JPN)
  225.     {
  226.     *lplValue = 1L;
  227.     return TRUE;
  228.     }
  229.  
  230. *lplValue = 0L;
  231. return FALSE;
  232.  
  233. }
  234.