错误:WTclient - 无法执行OPCENUM

时间:2015-01-19 17:11:49

标签: c++ opc

我正在编写一个OPC客户端来获取和发送标签到OPC服务器,我有这个错误:      WTclient - 无法执行OPCENUM 我的代码非常简单,我正在关注我在互联网上找到的完美工作的示例应用程序。 我的程序完美编译并运行,但是当达到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服务器,但为什么? (下载的应用程序确实与我的相关)

感谢!!!!

EDIT ---------------------------------------------- -------------------------- 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;
}

1 个答案:

答案 0 :(得分:0)

在调用任何其他COM函数之前,只需在使用COM(OPC Classic)的每个线程中调用CoInitialize(https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543(v=vs.85).aspx)或CoInitializeEx(https://msdn.microsoft.com/en-us/library/windows/desktop/ms695279(v=vs.85).aspx)。

CoInitialize已经过时,因此您应该使用CoInitializeEx。

微软在他们的文档中说这个函数:“对于每个使用COM库的线程,CoInitializeEx必须至少被调用一次,并且通常只被调用一次”。