home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / RunModule / src / ExecuteProcess.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-06-18  |  8.9 KB  |  271 lines

  1. // ExecuteProcess.cpp: implementation of the CExecuteProcess class.
  2. //
  3. /*
  4. Copyright 2001 Anish Mistry. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8.  
  9.    1. Redistributions of source code must retain the above copyright notice, 
  10.    this list of conditions and the following disclaimer.
  11.    2. Redistributions in binary form must reproduce the above copyright notice,
  12.    this list of conditions and the following disclaimer in the documentation 
  13.    and/or other materials provided with the distribution.
  14.  
  15. THIS SOFTWARE IS PROVIDED BY ANISH MISTRY ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  16. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  17. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS 
  18. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  20. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  22. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  
  25. The views and conclusions contained in the software and documentation are those
  26. of the authors and should not be interpreted as representing official policies,
  27. either expressed or implied, of Anish Mistry or AM Productions.
  28.  
  29. * Variation of the FreeBSD License. http://www.freebsd.org/copyright/freebsd-license.html
  30. */
  31. //////////////////////////////////////////////////////////////////////
  32.  
  33. #include "stdafx.h"
  34. #include "ExecuteProcess.h"
  35.  
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39.  
  40. CExecuteProcess::CExecuteProcess()
  41. {
  42.  
  43. }
  44.  
  45. CExecuteProcess::~CExecuteProcess()
  46. {
  47.  
  48. }
  49.  
  50. int CExecuteProcess::RunProgram(char *pObject,char *pCommand)
  51. {// begin RunProgram
  52.     if(lstrcmp(pObject,"") == 0)
  53.         return ERROR_FILE_NOT_FOUND;
  54.     if(lstrcmp(pCommand,"") == 0)
  55.     {
  56.         lstrcpy(pCommand,"open");
  57.     }
  58.  
  59.     // declarations
  60.     int nRtnVal = 33;
  61.     // deterimine object  type
  62.     unsigned long int nObjectAttribs = GetFileAttributes(pObject);
  63.     if(nObjectAttribs == 0xFFFFFFFF)
  64.     {// begin fail
  65.         // could be a file in the windows folder
  66.         nRtnVal = WinExec(pObject,SW_SHOWNORMAL);
  67.         if(nRtnVal > 31)
  68.             nRtnVal = 33;
  69.         // could be a URL check for a http://
  70.         if(strstr(pObject,"http://") || strstr(pObject,"mailto:"))
  71.         {// begin launch URL
  72.             nRtnVal = (int)ShellExecute(NULL,pCommand,pObject,NULL,NULL,SW_SHOWNORMAL);
  73.         }// end launch URL
  74.         if(strncmp(pObject,"\\\\",2) == 0)
  75.         {// begin launch network path
  76.             char pUNCPath[MAX_PATH] = {NULL};
  77.             if(lstrcmpi(pCommand,"explore") == 0)
  78.                 wsprintf(pUNCPath,"explorer.exe /e,,\"%hs\"",pObject);
  79.             else
  80.                 wsprintf(pUNCPath,"explorer.exe /n,,\"%hs\"",pObject);
  81.             nRtnVal = (int)WinExec(pUNCPath,SW_SHOWNORMAL);
  82.         }// end launch network path
  83.         // could be a clsid
  84.         else if(lstrlen(pObject) > 0 && pObject[0] == '{')
  85.         {// begin clsid
  86.             char pCLSID[MAX_PATH] = {NULL};
  87.             wsprintf(pCLSID,"explorer.exe /root,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::%hs",pObject);
  88.             nRtnVal = WinExec(pCLSID,SW_SHOWNORMAL);
  89.         }// end clsid
  90.  
  91.     }// end fail
  92.     else if(nObjectAttribs & FILE_ATTRIBUTE_DIRECTORY)
  93.     {// begin directory
  94.         char pDir[MAX_PATH] = {NULL};
  95.         if(lstrcmpi(pCommand,"explore") == 0)
  96.         {
  97.             wsprintf(pDir,"explorer.exe /e,,\"%hs\"",pObject);
  98.         }
  99.         else if(lstrcmpi(pCommand,"delete") == 0)
  100.         {// begin delete
  101.             if(!CStdFile::Delete(pObject))
  102.                  nRtnVal = 0;
  103.             else
  104.                  nRtnVal = 33;
  105.             return nRtnVal;
  106.         }// end delete
  107.         else if(lstrcmpi(pCommand,"rename") == 0)
  108.         {// begin rename
  109.             // find and replace the '=' to NULL
  110.             char *pRenameString = pObject;
  111.             for(int j = 0;pRenameString[j] != NULL;j++)
  112.                 if(pRenameString[j] == '=')
  113.                 {
  114.                     pRenameString[j] = '\0';
  115.                     pRenameString += j+1;
  116.                     break;
  117.                 }
  118.             if(CStdFile::Rename(pObject,pRenameString+1))
  119.                 return 33;    // sucessful
  120.             return 0;    // failed
  121.         }// end rename
  122.         else if(lstrcmpi(pCommand,"open") == 0)
  123.             wsprintf(pDir,"explorer.exe \"%hs\"",pObject);
  124. //            wsprintf(pDir,"explorer.exe /n,\"%hs\"",pObject);
  125.         else
  126.             return (int)ShellExecute(NULL,pCommand,pObject,NULL,NULL,SW_SHOWNORMAL);
  127.         nRtnVal = (int)WinExec(pDir,SW_SHOWNORMAL);
  128.         if(nRtnVal > 31)
  129.             nRtnVal = 33;
  130.     }// end directory
  131.     else
  132.     {// begin file
  133.         // set the object's current directory to the one that it is being run from
  134.         char pCurrentDirectory[MAX_PATH] = {NULL};
  135.         GetFileDirectory(pObject,pCurrentDirectory);
  136.         if(lstrcmp("",pCurrentDirectory) != 0)
  137.             SetCurrentDirectory(pCurrentDirectory);
  138.         if(lstrcmpi(pCommand,"delete") == 0)
  139.         {// begin delete
  140.             SetAppDirectoryAsCurrent();    // since this isn't needed for deleting
  141.             if(!CStdFile::Delete(pObject))
  142.                  nRtnVal = 0;
  143.             else
  144.                  nRtnVal = 33;
  145.             return nRtnVal;
  146.         }// end delete
  147.         else if(lstrcmpi(pCommand,"rename") == 0)
  148.         {
  149.             SetAppDirectoryAsCurrent();    // since this isn't needed for renaming
  150.             // find and replace the '=' to NULL
  151.             char *pRenameString = pObject;
  152.             for(int j = 0;pRenameString[j] != NULL;j++)
  153.                 if(pRenameString[j] == '=')
  154.                 {
  155.                     pRenameString[j] = '\0';
  156.                     pRenameString += j+1;
  157.                     break;
  158.                 }
  159.             if(CStdFile::Rename(pObject,pRenameString+1))
  160.                 return 33;    // sucessful
  161.             return 0;    // failed
  162.         }
  163.         else if(lstrcmpi(pCommand,"open") == 0)
  164.         {// begin open
  165.             if(WinExec(pObject,SW_SHOWNORMAL) < 32)
  166.             {// begin fail
  167.                 nRtnVal = (int)ShellExecute(NULL,pCommand,pObject,NULL,NULL,SW_SHOWNORMAL);
  168.                 if(nRtnVal == 31)
  169.                 {// begin find program
  170.                     char pFormattedString[MAX_PATH] = {NULL};
  171.                     char pNewObject[MAX_PATH] = {NULL};
  172.                     char pExtension[MAX_PATH] = {NULL};
  173.                     GetExtension(pObject,pExtension);
  174.                     GetDefaultApp(pExtension,pFormattedString);
  175.                     // insert pObject into program path
  176.                     FormatMessage( FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  177.                         pFormattedString,
  178.                         NULL,
  179.                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  180.                         pNewObject,
  181.                         MAX_PATH-1,
  182.                         &pObject);
  183.                     // run it
  184.                     nRtnVal = WinExec(pNewObject,SW_SHOWNORMAL);
  185.                     if(nRtnVal > 31)
  186.                         nRtnVal = 33;
  187.                     LocalFree(pNewObject);
  188.                 }// end find program
  189.             }// end fail
  190.         }// end open
  191.         else
  192.         {// begin default
  193.             nRtnVal = (int)ShellExecute(NULL,pCommand,pObject,NULL,NULL,SW_SHOWNORMAL);
  194.         }// end default
  195.         SetAppDirectoryAsCurrent();
  196.     }// end file
  197.     return nRtnVal;
  198. }// end RunProgram
  199.  
  200. const char * CExecuteProcess::GetExtension(const char *pFile,char *pBuffer)
  201. // returns the extension of a file including the '.'
  202. {// begin GetExtension
  203.     for(int i = lstrlen(pFile)-1;i >= 0;i--)
  204.     {// begin search
  205.         if(pFile[i] == '.')
  206.         {
  207.             const char *pStart = &pFile[i];
  208.             for(int j = 1;1;j++)
  209.             {
  210.                 if(!IsCharAlphaNumeric(pStart[j]))
  211.                     return lstrcpyn(pBuffer,pStart,j+1);
  212.             }
  213.         }
  214.     }// end search
  215.     return NULL;
  216. }// end GetExtension
  217.  
  218. const char * CExecuteProcess::GetDefaultApp(const char *pExtension, char *pBuffer)
  219. // returns a formatting string 'MyApp.exe "%1"'
  220. {// begin GetDefaultApp
  221.  
  222.     // open the CLASSES_ROOT section of registry
  223.     HKEY hKey = NULL;
  224.     char pSubKey[MAX_PATH] = {NULL};
  225.     unsigned long int nBufferSize = MAX_PATH-1;
  226.     unsigned long int nRegString = REG_SZ;
  227.     RegOpenKeyEx(HKEY_CLASSES_ROOT,pExtension,0,KEY_READ,&hKey);
  228.     // get the location of the program data
  229.     RegQueryValueEx(hKey,NULL/*Get the default value*/,0,&nRegString,(unsigned char *)pSubKey,&nBufferSize);
  230.     // close the key
  231.     RegCloseKey(hKey);
  232.     if(lstrcmp(pSubKey,"") == 0)
  233.     {// begin no program
  234.         // exit
  235.         return NULL;
  236.     }// end no program
  237.     // open the other location
  238.     HKEY hSubKey = NULL;
  239.     char pFirstSubKey[MAX_PATH] = {NULL};
  240.     wsprintf(pFirstSubKey,"%hs\\shell",pSubKey);
  241.     RegOpenKeyEx(HKEY_CLASSES_ROOT,pFirstSubKey,0,KEY_READ,&hSubKey);
  242.     // get the location of the first app
  243.     char pFirstApp[MAX_PATH] = {NULL};
  244.     unsigned long int nFirstSize = MAX_PATH-1;
  245.     RegEnumKey(hSubKey,0,pFirstApp,nFirstSize);
  246.     RegCloseKey(hSubKey);
  247.     if(lstrcmp(pFirstApp,"") == 0)
  248.     {// begin no program
  249.         // exit
  250.         return NULL;
  251.     }// end no program
  252.     // now get the actual app
  253.     HKEY hProgramKey = NULL;
  254.     char pApp[MAX_PATH] = {NULL};
  255.     wsprintf(pApp,"%hs\\%hs\\command",pFirstSubKey,pFirstApp);
  256.     RegOpenKeyEx(HKEY_CLASSES_ROOT,pApp,0,KEY_READ,&hProgramKey);
  257.     // get the first app
  258.     unsigned long int nAppSize = MAX_PATH-1;
  259.     RegQueryValueEx(hProgramKey,NULL/*Get the default value*/,0,&nRegString,(unsigned char *)pApp,&nAppSize);
  260.     RegCloseKey(hProgramKey);
  261.     if(lstrcmp(pApp,"") == 0)
  262.     {// begin no program
  263.         // exit
  264.         return NULL;
  265.     }// end no program
  266.     // copy into the buffer
  267.     lstrcpy(pBuffer,pApp);
  268.     return pBuffer;
  269. }// end GetDefaultApp
  270.  
  271.