• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kcmdwrapper.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE libraries
00003 
00004     Copyright (c) 2007 Bernhard Loos <nhuh.put@web.de>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
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     // Instead of checking the OS version we might check the for presence of
00081     // the reg keys that enable delayed variable expansion by default - that
00082     // would also cover the pathological case of running cmd from win2k under
00083     // winNT. OTOH, who cares?
00084     GetVersionEx(&vi);
00085     _snwprintf(command, COMMAND_BUFFER_SIZE,
00086         (vi.dwMajorVersion >= 5 /* Win2k */) ?
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 

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal