有关编写DLL文件问题,请大家帮忙看看!

spring0707 2009-03-11 02:19:43
源代码:
#include "stdafx.h"
#include <atlbase.h>
#include <objbase.h>
#include <iostream>
#include "opc.h"
#include "OPCdll.h"
#include "opc_i.c"
#include <string>
using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define OPC_SERVER_NAME L"OPC.SimaticNET" // e.g.: L"OPC.Evaluation:HV supply.1"
#define XVAL fltVal
unsigned pp[10];
BEGIN_MESSAGE_MAP(COPCdllApp, CWinApp)
END_MESSAGE_MAP()
COPCdllApp::COPCdllApp()
{
}
COPCdllApp theApp;
BOOL COPCdllApp::InitInstance()
{
COleObjectFactory::RegisterAll();
return TRUE;
}

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return AfxDllGetClassObject(rclsid, riid, ppv);
}

STDAPI DllCanUnloadNow(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return AfxDllCanUnloadNow();
}

STDAPI DllRegisterServer(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
COleObjectFactory::UpdateRegistryAll();
return S_OK;
}

IOPCServer* InstantiateServer(wchar_t ServerName[])
{
CLSID CLSID_OPCServer;
HRESULT hr;

hr = CLSIDFromString(ServerName, &CLSID_OPCServer);
_ASSERT(!FAILED(hr));
LONG cmq = 1;
MULTI_QI queue[1] =
{{&IID_IOPCServer,
NULL,
0}};
hr = CoCreateInstanceEx(CLSID_OPCServer, NULL, CLSCTX_SERVER,
/*&CoServerInfo*/NULL, cmq, queue);
_ASSERT(!hr);
return(IOPCServer*) queue[0].pItf;
}

void AddTheGroup(IOPCServer* pIOPCServer, IOPCItemMgt* &pIOPCItemMgt,
OPCHANDLE& hServerGroup)
{
DWORD dwUpdateRate = 0;
OPCHANDLE hClientGroup = 0;

HRESULT hr = pIOPCServer->AddGroup(/*szName*/ L"Group1",
/*bActive*/ FALSE,
/*dwRequestedUpdateRate*/ dwUpdateRate,
/*hClientGroup*/ hClientGroup,
/*pTimeBias*/ 0,
/*pPercentDeadband*/ 0,
/*dwLCID*/0,
/*phServerGroup*/&hServerGroup,
&dwUpdateRate,
/*riid*/ IID_IOPCItemMgt,
/*ppUnk*/ (IUnknown**) &pIOPCItemMgt);
_ASSERT(!FAILED(hr));
}

这个DLL可以运行,但是在调用Start7()时非常耗时间,导致我的界面接受PLC数据时经常卡住死机!!!由于我初次编写DLL文件,这些代码都是按照网上一步一步写出来的,不知怎么改进,所以恳请各位高手指点一下!!谢谢!
...全文
141 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jnwsx 2009-03-14
  • 打赏
  • 举报
回复
看不懂,太繁琐了
spring0707 2009-03-11
  • 打赏
  • 举报
回复
因为我不知道问题出在哪里,所以干脆附上整个源代码,请高手们帮忙指点一下,主要是调用start7()这个接口时太慢了,谢谢啦!
annywoody 2009-03-11
  • 打赏
  • 举报
回复
你贴这么多代码想说什么?
spring0707 2009-03-11
  • 打赏
  • 举报
回复
void ReadItem(IUnknown* pGroupIUnknown, OPCHANDLE hServerItem, VARIANT& varValue)
{
OPCITEMSTATE* pValue = NULL;

IOPCSyncIO* pIOPCSyncIO;
pGroupIUnknown->QueryInterface(__uuidof(pIOPCSyncIO), (void**) &pIOPCSyncIO);

HRESULT* pErrors = NULL; //to store error code(s)
HRESULT hr = pIOPCSyncIO->Read(OPC_DS_DEVICE, 1, &hServerItem, &pValue, &pErrors);
_ASSERT(!hr);
_ASSERT(pValue!=NULL);

varValue = pValue[0].vDataValue;

CoTaskMemFree(pErrors);
pErrors = NULL;

CoTaskMemFree(pValue);
pValue = NULL;

pIOPCSyncIO->Release();
}

void WriteItem(IUnknown* pGroupIUnknown, OPCHANDLE hServerItem, CString strSend)
{

IOPCSyncIO* pIOPCSyncIO;
pGroupIUnknown->QueryInterface(__uuidof(pIOPCSyncIO), (void**) &pIOPCSyncIO);

HRESULT* pErrors = NULL; //to store error code(s)
COleVariant WriteValue;
WriteValue = strSend;// strSend为待写的数据
WriteValue.ChangeType(VT_R4);
HRESULT hr =pIOPCSyncIO->Write(1, &hServerItem, WriteValue, &pErrors);
_ASSERT(!hr);


CoTaskMemFree(pErrors);
pErrors = NULL;

pIOPCSyncIO->Release();
}
void RemoveItem(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE hServerItem)
{
OPCHANDLE hServerArray[1];
hServerArray[0] = hServerItem;

HRESULT* pErrors; // to store error code(s)
HRESULT hr = pIOPCItemMgt->RemoveItems(1, hServerArray, &pErrors);
_ASSERT(!hr);

CoTaskMemFree(pErrors);
pErrors = NULL;
}


