Advertisement
Kitomas

kit_main.cpp as of 2023-01-05

Jan 5th, 2024
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include "_kit_misc.hpp"
  2.  
  3.  
  4. HINSTANCE _w32_hThisInst = nullptr;
  5. HINSTANCE _w32_hPrevInst = nullptr;
  6. LPSTR     _w32_lpszArg   = nullptr;
  7. int       _w32_nCmdShow  = SW_HIDE;
  8.  
  9.  
  10.  
  11.  
  12. //get first and last char of string, excluding leading or trailing whitespace
  13.  //(whitespace as in ' '; i'm not going to bother with '\t', '\n', etc.)
  14. static inline char* _getFirstCharPtr(char* start){
  15.   if(*start == 0) return start;
  16.   while(*start == ' ') ++start;
  17.   return start;
  18. }
  19.  
  20. static inline char* _getLastCharPtr(char* start){
  21.   if(*start == 0) return start;
  22.   char* end = start;
  23.   while(*(end+1) != 0 ) ++end; //go to null terminator
  24.   while(*end == ' ') --end; //go back until a non-whitespace char is found
  25.   return end;
  26. }
  27.  
  28.  
  29.  
  30.  
  31. extern int main(int argc, char** argv);
  32.  
  33.  
  34. int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
  35.                    LPSTR     lpszArg,   int       nCmdShow)
  36. {
  37.   //assign some global values
  38.   _w32_hThisInst = hThisInst;
  39.   _w32_hPrevInst = hPrevInst;
  40.   _w32_lpszArg   = lpszArg;
  41.   _w32_nCmdShow  = nCmdShow;
  42.  
  43.  
  44.   //convert lpszArg to how argv normally behaves
  45.   char*      p = (char*)lpszArg;
  46.   int    _argc = 1; //should always contain path to executable
  47.   char** _argv = nullptr;
  48.  
  49.    //parse for the number of arguments (assuming space as delimiter)
  50.   char* first = _getFirstCharPtr(p);
  51.   char* last  = _getLastCharPtr(p);
  52.   int Arg_len = (int)(last-first) + ((*first!=0) ? 1 : 0);
  53.  
  54.   if(Arg_len > 0) ++_argc;
  55.   for(p=first; p<=last; ++p){
  56.     if(*p == ' ') ++_argc;
  57.   }
  58.  
  59.    //create and fill in _argv
  60.   _argv = (char**)CoTaskMemAlloc( sizeof(char*) * _argc );
  61.   if(_argv == nullptr) return ERROR_NOT_ENOUGH_MEMORY;
  62.  
  63.   char filepath[MAX_PATH]; //path to current process
  64.   if(GetModuleFileNameA(NULL, filepath, MAX_PATH) == 0)
  65.     return ERROR_INSUFFICIENT_BUFFER;
  66.   _argv[0] = filepath;
  67.  
  68.   if(Arg_len > 0){
  69.     int i = 1;
  70.     _argv[i++] = first;
  71.     for(p=first; p<=last; ++p){
  72.       if(*p == ' '){
  73.         *p = 0; //set delimiter to null
  74.         _argv[i++] = p+1;
  75.       }
  76.     }
  77.     *p = 0; //make sure last arg has null terminator
  78.   }
  79.  
  80.  
  81.   int returnCode = main(_argc,_argv);
  82.   CoTaskMemFree(_argv);
  83.   return returnCode;
  84. }
  85.  
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement