![](https://habrastorage.org/web/b7d/3a4/d7e/b7d3a4d7e1b34bfd8e8a684333fd4b73.png)
本日は、下の図に示すような画像を作成しないように、すでに起動されているKOMPASに接続する方法について説明します。
![](https://habrastorage.org/web/c67/44b/e5a/c6744be5aac0452480379bf70040b15f.png)
KOMPAS-3Dのいくつかのインスタンスが起動しました
コンパスへの接続
KOMPASプログラムに接続するには、 ActiveInstanceメソッドが使用されます。 以下は、KOMPASに接続するプログラムの例です。
KompasObjectPtr kompas;
kompas.ActiveInstance(L"KOMPAS.Application.5");
//
kompas->Visible = true;
kompas.Unbind();
ActiveInstance – , , Unicode.
, ? . .
![](https://habrastorage.org/web/6ba/387/3cc/6ba3873cc55d4f40912a53195c7ab5f3.png)
![](https://habrastorage.org/web/fc8/e9e/66d/fc8e9e66dfaa40c2848c2ed44199c4b9.png)
. ActiveInstance try/catch, , .
try{
KompasObjectPtr kompas;
kompas.ActiveInstance(L"KOMPAS.Application.5");
//
kompas->Visible = true;
kompas.Unbind();
}catch(...){}
. , ActiveInstance , . : , ? .
– FindWindow . FindWindow . , . .
, , , , , . . .
Spy++. ( ).
![](https://habrastorage.org/web/a28/04b/51c/a2804b51c8a541c8908676a66d95149e.png)
« »
, , . .
, - , «-3D» . FindWindow . , , , - «-3D». , .
//
bool CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
//
unsigned int size;
size = GetWindowTextLength(hwnd);
if(!size) return true;
//
wchar_t* pbuffer;
pbuffer = (wchar_t*)malloc(sizeof(wchar_t)*(size+1));
//
GetWindowTextW(hwnd, pbuffer, size-1);
//
wchar_t *p;
p = wcsstr(pbuffer, L"-3D");
//
free(pbuffer);
if(!p) return true;
// ,
bool *pres;
pres = (bool*)lParam;
*pres = true;
return false;
}
// ?
bool IsKOMPASRun()
{
bool res = false;
EnumWindows((WNDENUMPROC)EnumWindowsProc, (LPARAM)(&res));
return res;
}
EnumWindows, Windows API. EnumWindowsProc. , «-3D». , , – . .
if(IsKOMPASRun())
ShowMessage(" ");
else
ShowMessage(" ");
, . , , «-3D». 4. , .
![](https://habrastorage.org/web/399/e9c/65e/399e9c65e2bc47a2af43c59847e7f04d.png)
. «kompas.exe» ( ). ? , .
bool IsKOMPASRun()
{
// ,
char ExeName[] = "kompas.exe";
size_t lenName = strlen(ExeName);
//
HANDLE hSnapshot;
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
//
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnapshot, &entry);
size_t len;
bool res = false;
do{
//
len = strlen(entry.szExeFile);
if(len != lenName) continue;
if(!strnicmp(entry.szExeFile, ExeName, len))
{
res = true;
break;
}
}while(Process32Next(hSnapshot, &entry));
CloseHandle(hSnapshot);
return res;
}
CreateToolhelp32Snapshot, Process32First, Process32Next, Windows API. tlhelp32.h. , . strnicmp, .
, , . , . ? -? .
COM
. . . ole32.dll, Windows COM. , , .
bool IsKOMPASRun()
{
wchar_t ObjectName[] = L"KOMPAS.Application.5";
// Ole32.dll
CoInitialize(NULL);
CLSID clsid;
// clsid
CLSIDFromProgID(ObjectName, &clsid);
//
HRESULT res;
IUnknown *pIUnknown;
res = GetActiveObject(clsid, NULL, &pIUnknown);
if(res == S_OK)
{
pIUnknown->Release();
return true;
}
return false;
}
GetActiveObject. . , , . CLSIDFromProgID CLSID ( 128- ). , . , .
bool IsKOMPASInstalled()
{
wchar_t ObjectName[] = L"KOMPAS.Application.5";
// Ole32.dll
CoInitialize(NULL);
CLSID clsid;
// clsid
HRESULT res;
res = CLSIDFromProgID(ObjectName, &clsid);
return (res == S_OK);
}
, .
wchar_t ObjectName[] = L"KOMPAS.Application.5";
……………………………………………………………
if(! IsKOMPASInstalled())
{
ShowMessage(" ");
return;
}
KompasObjectPtr kompas;
if(IsKOMPASRun())
kompas.ActiveInstance(ObjectName);
else
kompas.CreateInstance(ObjectName);
kompas->Visible = true;
kompas.Unbind();
ObjectName , .
, . . , . , GetActiveObject.
, .
.
![](https://habrastorage.org/web/595/eee/f27/595eeef271b24830b3578751fcb52716.png)