void RemoveGroup (IOPCServer* pIOPCServer, OPCHANDLE hServerGroup)
{
HRESULT hr = pIOPCServer->RemoveGroup(hServerGroup, TRUE);
_ASSERT(!hr);
}




extern "C"_declspec(dllexport) unsigned int Start(int x)
{

return pp[x];
}



extern "C"_declspec(dllexport) unsigned int Start7(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
IOPCServer* pIOPCServer = NULL; //pointer to IOPServer interface
IOPCItemMgt* pIOPCItemMgt = NULL; //pointer to IOPCItemMgt interface

OPCHANDLE hServerGroup; // server handle to the group
OPCHANDLE hServerItem; // server handle to the item


CoInitialize(NULL);

pIOPCServer = InstantiateServer(OPC_SERVER_NAME);

AddTheGroup(pIOPCServer, pIOPCItemMgt, hServerGroup);

VARIANT varValue; //to store the read value
VariantInit(&varValue);
//读第一个字
AddTheItem1(pIOPCItemMgt, hServerItem);
ReadItem(pIOPCItemMgt, hServerItem, varValue);
pp[1]=varValue.XVAL;
RemoveItem(pIOPCItemMgt, hServerItem);
//读第二个字
AddTheItem2(pIOPCItemMgt, hServerItem);
ReadItem(pIOPCItemMgt, hServerItem, varValue);
pp[2]=varValue.XVAL;
RemoveItem(pIOPCItemMgt, hServerItem);
//读第三个字
AddTheItem3(pIOPCItemMgt, hServerItem);
ReadItem(pIOPCItemMgt, hServerItem, varValue);
pp[3]=varValue.XVAL;
RemoveItem(pIOPCItemMgt, hServerItem);
//读第四个字
AddTheItem4(pIOPCItemMgt, hServerItem);
ReadItem(pIOPCItemMgt, hServerItem, varValue);
pp[4]=varValue.XVAL;
RemoveItem(pIOPCItemMgt, hServerItem);
//读第五个字
AddTheItem5(pIOPCItemMgt, hServerItem);
ReadItem(pIOPCItemMgt, hServerItem, varValue);
pp[5]=varValue.XVAL;
RemoveItem(pIOPCItemMgt, hServerItem);
//读第六个字
AddTheItem6(pIOPCItemMgt, hServerItem);
ReadItem(pIOPCItemMgt, hServerItem, varValue);
pp[6]=varValue.XVAL;
RemoveItem(pIOPCItemMgt, hServerItem);

RemoveGroup(pIOPCServer, hServerGroup);

pIOPCItemMgt->Release();
pIOPCServer->Release();

CoUninitialize();
return 0;
}

extern "C"_declspec(dllexport) void Write(int qq)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
IOPCServer* pIOPCServer = NULL; //pointer to IOPServer interface
IOPCItemMgt* pIOPCItemMgt = NULL; //pointer to IOPCItemMgt interface

OPCHANDLE hServerGroup; // server handle to the group
OPCHANDLE hServerItem; // server handle to the item


CoInitialize(NULL);

pIOPCServer = InstantiateServer(OPC_SERVER_NAME);

AddTheGroup(pIOPCServer, pIOPCItemMgt, hServerGroup);

AddTheItem2(pIOPCItemMgt, hServerItem);

CString varValue; //to stor the read value
varValue.Format("%d",qq);
WriteItem(pIOPCItemMgt, hServerItem, varValue);
RemoveItem(pIOPCItemMgt, hServerItem);

RemoveGroup(pIOPCServer, hServerGroup);

pIOPCItemMgt->Release();
pIOPCServer->Release();

CoUninitialize();
}
spring0707 2009-03-11
  • 打赏
  • 举报
回复
void AddTheItem1(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE& hServerItem)//第一个I字,地址为IW0
{
HRESULT hr;
OPCITEMDEF ItemArray[1] =
{{
/*szAccessPath*/ L"",
/*szItemID*/ L"S7:[S7 connection_1]IW0",
/*bActive*/ FALSE,
/*hClient*/ 1,
/*dwBlobSize*/ 0,
/*pBlob*/ NULL,
/*vtRequestedDataType*/ VT_R4,
/*wReserved*/0
}};

OPCITEMRESULT* pAddResult=NULL;
HRESULT* pErrors = NULL;
hr = pIOPCItemMgt->AddItems(1, ItemArray, &pAddResult, &pErrors);
_ASSERT(!hr);
hServerItem = pAddResult[0].hServer;
CoTaskMemFree(pAddResult->pBlob);

CoTaskMemFree(pAddResult);
pAddResult = NULL;

CoTaskMemFree(pErrors);
pErrors = NULL;
}

