|
楼主 |
发表于 2008-7-15 16:25:33
|
显示全部楼层
回复: 如何在程序中关闭sw?
2. 暴力终止Solidworks application
// Close SW application, if application is running with Error
bool CSolidWorksSystem::CloseApplication(const bool status)
{
bool result = false;
if (!status)
{
return result;
}
// find sldworks.exe
CString strName = _T("sldworks.exe");
long processID = 0;
processID = GetProcessIDFromName(strName);
if (processID < 0)
{
return result;
}
// catch error message dialog from SW with a invalid dongle
LPCWSTR lpWindowName = _T("SolidWorks");
HWND hwnd = FindWindow(NULL, lpWindowName);
#ifdef _DEBUG
if (hwnd)
SetForegroundWindow(hwnd);
#endif
// close error message dialogs from SW
for (;hwnd;)
{
SendMessage(hwnd, WM_CLOSE, 0, 0 );
hwnd = FindWindow(NULL, lpWindowName);
}
// is sldworks.exe running?
processID = GetProcessIDFromName(strName);
if (processID > 0)
{
HANDLE swHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, processID);
long exitcode= 0;
if (swHandle != (HANDLE)-1)
{
TerminateProcess(swHandle, exitcode);
CloseHandle(swHandle);
}
}
result = true;
return result;
} |
|