OPCEnum.EXE在OPC客户端的使用方法 点击:1537 | 回复:1



opcfan

    
  • 精华:0帖
  • 求助:0帖
  • 帖子:62帖 | 58回
  • 年度积分:0
  • 历史总积分:667
  • 注册:2004年9月29日
发表于:2007-02-19 12:32:00
楼主
[b]E-Mail:zhan826@163.com MSN:zhan826@hotmail.com QQ:10167223[/b]

在制作OPC Client 的程序的时候,常常需要浏览目标服务器的所有OPC Server方法有两个: 一、直接通过查询注册表。
void DisplayGeneralOPCServers (HTREEITEM hParent)
{
HKEY hKey = HKEY_CLASSES_ROOT; // search under this key
TCHAR szKey [DEFBUFFSIZE]; // allocate key buffer
DWORD dwLength = DEFBUFFSIZE;

// Search the registry for installed OPC Servers:
for (DWORD dwIndex = 0; 
 RegEnumKey (hKey, dwIndex, szKey, dwLength) == ERROR_SUCCESS; 
 ++dwIndex)
{
HKEY hSubKey;

// Open the registry key:
if (RegOpenKey (hKey, szKey, &hSubKey) == ERROR_SUCCESS)
{
// Search for OPC subkey:
if (RegQueryValue (hSubKey, _T("OPC"), NULL, NULL) == ERROR_SUCCESS)
{
// Display the prog ID for this server:
m_pServerList->InsertItem (szKey, ILI_COMPONENT, ILI_COMPONENT, hParent);
}

// Close the registry key:
RegCloseKey (hSubKey);
}

// Re-initialize length:
dwLength = DEFBUFFSIZE;
}
}
二、通过调用OPCEnum.exe 提供的 EnumClassesOfCategories方法实现。
void  DisplayComponentCatList (HTREEITEM hParent, CATID catid)
{
HRESULT hr;

// Make sure COM is initialized:
hr = CoInitializeEx (NULL, COINIT_MULTITHREADED);

if (SUCCEEDED (hr))
{
ICatInformation *pCat = NULL;

// Get component category manager:
hr = CoCreateInstance (CLSID_StdComponentCategoriesMgr, 
NULL,
CLSCTX_SERVER, 
IID_ICatInformation,
(void **)&pCat);

// If succeeded, enumerate registered components:
if (SUCCEEDED (hr))
{
IEnumCLSID *pEnum = NULL;

CATID arrcatid [1];
arrcatid [0] = catid;

// Enumerate registered components based on clsid:
hr = pCat->EnumClassesOfCategories (
sizeof (arrcatid) / sizeof (CATID), // number of catids in the array that follows
arrcatid, // catid array
0, 
NULL,
&pEnum); // clsid enumerator for registered components under this category

// If succeeded, process results:
if (SUCCEEDED (hr))
{
GUID guid;
ULONG fetched;

// Loop over enumerated components.  Call enemerator's next
// member function to reference next component and get its
// guid:
while ((hr = pEnum->Next (1, &guid, &fetched)) == S_OK)
{
// Get the ProgID from the guid:
WCHAR *wszProgID;
hr = ProgIDFromCLSID (guid, &wszProgID);

// If succeeded, add component to list:
if (SUCCEEDED (hr))
{
// ProgID string will be in UNICODE format.  Convert to 
// ANSI format if this is and ANSI build.  Ins



opcfan

  • 精华:0帖
  • 求助:0帖
  • 帖子:62帖 | 58回
  • 年度积分:0
  • 历史总积分:667
  • 注册:2004年9月29日
发表于:2008-07-31 13:17:18
1楼

二、通过调用OPCEnum.exe 提供的 EnumClassesOfCategories方法实现。 (原来有误,正确的如下)

 

void CKServerGeneralPage::DisplayComponentCatList (HTREEITEM hParent, CATID catid)
{
HRESULT hr;

// Make sure COM is initialized:
hr = CoInitializeEx (NULL, COINIT_MULTITHREADED);

if (SUCCEEDED (hr))
{
IOPCServerList2 *pCat = NULL;

// Get component category manager:CLSID_StdComponentCategoriesMgr
hr = CoCreateInstance (CLSID_OPCServerList,
NULL,
CLSCTX_SERVER,
//IID_ICatInformation,
IID_IOPCServerList2,
(void **)&pCat);

// If succeeded, enumerate registered components:
if (SUCCEEDED (hr))
{
IOPCEnumGUID *pEnum = NULL;

CATID arrcatid [1];
arrcatid [0] = catid;

// Enumerate registered components based on clsid:
hr = pCat->EnumClassesOfCategories (
sizeof (arrcatid) / sizeof (CATID),//number of catids in the array that follows
arrcatid, // catid array
0,
NULL,
(IOPCEnumGUID**)&pEnum); // clsid enumerator for registered components under this category

// If succeeded, process results:
if (SUCCEEDED (hr))
{
GUID guid;
ULONG fetched;

// Loop over enumerated components. Call enemerator's next
// member function to reference next component and get its
// guid:
while ((hr = pEnum->Next (1, &guid, &fetched)) == S_OK)
{
// Get the ProgID from the guid:
WCHAR *wszProgID;
hr = ProgIDFromCLSID (guid, &wszProgID);

// If succeeded, add component to list:
if (SUCCEEDED (hr))
{
// ProgID string will be in UNICODE format. Convert to
// ANSI format if this is and ANSI build. Insert component
// into list:
#ifdef _UNICODE
m_pServerList->InsertItem (wszProgID, ILI_COMPONENT, ILI_COMPONENT, hParent);
#else
TCHAR szProgID [DEFBUFFSIZE];

_wcstombsz (szProgID, wszProgID, sizeof (szProgID) / sizeof (TCHAR));
m_pServerList->InsertItem (szProgID, ILI_COMPONENT, ILI_COMPONENT, hParent);
#endif
// It is up to us to free the Prog ID string memory:
CoTaskMemFree (wszProgID);
}
}

// Release our enumerator:
pEnum->Release ();
}

// release our category mamager
pCat->Release ();
}

// Uninitialize COM:
CoUninitialize ();
}

}


热门招聘
相关主题

官方公众号

智造工程师