void AddTheItem2(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE& hServerItem)//第二个I字,地址为IW2
{
HRESULT hr;

OPCITEMDEF ItemArray[1] =
{{
/*szAccessPath*/ L"",
/*szItemID*/ L"S7:[S7 connection_1]IW2",
/*bActive*/ FALSE,
/*hClient*/ 1,
/*dwBlobSize*/ 0,
/*pBlob*/ NULL,
/*vtRequestedDataType*/ VT_R4,
/*wReserved*/0
}};

OPCITEMRESULT* pAddResult=NULL;
HRESULT* pErrors = NULL;

hr = pIOPCItemMgt->AddItems(1, ItemArray, &pAddResult, &pErrors);
_ASSERT(!hr);

hServerItem = pAddResult[0].hServer;

CoTaskMemFree(pAddResult->pBlob);

CoTaskMemFree(pAddResult);
pAddResult = NULL;

CoTaskMemFree(pErrors);
pErrors = NULL;
}


void AddTheItem3(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE& hServerItem)//第三个I字,地址为IW4
{
HRESULT hr;
OPCITEMDEF ItemArray[1] =
{{
/*szAccessPath*/ L"",
/*szItemID*/ L"S7:[S7 connection_1]IW4",
/*bActive*/ FALSE,
/*hClient*/ 1,
/*dwBlobSize*/ 0,
/*pBlob*/ NULL,
/*vtRequestedDataType*/ VT_R4,
/*wReserved*/0
}};

OPCITEMRESULT* pAddResult=NULL;
HRESULT* pErrors = NULL;

hr = pIOPCItemMgt->AddItems(1, ItemArray, &pAddResult, &pErrors);
_ASSERT(!hr);

hServerItem = pAddResult[0].hServer;

CoTaskMemFree(pAddResult->pBlob);

CoTaskMemFree(pAddResult);
pAddResult = NULL;

CoTaskMemFree(pErrors);
pErrors = NULL;
}

void AddTheItem4(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE& hServerItem)
{
HRESULT hr;

OPCITEMDEF ItemArray[1] =
{{
/*szAccessPath*/ L"",
/*szItemID*/ L"S7:[S7 Connection_1]IW6",
/*bActive*/ FALSE,
/*hClient*/ 1,
/*dwBlobSize*/ 0,
/*pBlob*/ NULL,
/*vtRequestedDataType*/ VT_R4,
/*wReserved*/0
}};


OPCITEMRESULT* pAddResult=NULL;
HRESULT* pErrors = NULL;

hr = pIOPCItemMgt->AddItems(1, ItemArray, &pAddResult, &pErrors);
_ASSERT(!hr);

hServerItem = pAddResult[0].hServer;

CoTaskMemFree(pAddResult->pBlob);

CoTaskMemFree(pAddResult);
pAddResult = NULL;

CoTaskMemFree(pErrors);
pErrors = NULL;
}


void AddTheItem5(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE& hServerItem)//第一个O字节地址为QB12,1
{
HRESULT hr;

OPCITEMDEF ItemArray[1] =
{{
/*szAccessPath*/ L"",
/*szItemID*/ L"S7:[S7 Connection_1]QW0",
/*bActive*/ FALSE,
/*hClient*/ 1,
/*dwBlobSize*/ 0,
/*pBlob*/ NULL,
/*vtRequestedDataType*/ VT_R4,
/*wReserved*/0
}};

OPCITEMRESULT* pAddResult=NULL;
HRESULT* pErrors = NULL;

hr = pIOPCItemMgt->AddItems(1, ItemArray, &pAddResult, &pErrors);
_ASSERT(!hr);

hServerItem = pAddResult[0].hServer;

CoTaskMemFree(pAddResult->pBlob);

CoTaskMemFree(pAddResult);
pAddResult = NULL;

CoTaskMemFree(pErrors);
pErrors = NULL;
}

void AddTheItem6(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE& hServerItem)//第二个O字节地址为QB12,1
{
HRESULT hr;

OPCITEMDEF ItemArray[1] =
{{
/*szAccessPath*/ L"",
/*szItemID*/ L"S7:[S7 Connection_1]QW2",
/*bActive*/ FALSE,
/*hClient*/ 1,
/*dwBlobSize*/ 0,
/*pBlob*/ NULL,
/*vtRequestedDataType*/ VT_R4,
/*wReserved*/0
}};


OPCITEMRESULT* pAddResult=NULL;
HRESULT* pErrors = NULL;

hr = pIOPCItemMgt->AddItems(1, ItemArray, &pAddResult, &pErrors);
_ASSERT(!hr);

hServerItem = pAddResult[0].hServer;

CoTaskMemFree(pAddResult->pBlob);

CoTaskMemFree(pAddResult);
pAddResult = NULL;

CoTaskMemFree(pErrors);
pErrors = NULL;
}

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