utils.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,558 行 · 第 1/4 页
CPP
1,558 行
{
case WAIT_OBJECT_0:
// process terminated
if ( !::GetExitCodeProcess(hProcess, &rc) )
{
wxLogLastError(_T("GetExitCodeProcess"));
}
break;
default:
wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
// fall through
case WAIT_FAILED:
wxLogLastError(_T("WaitForSingleObject"));
// fall through
case WAIT_TIMEOUT:
if ( krc )
*krc = wxKILL_ERROR;
rc = STILL_ACTIVE;
break;
}
}
// the return code is the same as from Unix kill(): 0 if killed
// successfully or -1 on error
if ( !ok || rc == STILL_ACTIVE )
return -1;
if ( krc )
*krc = wxKILL_OK;
return 0;
}
typedef HANDLE (WINAPI *CreateToolhelp32Snapshot_t)(DWORD,DWORD);
typedef BOOL (WINAPI *Process32_t)(HANDLE,LPPROCESSENTRY32);
CreateToolhelp32Snapshot_t lpfCreateToolhelp32Snapshot;
Process32_t lpfProcess32First, lpfProcess32Next;
static void InitToolHelp32()
{
static bool s_initToolHelpDone = false;
if (s_initToolHelpDone)
return;
s_initToolHelpDone = true;
lpfCreateToolhelp32Snapshot = NULL;
lpfProcess32First = NULL;
lpfProcess32Next = NULL;
#if wxUSE_DYNLIB_CLASS
wxDynamicLibrary dllKernel(_T("kernel32.dll"), wxDL_VERBATIM);
// Get procedure addresses.
// We are linking to these functions of Kernel32
// explicitly, because otherwise a module using
// this code would fail to load under Windows NT,
// which does not have the Toolhelp32
// functions in the Kernel 32.
lpfCreateToolhelp32Snapshot =
(CreateToolhelp32Snapshot_t)dllKernel.RawGetSymbol(_T("CreateToolhelp32Snapshot"));
lpfProcess32First =
(Process32_t)dllKernel.RawGetSymbol(_T("Process32First"));
lpfProcess32Next =
(Process32_t)dllKernel.RawGetSymbol(_T("Process32Next"));
#endif // wxUSE_DYNLIB_CLASS
}
// By John Skiff
int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc)
{
InitToolHelp32();
if (krc)
*krc = wxKILL_OK;
// If not implemented for this platform (e.g. NT 4.0), silently ignore
if (!lpfCreateToolhelp32Snapshot || !lpfProcess32First || !lpfProcess32Next)
return 0;
// Take a snapshot of all processes in the system.
HANDLE hProcessSnap = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE) {
if (krc)
*krc = wxKILL_ERROR;
return -1;
}
//Fill in the size of the structure before using it.
PROCESSENTRY32 pe;
wxZeroMemory(pe);
pe.dwSize = sizeof(PROCESSENTRY32);
// Walk the snapshot of the processes, and for each process,
// kill it if its parent is pid.
if (!lpfProcess32First(hProcessSnap, &pe)) {
// Can't get first process.
if (krc)
*krc = wxKILL_ERROR;
CloseHandle (hProcessSnap);
return -1;
}
do {
if (pe.th32ParentProcessID == (DWORD) pid) {
if (wxKill(pe.th32ProcessID, sig, krc))
return -1;
}
} while (lpfProcess32Next (hProcessSnap, &pe));
return 0;
}
// Execute a program in an Interactive Shell
bool wxShell(const wxString& command)
{
wxString cmd;
#ifdef __WXWINCE__
cmd = command;
#else
wxChar *shell = wxGetenv(wxT("COMSPEC"));
if ( !shell )
shell = (wxChar*) wxT("\\COMMAND.COM");
if ( !command )
{
// just the shell
cmd = shell;
}
else
{
// pass the command to execute to the command processor
cmd.Printf(wxT("%s /c %s"), shell, command.c_str());
}
#endif
return wxExecute(cmd, wxEXEC_SYNC) == 0;
}
// Shutdown or reboot the PC
bool wxShutdown(wxShutdownFlags WXUNUSED_IN_WINCE(wFlags))
{
#ifdef __WXWINCE__
// TODO-CE
return false;
#elif defined(__WIN32__)
bool bOK = true;
if ( wxGetOsVersion(NULL, NULL) == wxWINDOWS_NT ) // if is NT or 2K
{
// Get a token for this process.
HANDLE hToken;
bOK = ::OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&hToken) != 0;
if ( bOK )
{
TOKEN_PRIVILEGES tkp;
// Get the LUID for the shutdown privilege.
::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
bOK = ::GetLastError() == ERROR_SUCCESS;
}
}
if ( bOK )
{
UINT flags = EWX_SHUTDOWN | EWX_FORCE;
switch ( wFlags )
{
case wxSHUTDOWN_POWEROFF:
flags |= EWX_POWEROFF;
break;
case wxSHUTDOWN_REBOOT:
flags |= EWX_REBOOT;
break;
default:
wxFAIL_MSG( _T("unknown wxShutdown() flag") );
return false;
}
bOK = ::ExitWindowsEx(flags, 0) != 0;
}
return bOK;
#endif // Win32/16
}
wxPowerType wxGetPowerType()
{
// TODO
return wxPOWER_UNKNOWN;
}
wxBatteryState wxGetBatteryState()
{
// TODO
return wxBATTERY_UNKNOWN_STATE;
}
// ----------------------------------------------------------------------------
// misc
// ----------------------------------------------------------------------------
// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
wxMemorySize wxGetFreeMemory()
{
#if defined(__WIN64__)
MEMORYSTATUSEX memStatex;
memStatex.dwLength = sizeof (memStatex);
::GlobalMemoryStatusEx (&memStatex);
return (wxMemorySize)memStatex.ullAvailPhys;
#else /* if defined(__WIN32__) */
MEMORYSTATUS memStatus;
memStatus.dwLength = sizeof(MEMORYSTATUS);
::GlobalMemoryStatus(&memStatus);
return (wxMemorySize)memStatus.dwAvailPhys;
#endif
}
unsigned long wxGetProcessId()
{
return ::GetCurrentProcessId();
}
// Emit a beeeeeep
void wxBell()
{
::MessageBeep((UINT)-1); // default sound
}
bool wxIsDebuggerRunning()
{
#if wxUSE_DYNLIB_CLASS
// IsDebuggerPresent() is not available under Win95, so load it dynamically
wxDynamicLibrary dll(_T("kernel32.dll"), wxDL_VERBATIM);
typedef BOOL (WINAPI *IsDebuggerPresent_t)();
if ( !dll.HasSymbol(_T("IsDebuggerPresent")) )
{
// no way to know, assume no
return false;
}
return (*(IsDebuggerPresent_t)dll.GetSymbol(_T("IsDebuggerPresent")))() != 0;
#else
return false;
#endif
}
// ----------------------------------------------------------------------------
// OS version
// ----------------------------------------------------------------------------
wxString wxGetOsDescription()
{
wxString str;
OSVERSIONINFO info;
wxZeroMemory(info);
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if ( ::GetVersionEx(&info) )
{
switch ( info.dwPlatformId )
{
case VER_PLATFORM_WIN32s:
str = _("Win32s on Windows 3.1");
break;
case VER_PLATFORM_WIN32_WINDOWS:
switch (info.dwMinorVersion)
{
case 0:
if ( info.szCSDVersion[1] == 'B' ||
info.szCSDVersion[1] == 'C' )
{
str = _("Windows 95 OSR2");
}
else
{
str = _("Windows 95");
}
break;
case 10:
if ( info.szCSDVersion[1] == 'B' ||
info.szCSDVersion[1] == 'C' )
{
str = _("Windows 98 SE");
}
else
{
str = _("Windows 98");
}
break;
case 90:
str = _("Windows ME");
break;
default:
str.Printf(_("Windows 9x (%d.%d)"),
info.dwMajorVersion,
info.dwMinorVersion);
break;
}
if ( !wxIsEmpty(info.szCSDVersion) )
{
str << _T(" (") << info.szCSDVersion << _T(')');
}
break;
case VER_PLATFORM_WIN32_NT:
if ( info.dwMajorVersion == 5 )
{
switch ( info.dwMinorVersion )
{
case 0:
str.Printf(_("Windows 2000 (build %lu"),
info.dwBuildNumber);
break;
case 1:
str.Printf(_("Windows XP (build %lu"),
info.dwBuildNumber);
break;
case 2:
str.Printf(_("Windows Server 2003 (build %lu"),
info.dwBuildNumber);
break;
}
}
if ( wxIsEmpty(str) )
{
str.Printf(_("Windows NT %lu.%lu (build %lu"),
info.dwMajorVersion,
info.dwMinorVersion,
info.dwBuildNumber);
}
if ( !wxIsEmpty(info.szCSDVersion) )
{
str << _T(", ") << info.szCSDVersion;
}
str << _T(')');
break;
}
}
else
{
wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
}
return str;
}
wxToolkitInfo& wxAppTraits::GetToolkitInfo()
{
// cache the version info, it's not going to change
//
// NB: this is MT-safe, we may use these static vars from different threads
// but as they always have the same value it doesn't matter
static int s_ver = -1,
s_major = -1,
s_minor = -1;
if ( s_ver == -1 )
{
OSVERSIONINFO info;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?