几何尺寸与公差论坛

 找回密码
 注册
查看: 2352|回复: 3

如何在程序中关闭sw?

[复制链接]
发表于 2008-7-5 10:03:20 | 显示全部楼层 |阅读模式
在后台运行SW,发现用程序无法关闭SW,代码如下:
HRESULT hr = NOERROR;

       CLSID clsid;

       LPUNKNOWN pUnk = NULL;

       LPDISPATCH pDisp = NULL;



       hr = ::CLSIDFromProgID(L"SldWorks.Application", &clsid);

      

       if (SUCCEEDED(hr))

       {

              if(::GetActiveObject(clsid, NULL, &pUnk) == S_OK)

              {

                     VERIFY(pUnk->QueryInterface(IID_IDispatch, (LPVOID*)

                            &pDisp) == S_OK);

                     m_SldWorks.AttachDispatch(pDisp);

                     pUnk->Release();

              }

              else

                     VERIFY(m_SldWorks.CreateDispatch(clsid) == TRUE);
…………….

…………….

pModelDoc.ReleaseDispatch();

m_SldWorks.CloseDoc(filepath);///////////I have close the doc



m_SldWorks.ReleaseDispatch();//////make the sw application end,but it doesn’t work m_SldWorks.ExitApp();

打开任务管理器,发现SLDWORKS.EXE仍然在运行.请问该如何关闭?
 楼主| 发表于 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;
}
发表于 2008-7-20 23:20:23 | 显示全部楼层

回复: 如何在程序中关闭sw?

1. normally close SolidWorks application

// close the SolidWorks session
if (m_pSldWorks.m_lpDispatch)
{
m_pSldWorks.ExitApp ();
if (m_pSldWorks.m_lpDispatch)
{
m_pSldWorks.ReleaseDispatch();
m_pSldWorks.m_lpDispatch = NULL;
}
}
发表于 2008-7-20 23:27:51 | 显示全部楼层

回复: 如何在程序中关闭sw?

m_SldWorks.CloseDoc(filepath);///////////I have close the doc
> This means to close SW document (part, assembly, etc.), not but terminate SW application.

add a call exitApp if you plan to terminate SW application.

m_SldWorks.ReleaseDispatch();//////make the sw application end,but it doesn’t work m_SldWorks.ExitApp();
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 23:43 , Processed in 0.037355 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表