home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / sharpdevelop / 087cSetup.exe / AddIns / vcppexec.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-20  |  3.0 KB  |  109 lines

  1. // vcppexec.cpp
  2. // Copyright (c) 2002 Christoph Wille
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  
  18. #include "windows.h"
  19. #include "Shlobj.h"
  20.  
  21. #using <mscorlib.dll>
  22. using namespace System;
  23. using namespace System::Runtime::InteropServices;
  24.  
  25. namespace HelperServices
  26. {
  27. __value public enum ShowWindowOption { normal=1, hidden };
  28.  
  29. __gc public class ExecuteDirect
  30. {
  31. public:
  32.    ExecuteDirect(){}
  33.     
  34. public:
  35.    long RunProgram(System::String *sCmdLine, ShowWindowOption option)
  36.    {
  37.     wchar_t __nogc* pszCmdLine = static_cast<wchar_t*>(Marshal::StringToHGlobalUni(sCmdLine).ToPointer());
  38.  
  39.     STARTUPINFOW startup = {0};
  40.     startup.cb = sizeof(startup);
  41.     if (ShowWindowOption::hidden == option)
  42.     {
  43.         startup.dwFlags = STARTF_USESHOWWINDOW;
  44.         startup.wShowWindow = SW_HIDE;
  45.     }
  46.  
  47.     PROCESS_INFORMATION process = {0};
  48.  
  49.     __try
  50.     { 
  51.       if (::CreateProcessW(NULL, pszCmdLine, NULL, NULL, FALSE,
  52.         CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS,
  53.         NULL, NULL, &startup, &process))
  54.       {
  55.         CloseHandle( process.hThread );
  56.         CloseHandle( process.hProcess );
  57.       }
  58.       else
  59.       {
  60.         DWORD dwError = ::GetLastError();
  61.         return (long)dwError;
  62.       }
  63.     }
  64.     __finally
  65.     {
  66.         Marshal::FreeHGlobal(pszCmdLine);
  67.     }
  68.     return 0;
  69.    }
  70.  
  71.    bool BrowseForFolder(System::String *sDialogTitle, long hwnd, System::String **sPath)
  72.    {
  73.        *sPath = S"";
  74.        BROWSEINFO bi = { 0 };
  75.        TCHAR path[MAX_PATH];
  76.        bi.hwndOwner = (HWND)hwnd;
  77.        char __nogc* pszDialogTitle = static_cast<char*>(Marshal::StringToHGlobalAnsi(sDialogTitle).ToPointer());
  78.        bi.lpszTitle = pszDialogTitle;
  79.        bi.pszDisplayName = path;
  80.  
  81.        LPITEMIDLIST pidl = ::SHBrowseForFolder ( &bi );
  82.        Marshal::FreeHGlobal(pszDialogTitle);
  83.        
  84.        if (NULL != pidl)
  85.        {
  86.           TCHAR szPathTranslated[MAX_PATH];
  87.           if (::SHGetPathFromIDList (pidl, szPathTranslated))
  88.           {
  89.             *sPath = Marshal::PtrToStringAnsi(szPathTranslated);
  90.           }
  91.  
  92.           // free memory used
  93.           IMalloc * lpMalloc = NULL;
  94.           if ( SUCCEEDED(::SHGetMalloc ( &lpMalloc)) )
  95.           {
  96.             lpMalloc->Free(pidl);
  97.             lpMalloc->Release();
  98.           }
  99.           return true;
  100.        }
  101.        else
  102.        {
  103.           // failed
  104.           return false;
  105.        }
  106.    }
  107. };
  108. };
  109.