OpenOPC.OPCError: Dispatch: 无效的类字符串
时间: 2024-05-26 19:14:28 浏览: 8
这个错误通常是因为你的代码中指定的 CLSID 或 ProgID 不正确导致的。请确保你的代码中指定的 CLSID 或 ProgID 是正确的,并且与你的 OPC 服务器相匹配。你可以尝试使用工具如 OpcEnum 来查看你的 OPC 服务器的 CLSID 和 ProgID 信息,以便确认你的代码中使用的信息是否正确。同时,也要确保你的 OPC 服务器已经正确地安装和注册,以便于你的客户端能够正确地连接到它。
相关问题
vc2019开发opcserver的例子代码
下面是一个使用VC2019开发OPC Server的示例代码:
```cpp
#include <windows.h>
#include <tchar.h>
#include <opcda.h>
#include <opcerror.h>
#include <atlbase.h>
class CMyOPCServer : public CComObjectRootEx<CComMultiThreadModel>,
public IOPCServer
{
public:
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
BEGIN_COM_MAP(CMyOPCServer)
COM_INTERFACE_ENTRY(IOPCServer)
END_COM_MAP()
// IOPCServer methods
STDMETHODIMP AddGroup(LPCWSTR szName, BOOL bActive, DWORD dwRequestedUpdateRate,
OPCHANDLE hClientGroup, LONG* pTimeBias, FLOAT* pPercentDeadband,
DWORD dwLCID, OPCHANDLE* phServerGroup, DWORD* pRevisedUpdateRate,
REFIID riid, LPUNKNOWN* ppUnk)
{
// TODO: Implement AddGroup method
return E_NOTIMPL;
}
STDMETHODIMP GetErrorString(HRESULT dwError, LCID dwLocale, LPWSTR* ppString)
{
// TODO: Implement GetErrorString method
return E_NOTIMPL;
}
STDMETHODIMP GetGroupByName(LPCWSTR szName, REFIID riid, LPUNKNOWN* ppUnk)
{
// TODO: Implement GetGroupByName method
return E_NOTIMPL;
}
STDMETHODIMP GetStatus(OPCSERVERSTATUS** ppServerStatus)
{
// TODO: Implement GetStatus method
return E_NOTIMPL;
}
STDMETHODIMP RemoveGroup(OPCHANDLE hServerGroup, BOOL bForce)
{
// TODO: Implement RemoveGroup method
return E_NOTIMPL;
}
STDMETHODIMP CreateGroupEnumerator(DWORD dwScope, REFIID riid,
LPUNKNOWN* ppUnk)
{
// TODO: Implement CreateGroupEnumerator method
return E_NOTIMPL;
}
};
int _tmain(int argc, TCHAR* argv[])
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr))
{
_tprintf(_T("Failed to initialize COM library: 0x%08X\n"), hr);
return -1;
}
hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_NONE,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if (FAILED(hr))
{
_tprintf(_T("Failed to initialize security: 0x%08X\n"), hr);
CoUninitialize();
return -1;
}
CComObject<CMyOPCServer>* pServer;
hr = CComObject<CMyOPCServer>::CreateInstance(&pServer);
if (FAILED(hr))
{
_tprintf(_T("Failed to create OPC server object: 0x%08X\n"), hr);
CoUninitialize();
return -1;
}
hr = pServer->QueryInterface(IID_IOPCServer, (LPVOID*)&pServer);
if (FAILED(hr))
{
_tprintf(_T("Failed to get IOPCServer interface: 0x%08X\n"), hr);
CoUninitialize();
return -1;
}
// TODO: Register OPC server
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// TODO: Unregister OPC server
pServer->Release();
CoUninitialize();
return 0;
}
```
请注意,此代码仅提供了一个框架,需要根据具体的OPC Server实现进行修改和扩展。例如,您需要实现IOPCServer接口中的所有方法,并在TODO注释的位置添加注册和注销OPC服务器的代码。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)