Error: WTclient - Failed to Execute OPCENUM
Error: WTclient - Failed to Execute OPCENUM
我正在编写一个 OPC 客户端来获取标签并将标签发送到 OPC 服务器,但我遇到了这个错误:
WTclient - 无法执行 OPCENUM
我的代码非常简单,我正在按照我在 Internet 上找到的完美运行的示例应用程序进行操作。
我的程序可以完美编译并运行,但是当到达 Numbr = NumberOfOPCServers(TRUE, MachineName);弹出错误的行。
我的 .ccp 是:
#include "stdafx.h"
#include "opcda.h"
#include "opc_ae.h"
#include "wtclientapi.h"
#include "OPC2.h"
int _tmain(int argc, _TCHAR* argv[])
{
int Numbr;
MachineName = "";
Numbr = NumberOfOPCServers(TRUE, MachineName);
return 0;
}
深入调试另一个互联网例子,我发现错误来自这个函数:
int CWTclientApp::GetServerListFromOPCENUM(CString pathname)
{
IOPCServerList *gpOPC;
HRESULT hr, hr2;
IEnumGUID *pEnumGUID;
CLSID catid, clsid;
unsigned long c;
LPOLESTR pszProgID, pszUserType;
int i;
OPCSVRDESCR *pSvr;
for (i=0; i<MyServerList.GetSize(); i++)
{
pSvr = (OPCSVRDESCR *)MyServerList.GetAt(i);
delete (pSvr);
}
MyServerList.RemoveAll();
// 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());
}
............................................
从函数 NumberOfOPCServers():
中调用
_declspec(dllexport) int WINAPI NumberOfOPCServers (bool UseOPCENUM, LPCSTR MachineName)
{
CWTclientApp *pApp;
CString path;
path = MachineName;
path.MakeUpper();
if (path == "LOCAL")
path = "";
else
path = MachineName;
pApp = (CWTclientApp *)AfxGetApp();
if (UseOPCENUM)
return (pApp->GetServerListFromOPCENUM(path));
else
return (pApp->GetServerListFromRegistry());
}
知道为什么会出现此错误吗?看起来 OPCenum.exe 没有在我的机器(本地服务器)上枚举我的 OPC 服务器 运行,但为什么呢? (下载的应用程序有,我的没有)
谢谢!!!!
编辑---------------------------------------- ----------------------------
CreateServerEnumerator():
IOPCServerList *CWTclientApp::CreateServerEnumerator (CString PathName)
{
HRESULT r2;
MULTI_QI mqi;
COSERVERINFO sin, *sinptr;
DWORD clsctx;
// set up server info
//
if (PathName.GetLength() > 0)
{
sinptr = &sin;
sin.dwReserved1 = 0;
sin.dwReserved2 = 0;
sin.pwszName = WSTRFromCString (PathName, FALSE);
sin.pAuthInfo = 0;
clsctx = CLSCTX_REMOTE_SERVER;
}
else
{
sinptr = 0; // pointer should be NULL if local
clsctx = CLSCTX_LOCAL_SERVER;
}
// set up mqi
//
mqi.pIID = &IID_IOPCServerList;
mqi.hr = 0;
mqi.pItf = 0;
r2 = CoCreateInstanceEx(CLSID_OPCServerList, NULL,
clsctx, sinptr, 1, &mqi);
if (PathName.GetLength() > 0)
WSTRFree (sin.pwszName, FALSE);
if (FAILED(r2) || FAILED(mqi.hr))
return (NULL);
return (IOPCServerList*)mqi.pItf;
}
在调用任何其他 COM 函数之前,只需在使用 COM (OPC Classic) 的每个线程中调用 CoInitialize (https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543(v=vs.85).aspx) or CoInitializeEx (https://msdn.microsoft.com/en-us/library/windows/desktop/ms695279(v=vs.85).aspx)。
CoInitialize 已过时,因此您应该使用 CoInitializeEx。
Microsoft 在他们的文档中对此函数说:"CoInitializeEx must be called at least once, and is usually called only once, for each thread that uses the COM library".
我正在编写一个 OPC 客户端来获取标签并将标签发送到 OPC 服务器,但我遇到了这个错误: WTclient - 无法执行 OPCENUM 我的代码非常简单,我正在按照我在 Internet 上找到的完美运行的示例应用程序进行操作。 我的程序可以完美编译并运行,但是当到达 Numbr = NumberOfOPCServers(TRUE, MachineName);弹出错误的行。
我的 .ccp 是:
#include "stdafx.h"
#include "opcda.h"
#include "opc_ae.h"
#include "wtclientapi.h"
#include "OPC2.h"
int _tmain(int argc, _TCHAR* argv[])
{
int Numbr;
MachineName = "";
Numbr = NumberOfOPCServers(TRUE, MachineName);
return 0;
}
深入调试另一个互联网例子,我发现错误来自这个函数:
int CWTclientApp::GetServerListFromOPCENUM(CString pathname)
{
IOPCServerList *gpOPC;
HRESULT hr, hr2;
IEnumGUID *pEnumGUID;
CLSID catid, clsid;
unsigned long c;
LPOLESTR pszProgID, pszUserType;
int i;
OPCSVRDESCR *pSvr;
for (i=0; i<MyServerList.GetSize(); i++)
{
pSvr = (OPCSVRDESCR *)MyServerList.GetAt(i);
delete (pSvr);
}
MyServerList.RemoveAll();
// 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());
}
............................................
从函数 NumberOfOPCServers():
中调用_declspec(dllexport) int WINAPI NumberOfOPCServers (bool UseOPCENUM, LPCSTR MachineName)
{
CWTclientApp *pApp;
CString path;
path = MachineName;
path.MakeUpper();
if (path == "LOCAL")
path = "";
else
path = MachineName;
pApp = (CWTclientApp *)AfxGetApp();
if (UseOPCENUM)
return (pApp->GetServerListFromOPCENUM(path));
else
return (pApp->GetServerListFromRegistry());
}
知道为什么会出现此错误吗?看起来 OPCenum.exe 没有在我的机器(本地服务器)上枚举我的 OPC 服务器 运行,但为什么呢? (下载的应用程序有,我的没有)
谢谢!!!!
编辑---------------------------------------- ---------------------------- CreateServerEnumerator():
IOPCServerList *CWTclientApp::CreateServerEnumerator (CString PathName)
{
HRESULT r2;
MULTI_QI mqi;
COSERVERINFO sin, *sinptr;
DWORD clsctx;
// set up server info
//
if (PathName.GetLength() > 0)
{
sinptr = &sin;
sin.dwReserved1 = 0;
sin.dwReserved2 = 0;
sin.pwszName = WSTRFromCString (PathName, FALSE);
sin.pAuthInfo = 0;
clsctx = CLSCTX_REMOTE_SERVER;
}
else
{
sinptr = 0; // pointer should be NULL if local
clsctx = CLSCTX_LOCAL_SERVER;
}
// set up mqi
//
mqi.pIID = &IID_IOPCServerList;
mqi.hr = 0;
mqi.pItf = 0;
r2 = CoCreateInstanceEx(CLSID_OPCServerList, NULL,
clsctx, sinptr, 1, &mqi);
if (PathName.GetLength() > 0)
WSTRFree (sin.pwszName, FALSE);
if (FAILED(r2) || FAILED(mqi.hr))
return (NULL);
return (IOPCServerList*)mqi.pItf;
}
在调用任何其他 COM 函数之前,只需在使用 COM (OPC Classic) 的每个线程中调用 CoInitialize (https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543(v=vs.85).aspx) or CoInitializeEx (https://msdn.microsoft.com/en-us/library/windows/desktop/ms695279(v=vs.85).aspx)。
CoInitialize 已过时,因此您应该使用 CoInitializeEx。
Microsoft 在他们的文档中对此函数说:"CoInitializeEx must be called at least once, and is usually called only once, for each thread that uses the COM library".