home *** CD-ROM | disk | FTP | other *** search
- // vcppexec.cpp
- // Copyright (c) 2002 Christoph Wille
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
- #include "windows.h"
- #include "Shlobj.h"
-
- #using <mscorlib.dll>
- using namespace System;
- using namespace System::Runtime::InteropServices;
-
- namespace HelperServices
- {
- __value public enum ShowWindowOption { normal=1, hidden };
-
- __gc public class ExecuteDirect
- {
- public:
- ExecuteDirect(){}
-
- public:
- long RunProgram(System::String *sCmdLine, ShowWindowOption option)
- {
- wchar_t __nogc* pszCmdLine = static_cast<wchar_t*>(Marshal::StringToHGlobalUni(sCmdLine).ToPointer());
-
- STARTUPINFOW startup = {0};
- startup.cb = sizeof(startup);
- if (ShowWindowOption::hidden == option)
- {
- startup.dwFlags = STARTF_USESHOWWINDOW;
- startup.wShowWindow = SW_HIDE;
- }
-
- PROCESS_INFORMATION process = {0};
-
- __try
- {
- if (::CreateProcessW(NULL, pszCmdLine, NULL, NULL, FALSE,
- CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS,
- NULL, NULL, &startup, &process))
- {
- CloseHandle( process.hThread );
- CloseHandle( process.hProcess );
- }
- else
- {
- DWORD dwError = ::GetLastError();
- return (long)dwError;
- }
- }
- __finally
- {
- Marshal::FreeHGlobal(pszCmdLine);
- }
- return 0;
- }
-
- bool BrowseForFolder(System::String *sDialogTitle, long hwnd, System::String **sPath)
- {
- *sPath = S"";
- BROWSEINFO bi = { 0 };
- TCHAR path[MAX_PATH];
- bi.hwndOwner = (HWND)hwnd;
- char __nogc* pszDialogTitle = static_cast<char*>(Marshal::StringToHGlobalAnsi(sDialogTitle).ToPointer());
- bi.lpszTitle = pszDialogTitle;
- bi.pszDisplayName = path;
-
- LPITEMIDLIST pidl = ::SHBrowseForFolder ( &bi );
- Marshal::FreeHGlobal(pszDialogTitle);
-
- if (NULL != pidl)
- {
- TCHAR szPathTranslated[MAX_PATH];
- if (::SHGetPathFromIDList (pidl, szPathTranslated))
- {
- *sPath = Marshal::PtrToStringAnsi(szPathTranslated);
- }
-
- // free memory used
- IMalloc * lpMalloc = NULL;
- if ( SUCCEEDED(::SHGetMalloc ( &lpMalloc)) )
- {
- lpMalloc->Free(pidl);
- lpMalloc->Release();
- }
- return true;
- }
- else
- {
- // failed
- return false;
- }
- }
- };
- };
-