Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * elevate (sudo for windows)
- * @version 0.2
- * @filename elevate.c
- * @author Christof Germishuizen <christofg@gmail.com>
- *
- * Copyright (C) 2009 Christof Germishuizen <christofg@gmail.com>
- * copying and distribution of this file, with or without modification,
- * are permitted in any medium without royalty provided the copyright
- * notice and this notice are preserved. This file is offered as-is,
- * without any warranty.
- */
- #ifndef UNICODE
- #define UNICODE
- #endif
- #include <windows.h>
- #include <shellapi.h>
- #include <wchar.h>
- LPWSTR *mergestrings(LPWSTR *left, LPCWSTR right)
- {
- size_t size = ( 1 + lstrlen(*left) + lstrlen(right) ) * sizeof(LPWSTR*);
- if ( *left ) {
- LPWSTR leftcopy = _wcsdup(*left);
- *left = (LPWSTR)realloc(*left, size);
- *left = lstrcpy(*left, leftcopy);
- *left = lstrcat(*left, right);
- free( leftcopy );
- } else *left = _wcsdup(right);
- return left;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpcommand, int nShowCmd)
- {
- DWORD result = 0x2a; // Life, the Universe, Everything.
- LPWSTR *argv = NULL;
- int argc = 0;
- if ( argv = CommandLineToArgvW(GetCommandLineW(), &argc) ) {
- if ( argc < 2 ) {
- LPWSTR usagemsg = NULL;
- usagemsg = *mergestrings(&usagemsg, argv[0]);
- usagemsg = *mergestrings(&usagemsg, TEXT(" <command_to_run> [arguments]"));
- MessageBox(NULL, usagemsg, TEXT("Usage:"), MB_OK | MB_ICONEXCLAMATION );
- LocalFree( argv );
- free( usagemsg );
- return ERROR_BAD_ARGUMENTS;
- // argv is populated with at least a <command_to_run>
- } else {
- LPWSTR command = argv[1];
- LPWSTR arguments = NULL;
- int c;
- for ( c = 2; c < argc; c++ ) {
- arguments = *mergestrings(&arguments, argv[c]);
- arguments = *mergestrings(&arguments, TEXT(" "));
- }
- result = (DWORD)ShellExecute(NULL, TEXT("runas"), command, arguments, NULL, SW_SHOWNORMAL);
- LocalFree( argv );
- if ( arguments ) free( arguments );
- // translate ShellExecute()'s wacky return codes to something more meaningful.
- switch ( result )
- {
- case 0: /* "The operating system is out of memory or resources." */
- result = ERROR_OUTOFMEMORY;
- break;
- case 27: /* SE_ERR_ASSOCINCOMPLETE */
- case 31: /* SE_ERR_NOASSOC */
- result = ERROR_NO_ASSOCIATION;
- break;
- case 28: /* SE_ERR_DDETIMEOUT */
- case 29: /* SE_ERR_DDEFAIL */
- case 30: /* SE_ERR_DDEBUSY */
- result = ERROR_DDE_FAIL;
- break;
- case 32: /* SE_ERR_DLLNOTFOUND */
- result = ERROR_DLL_NOT_FOUND;
- break;
- // ShellExecute() succeeded if it returns > 32.
- default:
- if ( result > 32 ) result = 0x2a;
- }
- }
- // CommandLineToArgvW() failed in a strange way. Let's get its error code.
- } else result = GetLastError();
- if (result != 0x2a) {
- LPWSTR errormsg = NULL;
- FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
- NULL, result, 0, (LPWSTR)&errormsg, 0, NULL);
- MessageBox(NULL, errormsg, TEXT("Error:"), MB_OK | MB_ICONERROR);
- LocalFree( errormsg );
- return result;
- // It all went fine. :)
- } else return NO_ERROR;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement