KDECore
kcmdwrapper.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <windows.h>
00023 #include <shellapi.h>
00024 #include <wchar.h>
00025 #include <stdio.h>
00026
00027 void errorExit(LPWSTR lpszFunction)
00028 {
00029 WCHAR szBuf[100];
00030 LPVOID lpMsgBuf;
00031 DWORD dw = GetLastError();
00032
00033 FormatMessageW(
00034 FORMAT_MESSAGE_ALLOCATE_BUFFER |
00035 FORMAT_MESSAGE_FROM_SYSTEM,
00036 NULL,
00037 dw,
00038 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00039 (LPWSTR) &lpMsgBuf,
00040 0, NULL);
00041
00042 _snwprintf(szBuf, 100,
00043 L"kcmdwrapper: %s failed with error %d: %s",
00044 lpszFunction, dw, lpMsgBuf);
00045
00046 OutputDebugStringW(szBuf);
00047 LocalFree(lpMsgBuf);
00048 ExitProcess(-1);
00049 }
00050
00051 void printUsage(void)
00052 {
00053 wprintf(L"A Wrapper for cmd.exe\n\n");
00054 wprintf(L"calls shell /S /C argument with proper quoting\n");
00055 wprintf(L"Usage: kcmdwrapper shell argument");
00056 ExitProcess(-1);
00057 }
00058
00059 void printError(const char * p) {}
00060 #define COMMAND_BUFFER_SIZE 34000
00061
00062 int main(int argc, char **argv)
00063 {
00064 LPWSTR *argList;
00065 int nArgs;
00066 WCHAR command[COMMAND_BUFFER_SIZE];
00067 OSVERSIONINFO vi;
00068 STARTUPINFOW si;
00069 PROCESS_INFORMATION pi;
00070 DWORD exitCode;
00071
00072 argList = CommandLineToArgvW(GetCommandLineW(), &nArgs);
00073
00074 if (NULL == argList)
00075 errorExit(L"CommandLineToArgvW");
00076
00077 if (nArgs != 3)
00078 printUsage();
00079
00080
00081
00082
00083
00084 GetVersionEx(&vi);
00085 _snwprintf(command, COMMAND_BUFFER_SIZE,
00086 (vi.dwMajorVersion >= 5 ) ?
00087 L"/V:OFF /S /C \"%s\"" : L"/S /C \"%s\"",
00088 argList[2]);
00089
00090
00091 ZeroMemory( &si, sizeof(si) );
00092 si.cb = sizeof(si);
00093 ZeroMemory( &pi, sizeof(pi) );
00094
00095
00096 if (CreateProcessW(argList[1], command, NULL, NULL, true, 0, NULL, NULL, &si, &pi) == 0)
00097 errorExit(L"CreateProcessW");
00098
00099 CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
00100 CloseHandle(GetStdHandle(STD_ERROR_HANDLE));
00101 CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
00102
00103 if (WaitForSingleObject(pi.hProcess, INFINITE) == WAIT_FAILED)
00104 errorExit(L"WaitForSingleObject");
00105
00106 if (!GetExitCodeProcess(pi.hProcess, &exitCode))
00107 errorExit(L"WaitForSingleObject");
00108
00109 if (exitCode == STILL_ACTIVE )
00110 OutputDebugStringW(L"cmdwrapper: STILL_ACTIVE returned");
00111
00112 return exitCode;
00113 }
00114