//.........这里部分代码省略.........
else return "LoadLibrary failed due to an unknown error";
#endif
}
/**************************************************************************/
/* */
/* Function Registration */
/* */
/**************************************************************************/
// get the registration symbols
std::vector<std::string> toolkit_function_reg_names
{"get_toolkit_function_registration",
"_Z33get_toolkit_function_registrationv",
"__Z33get_toolkit_function_registrationv"};
get_toolkit_function_registration_type get_toolkit_function_registration = nullptr;
for (auto reg_name : toolkit_function_reg_names) {
get_toolkit_function_registration =
reinterpret_cast<get_toolkit_function_registration_type>
(
#ifndef _WIN32
dlsym(dl, reg_name.c_str())
#else
(void *)GetProcAddress((HMODULE)dl, reg_name.c_str())
#endif
);
if (get_toolkit_function_registration != nullptr) break;
}
// register functions
if (get_toolkit_function_registration) {
auto functions = (*get_toolkit_function_registration)();
for (auto& fn: functions) {
if (!regentry.modulename.empty()) {
fn.name = regentry.modulename + "." + fn.name;
}
fn.description["file"] = regentry.original_soname;
logstream(LOG_INFO) << "Adding function: " << fn.name << std::endl;
regentry.functions.push_back(fn.name);
}
toolkit_functions->register_toolkit_function(functions);
}
/**************************************************************************/
/* */
/* Class Registration */
/* */
/**************************************************************************/
std::vector<std::string> toolkit_class_reg_names
{"get_toolkit_class_registration",
"_Z30get_toolkit_class_registrationv",
"__Z30get_toolkit_class_registrationv"};
get_toolkit_class_registration_type get_toolkit_class_registration = nullptr;
for (auto reg_name : toolkit_class_reg_names) {
get_toolkit_class_registration =
reinterpret_cast<get_toolkit_class_registration_type>
(
#ifndef _WIN32
dlsym(dl, reg_name.c_str())
#else
(void *)GetProcAddress((HMODULE)dl, reg_name.c_str())
#endif
);
if (get_toolkit_class_registration != nullptr) break;
}
// register classes
if (get_toolkit_class_registration) {
auto class_reg = (*get_toolkit_class_registration)();
for (auto& cl: class_reg) {
if (!regentry.modulename.empty()) {
cl.name = regentry.modulename + "." + cl.name;
}
cl.description["file"] = regentry.original_soname;
logstream(LOG_INFO) << "Adding class : " << cl.name << std::endl;
regentry.functions.push_back(cl.name);
}
classes->register_toolkit_class(class_reg);
}
if (regentry.functions.empty() && regentry.classes.empty()) {
// nothing has been registered! unload the dl
#ifndef _WIN32
dlclose(dl);
#else
FreeLibrary((HMODULE)dl);
#endif
return "No functions or classes registered by " + sanitize_url(soname);
}
// note that it is possible to load a toolkit multiple times.
// It is not safe to unload previously loaded toolkits since I may have
// a reference to it (for instance a class). We just keep loading over
// and hope for the best.
// store and remember the dlhandle and what was registered;
dynamic_loaded_toolkits[regentry.original_soname] = regentry;
return std::string();
}
//.........这里部分代码省略.........
si.wShowWindow = einfo->nShow;
logtofilew (logfile, L"Using nShow == %d\n", si.wShowWindow);
}
if (einfo->fMask & SEE_MASK_NO_CONSOLE)
{
logtofilew (logfile, L"We will create new console and will not inherit in/out/err handles\n");
}
else
{
logtofilew (logfile, L"We will not create new console, child process will inherit in/out/err handles\n");
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle (STD_ERROR_HANDLE);
}
if (fix_redir && iam32on64 ())
{
kernel32 = LoadLibraryW (L"kernel32.dll");
if (kernel32 != NULL)
{
disablewow64 = (Wow64DisableWow64FsRedirectionFunction) GetProcAddress (kernel32, "Wow64DisableWow64FsRedirection");
revertwow64 = (Wow64RevertWow64FsRedirectionFunction) GetProcAddress (kernel32, "Wow64RevertWow64FsRedirection");
if (disablewow64 == NULL || revertwow64 == NULL)
fix_redir = 0;
else
fix_redir = disablewow64 (&redir);
}
else
fix_redir = 0;
}
else
fix_redir = 0;
ret = CreateProcessW (newargv[0], exp_data, NULL, NULL, TRUE, einfo->fMask & SEE_MASK_NO_CONSOLE ? CREATE_NEW_CONSOLE : 0, NULL, lpdir, &si, &pi);
err = GetLastError();
if (fix_redir != 0)
revertwow64 (redir);
if (kernel32 != NULL)
FreeLibrary (kernel32);
if (ret != 0)
{
logtofilew (logfile, L"CreateProcessW() succeeded\n");
ret = 0;
if (executable)
{
logtofilew (logfile, L"Waiting until executable process terminates...\n");
WaitForSingleObject (pi.hProcess, INFINITE);
logtofilew (logfile, L"Finished waiting until executable process terminates\n");
}
else
{
if (einfo->fMask & SEE_MASK_NOCLOSEPROCESS)
{
einfo->hProcess = pi.hProcess;
logtofilew (logfile, L"Will return process handle %08X\n", pi.hProcess);
}
if (einfo->fMask & SEE_MASK_WAITFORINPUTIDLE)
{
logtofilew (logfile, L"Waiting until non-executable process' input idles...\n");
WaitForInputIdle (pi.hProcess, 60*1000);
logtofilew (logfile, L"Finished waiting until non-executable process' input idles\n");
}
}
einfo->hInstApp = (HINSTANCE) 33;
}
else
{
logtofilew (logfile, L"CreateProcessW() have failed with %d\n", err);
switch (err)
{
case ERROR_FILE_NOT_FOUND:
einfo->hInstApp = (HINSTANCE) SE_ERR_FNF;
break;
case ERROR_PATH_NOT_FOUND:
einfo->hInstApp = (HINSTANCE) SE_ERR_PNF;
break;
case ERROR_ACCESS_DENIED:
einfo->hInstApp = (HINSTANCE) SE_ERR_ACCESSDENIED;
break;
case ERROR_NOT_ENOUGH_MEMORY:
einfo->hInstApp = (HINSTANCE) SE_ERR_OOM;
break;
default:
einfo->hInstApp = (HINSTANCE) 33;
}
ret = 1;
}
logtofilew (logfile, L"hInstApp is set to %d\n", einfo->hInstApp);
free (exp_data);
if (dupdata != NULL)
free (dupdata);
if (newargv != NULL)
free (newargv);
logtofilew (logfile, L"<run_handler %d\n", ret);
return ret;
}
开发者ID:LRN,项目名称:mimerun,代码行数:101,代码来源:mimerun.c
示例13: Wname
/**
* \fn int CEnvironment::win32_setenv(const std::wstring &name, const std::wstring &value = L"",
* updateAction action = autoDetect)
* \brief Internal function used to manipulate with environment variables on win32.
*
* This function make all dirty work with setting, deleting and modifying environment variables.
*
* \param name The environment variable name.
* \param value (optional) the new value of environment variable.
* \param action (optional) the action.
* \return Zero on success, 2 if at least one external runtime update failed, 4 if process
* environment update failed, 8 if our runtime environment update failed or, in case of
* several errors, sum of all errors values; non-zero in case of other errors.
*/
int CEnvironment::win32_setenv(const std::string &name, const std::string &value /* = "" */, enum updateAction action /* = autoDetect */)
{
std::wstring Wname (win32ConvertUtf8ToW(name));
if (Wname.empty() || name.find('=') != std::wstring::npos)
return -1;
if ( (action == addOnly || action == addOrUpdateOnly) && value.empty() )
return -1;
if (action == addOnly && !(getenv(name).empty()) )
return 0;
bool convIsOK;
std::wstring Wvalue (win32ConvertUtf8ToW(value,&convIsOK));
if (!convIsOK)
return -1;
int retValue = 0;
std::wstring EnvString;
if (action == deleteVariable)
EnvString = Wname + L"=";
else
EnvString = Wname + L"=" + Wvalue;
static const wchar_t *modulesList[] =
{
/*{ L"msvcrt20.dll" }, // Visual C++ 2.0 / 2.1 / 2.2
{ L"msvcrt40.dll" }, // Visual C++ 4.0 / 4.1 */ // too old and no UNICODE support - ignoring
{ L"msvcrt.dll" }, // Visual Studio 6.0 / MinGW[-w64]
{ L"msvcr70.dll" }, // Visual Studio 2002
{ L"msvcr71.dll" }, // Visual Studio 2003
{ L"msvcr80.dll" }, // Visual Studio 2005
{ L"msvcr90.dll" }, // Visual Studio 2008
{ L"msvcr100.dll" }, // Visual Studio 2010
#ifdef _DEBUG
{ L"msvcr100d.dll" },// Visual Studio 2010 (debug)
#endif
{ L"msvcr110.dll" }, // Visual Studio 2012
#ifdef _DEBUG
{ L"msvcr110d.dll" },// Visual Studio 2012 (debug)
#endif
{ NULL } // Terminating NULL for list
};
// Check all modules each function run, because modules can be loaded/unloaded at runtime
for (int i = 0; modulesList[i]; i++)
{
HMODULE hModule;
if (!GetModuleHandleExW(0, modulesList[i], &hModule) || hModule == NULL) // Flag 0 ensures that module will be kept loaded until it'll be freed
continue; // Module not loaded
wputenvPtr wputenvFunc = (wputenvPtr) GetProcAddress(hModule, "_wputenv");
if (wputenvFunc != NULL && wputenvFunc(EnvString.c_str()) != 0)
retValue |= 2; // At lest one external runtime library Environment update failed
FreeLibrary(hModule);
}
// Update process Environment used for current process and for future new child processes
if (action == deleteVariable || value.empty())
retValue += SetEnvironmentVariableW(Wname.c_str(), NULL) ? 0 : 4; // 4 if failed
else
retValue += SetEnvironmentVariableW(Wname.c_str(), Wvalue.c_str()) ? 0 : 4; // 4 if failed
// Finally update our runtime Environment
retValue += (::_wputenv(EnvString.c_str()) == 0) ? 0 : 8; // 8 if failed
return retValue;
}
请发表评论