88 } 89 90 if (len == 0) { 91 // error occured 92 JP_THROW(SysError("GetModuleFileName failed", GetModuleFileName)); 93 } 94 return tstring(buf.begin(), buf.begin() + len); 95 } 96 97 } // namespace 98 99 tstring getProcessModulePath() { 100 return getModulePath(NULL); 101 } 102 103 HMODULE getCurrentModuleHandle() 104 { 105 // get module handle for the address of this function 106 LPCWSTR address = reinterpret_cast<LPCWSTR>(getCurrentModuleHandle); 107 HMODULE hmodule = NULL; 108 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, 109 address, &hmodule)) 110 { 111 JP_THROW(SysError(tstrings::any() << "GetModuleHandleExW failed", GetModuleHandleExW)); 112 } 113 return hmodule; 114 } 115 116 tstring getCurrentModulePath() 117 { 118 return getModulePath(getCurrentModuleHandle()); 119 } 120 121 tstring_array getCommandArgs(CommandArgProgramNameMode progNameMode) 122 { 123 int argc = 0; 124 tstring_array result; 125 126 LPWSTR *parsedArgs = CommandLineToArgvW(GetCommandLineW(), &argc); 127 if (parsedArgs == NULL) { 128 JP_THROW(SysError("CommandLineToArgvW failed", CommandLineToArgvW)); 129 } 130 // the 1st element contains program name 131 for (int i = progNameMode == ExcludeProgramName ? 1 : 0; i < argc; i++) { | 88 } 89 90 if (len == 0) { 91 // error occured 92 JP_THROW(SysError("GetModuleFileName failed", GetModuleFileName)); 93 } 94 return tstring(buf.begin(), buf.begin() + len); 95 } 96 97 } // namespace 98 99 tstring getProcessModulePath() { 100 return getModulePath(NULL); 101 } 102 103 HMODULE getCurrentModuleHandle() 104 { 105 // get module handle for the address of this function 106 LPCWSTR address = reinterpret_cast<LPCWSTR>(getCurrentModuleHandle); 107 HMODULE hmodule = NULL; 108 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 109 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, address, &hmodule)) 110 { 111 JP_THROW(SysError(tstrings::any() << "GetModuleHandleExW failed", 112 GetModuleHandleExW)); 113 } 114 return hmodule; 115 } 116 117 tstring getCurrentModulePath() 118 { 119 return getModulePath(getCurrentModuleHandle()); 120 } 121 122 tstring_array getCommandArgs(CommandArgProgramNameMode progNameMode) 123 { 124 int argc = 0; 125 tstring_array result; 126 127 LPWSTR *parsedArgs = CommandLineToArgvW(GetCommandLineW(), &argc); 128 if (parsedArgs == NULL) { 129 JP_THROW(SysError("CommandLineToArgvW failed", CommandLineToArgvW)); 130 } 131 // the 1st element contains program name 132 for (int i = progNameMode == ExcludeProgramName ? 1 : 0; i < argc; i++) { |