发表于:2008-01-24 13:45:00
11楼
枚举计算机,主要用到这三个函数,实现比较简单,网上应该可以查到
1、WNetOpenEnum
2、WNetEnumResource
3、WNetCloseEnum
枚举远程OPC服务器也有两种办法:
1 枚举注册表
// search the registry for OPC entries
HKEY hk = HKEY_CLASSES_ROOT;
if( node )
{
DWORD dwR = RegConnectRegistry ((LPTSTR)node, HKEY_CLASSES_ROOT, &hk);
if( dwR != ERROR_SUCCESS )
return FALSE;
}
TCHAR key[MAX_KEYLEN];
for( int index=0; RegEnumKey(hk, index, key, MAX_KEYLEN)==ERROR_SUCCESS; index++)
{
HKEY hProgID;
TCHAR dummy[MAX_KEYLEN];
if(RegOpenKey(hk, key, &hProgID )==ERROR_SUCCESS )
{
LONG size=MAX_KEYLEN;
if(RegQueryValue(hProgID, _T("OPC"), dummy, &size)==ERROR_SUCCESS )
{
TCHAR desc[MAX_KEYLEN];
size=MAX_KEYLEN;
RegQueryValue(hk, key, desc, &size);
//得到key;
}
RegCloseKey( hProgID );
}
}
2、通过OPCNUM
IOPCServerList *gpOPC;
HRESULT hr, hr2;
IEnumGUID *pEnumGUID;
CLSID catid, clsid;
unsigned long c;
LPOLESTR pszProgID, pszUserType;
// create the enumerator object
gpOPC = CreateServerEnumerator(pathname);
if (gpOPC == NULL)
{
DoErrorMsg ( 0, "Failed to Execute OPCENUM");
// revert to search of Registry if OPCENUM fails to execute
return (GetServerListFromRegistry());
}
// use category id's to enumerate server list
catid = CATID_OPCDAServer20;
gpOPC->EnumClassesOfCategories(1, &catid,
1, &catid, &pEnumGUID);
if (pEnumGUID != NULL)
{
while (SUCCEEDED(hr = pEnumGUID->Next(1, &clsid, &c)))
{
if (!ValidCLSID(clsid))
break;
// add server name to list
//得到 clsid;
hr2 = gpOPC->GetClassDetails(clsid, &pszProgID, &pszUserType);
if (FAILED(hr2))
//错误处理
else
{
//得到pszProgID;
CoTaskMemFree(pszProgID);
CoTaskMemFree(pszUserType);
}
}
pEnumGUID->Release();
}
gpOPC->Release();
以上仅供参考