Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "_kit_misc.hpp"
- HINSTANCE _w32_hThisInst = nullptr;
- HINSTANCE _w32_hPrevInst = nullptr;
- LPSTR _w32_lpszArg = nullptr;
- int _w32_nCmdShow = SW_HIDE;
- //get first and last char of string, excluding leading or trailing whitespace
- //(whitespace as in ' '; i'm not going to bother with '\t', '\n', etc.)
- static inline char* _getFirstCharPtr(char* start){
- if(*start == 0) return start;
- while(*start == ' ') ++start;
- return start;
- }
- static inline char* _getLastCharPtr(char* start){
- if(*start == 0) return start;
- char* end = start;
- while(*(end+1) != 0 ) ++end; //go to null terminator
- while(*end == ' ') --end; //go back until a non-whitespace char is found
- return end;
- }
- extern int main(int argc, char** argv);
- int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
- LPSTR lpszArg, int nCmdShow)
- {
- //assign some global values
- _w32_hThisInst = hThisInst;
- _w32_hPrevInst = hPrevInst;
- _w32_lpszArg = lpszArg;
- _w32_nCmdShow = nCmdShow;
- //convert lpszArg to how argv normally behaves
- char* p = (char*)lpszArg;
- int _argc = 1; //should always contain path to executable
- char** _argv = nullptr;
- //parse for the number of arguments (assuming space as delimiter)
- char* first = _getFirstCharPtr(p);
- char* last = _getLastCharPtr(p);
- int Arg_len = (int)(last-first) + ((*first!=0) ? 1 : 0);
- if(Arg_len > 0) ++_argc;
- for(p=first; p<=last; ++p){
- if(*p == ' ') ++_argc;
- }
- //create and fill in _argv
- _argv = (char**)CoTaskMemAlloc( sizeof(char*) * _argc );
- if(_argv == nullptr) return ERROR_NOT_ENOUGH_MEMORY;
- char filepath[MAX_PATH]; //path to current process
- if(GetModuleFileNameA(NULL, filepath, MAX_PATH) == 0)
- return ERROR_INSUFFICIENT_BUFFER;
- _argv[0] = filepath;
- if(Arg_len > 0){
- int i = 1;
- _argv[i++] = first;
- for(p=first; p<=last; ++p){
- if(*p == ' '){
- *p = 0; //set delimiter to null
- _argv[i++] = p+1;
- }
- }
- *p = 0; //make sure last arg has null terminator
- }
- int returnCode = main(_argc,_argv);
- CoTaskMemFree(_argv);
- return returnCode;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement