Advertisement
Guest User

Christof

a guest
Jun 30th, 2010
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. /**
  2. * elevate (sudo for windows)
  3. * @version 0.2
  4. * @filename elevate.c
  5. * @author Christof Germishuizen <christofg@gmail.com>
  6. *
  7. * Copyright (C) 2009 Christof Germishuizen <christofg@gmail.com>
  8. *    copying and distribution of this file, with or without modification,
  9. *    are permitted in any medium without royalty provided the copyright
  10. *    notice and this notice are preserved.  This file is offered as-is,
  11. *    without any warranty.
  12. */
  13.  
  14. #ifndef UNICODE
  15. #define UNICODE
  16. #endif
  17. #include <windows.h>
  18. #include <shellapi.h>
  19. #include <wchar.h>
  20.  
  21. LPWSTR *mergestrings(LPWSTR *left, LPCWSTR right)
  22. {
  23.     size_t size = ( 1 + lstrlen(*left) + lstrlen(right) ) * sizeof(LPWSTR*);
  24.  
  25.     if ( *left ) {
  26.         LPWSTR leftcopy = _wcsdup(*left);
  27.         *left = (LPWSTR)realloc(*left, size);
  28.         *left = lstrcpy(*left, leftcopy);
  29.         *left = lstrcat(*left, right);
  30.         free( leftcopy );
  31.    
  32.     } else *left = _wcsdup(right);
  33.    
  34.     return left;
  35. }
  36.  
  37. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpcommand, int nShowCmd)
  38. {
  39.     DWORD result = 0x2a; // Life, the Universe, Everything.
  40.     LPWSTR *argv = NULL;
  41.     int argc = 0;
  42.    
  43.     if ( argv = CommandLineToArgvW(GetCommandLineW(), &argc) ) {
  44.  
  45.         if ( argc < 2 ) {
  46.  
  47.             LPWSTR usagemsg = NULL;
  48.             usagemsg = *mergestrings(&usagemsg, argv[0]);
  49.             usagemsg = *mergestrings(&usagemsg, TEXT(" <command_to_run> [arguments]"));
  50.             MessageBox(NULL, usagemsg, TEXT("Usage:"), MB_OK | MB_ICONEXCLAMATION );
  51.            
  52.             LocalFree( argv );
  53.             free( usagemsg );
  54.            
  55.             return ERROR_BAD_ARGUMENTS;
  56.  
  57.         // argv is populated with at least a <command_to_run>
  58.         } else {
  59.  
  60.             LPWSTR command = argv[1];
  61.             LPWSTR arguments = NULL;
  62.             int c;
  63.  
  64.             for ( c = 2; c < argc; c++ ) {
  65.                 arguments = *mergestrings(&arguments, argv[c]);
  66.                 arguments = *mergestrings(&arguments, TEXT(" "));
  67.             }
  68.            
  69.             result = (DWORD)ShellExecute(NULL, TEXT("runas"), command, arguments, NULL, SW_SHOWNORMAL);
  70.  
  71.             LocalFree( argv );
  72.             if ( arguments ) free( arguments );
  73.            
  74.             // translate ShellExecute()'s wacky return codes to something more meaningful.
  75.             switch ( result )
  76.             {
  77.                 case 0: /* "The operating system is out of memory or resources." */
  78.                     result = ERROR_OUTOFMEMORY;
  79.                     break;
  80.            
  81.                 case 27: /* SE_ERR_ASSOCINCOMPLETE */
  82.                 case 31: /* SE_ERR_NOASSOC */
  83.                     result = ERROR_NO_ASSOCIATION;
  84.                     break;
  85.            
  86.                 case 28: /* SE_ERR_DDETIMEOUT */
  87.                 case 29: /* SE_ERR_DDEFAIL */
  88.                 case 30: /* SE_ERR_DDEBUSY */
  89.                     result = ERROR_DDE_FAIL;
  90.                     break;
  91.            
  92.                 case 32: /* SE_ERR_DLLNOTFOUND */
  93.                     result = ERROR_DLL_NOT_FOUND;
  94.                     break;
  95.            
  96.                 // ShellExecute() succeeded if it returns > 32.
  97.                 default:
  98.                     if ( result > 32 ) result = 0x2a;
  99.             }
  100.         }
  101.     // CommandLineToArgvW() failed in a strange way. Let's get its error code.
  102.     } else result = GetLastError();
  103.  
  104.     if (result != 0x2a) {
  105.  
  106.         LPWSTR errormsg = NULL;
  107.         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  108.                       NULL, result, 0, (LPWSTR)&errormsg, 0, NULL);
  109.  
  110.         MessageBox(NULL, errormsg, TEXT("Error:"), MB_OK | MB_ICONERROR);
  111.         LocalFree( errormsg );
  112.  
  113.         return result;
  114.  
  115.     // It all went fine. :)
  116.     }  else return NO_ERROR;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement