写常规dll之悟

 为了写一个读取文件内容的dll,通过将文件路径传给dll函数进行调用。dll的编写实现如下:

1.dll生成

两个文件,头文件dataIO.h和实现文件dataIO.cpp,

其中dataIO.h如下

#include <vector>
using namespace std;

struct _declspec(dllexport) SMStruct{   //此导出结构体用于存放文件每一行的信息
	char mobile[15];
	//char date[9];
	char datatime[20];
	char comand[15];
};

struct _declspec(dllexport) SMSVecStruct{  //此导出结构体用于存放整个文件所有行的信息
	int smsN;
	vector<SMStruct> smsVector;
};

extern "C" _declspec(dllexport) SMSVecStruct readSMS(char *filename);//导出读取文件内容函数


dataIO.cpp文件内容如下

#include "dataIO.h"
#include <fstream>
#include <iostream>


extern "C" _declspec(dllexport) SMSVecStruct readSMS(char *filename)
{
	fstream smsFile;
	locale::global(std::locale(""));
	smsFile.open(filename,ios::app|ios::in);

	char buf[256];
	int smsn=0;

	SMSVecStruct smsVec;

	smsFile>>buf;
	while(!smsFile.eof())
	{
		smsn++;

		SMStruct sms;
		sms.mobile[0]='\0';
		sms.datatime[0]='\0';
		sms.comand[0]='\0';

		cout<<buf<<' ';
		strcat(sms.mobile,buf);

		smsFile>>buf;
		cout<<buf<<' ';
		strcat(sms.datatime,buf);

		smsFile>>buf;
		cout<<buf<<endl;
		strcat(sms.comand,buf);

		smsFile>>buf;

		smsVec.smsVector.push_back(sms);
	}

	smsVec.smsN=smsn;


	smsFile.close();

	return smsVec;
}

 


2.调用dll

上面的代码实现就是用于建立所需dll文件即dataIO.dll,将此dll文件放到你所新建的工程目录的debug中以供你的程序来调用,在main函数之前加入

#include "dataIO.h"  (这句是因为有结构体的导出,所以要包含上面相关的头文件)

struct _declspec(dllexport) SMStruct; (这句是结构体SMStruct的导出)
struct _declspec(dllexport) SMSVecStruct;(这句是结构体SMSVecStruct的导出)

对于函数可以有多种方式导出

 

main函数文件代码如下

#include <iostream>
#include <Windows.h>
using namespace std;

#include "dataIO.h"

struct _declspec(dllexport) SMStruct;
struct _declspec(dllexport) SMSVecStruct;

int main()
{
	typedef SMSVecStruct (*pReadSMS)(char *filename);
	pReadSMS readSMS=NULL;
	HINSTANCE hDll;
	//wstring dllFile="dataIO.dll";
	hDll=LoadLibrary("dataIO.dll");
	readSMS=(pReadSMS)GetProcAddress(hDll,"readSMS");

	if (readSMS)
	{
		SMSVecStruct smsVec=readSMS("G:\\znc\\c&c++\\allTest\\allTest\\Debug\\msgs.txt");
		//cout<<"调用dll后s="<<s<<endl;
	}

	FreeLibrary(hDll);

	getchar();
	return 0;
}


 



 

 

 

//if DATAPERIOD>=1 and dataperiod<=8 then begin if DATAPERIOD=0 then minbi:="fbase.var2#min1"; if DATAPERIOD=1 then minbi:="fbase.var2#min5"; if DATAPERIOD=2 then minbi:="fbase.var2#min30"; if DATAPERIOD=3 then minbi:="fbase.var2#min60"; if DATAPERIOD=4 then minbi:="fbase.var2#day"; if DATAPERIOD=5 then minbi:="fbase.var2#day"; if DATAPERIOD=6 then minbi:="fbase.var2#week"; if DATAPERIOD=7 then minbi:="fbase.var2#month"; if DATAPERIOD=8 then minbi:="fbase.var2#year"; var1:="chzhshch@kxian"; var2:="chzhshch@bi";{处理笔关系} pre:=2; {设置小数点位数} if STKTYPE=0 then pre:=1; if STKTYPE=11 then pre:=3; a1:=barslast(var20); a2:=ref(var2,a1); aa1:=ref(a1+1,1); aa2:=ref(a2,1); a3:if (aa2>0,l-ref(h,aa1),h-ref(l,aa1)),linethick0;{计算点位差} drawnumber(var2=1,h,h,pre),pxup20,color3300ff; {显示数字} drawnumber(var2=-1,l,l,pre),pxdn5,colorffff00,align0; //显示周期 drawtext(var2=1,h,'T'),pxup65,coloryellow,align1; drawnumber(var2=1,h,aa1,0),coloryellow,pxup65,align2; drawtext(var2=-1,l,'T'),pxdn50,coloryellow,align1; drawnumber(var2=-1,l,aa1,0),coloryellow,pxdn50,align2; var3:="chzhshch@tongji"(minbi);{同级别分解} zg:="chzhshch@zg"(var3);//一定要先调用ZG zd:="chzhshch@zd"(var3); //以背景块的形式画中枢 stickline(zg>zd,zg,zd,10,0),color000055; drawline(var2=1,h,var2=-1,l,0),linedot,coloryellow; drawline(var2=-1,l,var2=1,h,0),linedot,coloryellow; drawline(var3=1,h,var3=-1,l,0),colorFF6600; drawline(var3=-1,l,var3=1,h,0),colorFF6600; //以框的形式画中枢 stickline(zg>zd and zgref(zg,1),zg,zd,2,0),COLORMAGENTA; stickline(zg>zd and zgrefx(zg,1),zg,zd,2,0),COLORMAGENTA; drawline(zg>zd and zgref(zg,1),zg,zg>zd and zgrefx(zg,1),zg,0),COLORMAGENTA; drawline(zg>zd and zdref(zd,1),zd,zg>zd and zdrefx(zd,1),zd,0),COLORMAGENTA; end; //kline(o,h,l,c,1); STICKLINE(C>=O,O,C,8,0),COLOR0000AA; STICKLINE(C>=O,O,C,7,0),COLOR0011BB; STICKLINE(C>=O,O,C,6,0),COLOR0022CC; STICKLINE(C>=O,O,C,5,0),COLOR0033DD; STICKLINE(C>=O,O,C,4,0),COLOR0044EE; STICKLINE(C>=O,O,C,3,0),COLOR0055FF; STICKLINE(C>=O,O,C,2,0),COLOR0066FF; STICKLINE(C>=O,O,C,1,0),COLOR0077FF; STICKLINE(C>=O,C,H,0,0),colorred; STICKLINE(C>=O,O,L,0,0),colorred; STICKLINE(C<O,C,O,8,0),COLOR00AA00; STICKLINE(C<O,C,O,7,0),COLOR00BB11; STICKLINE(C<O,C,O,6,0),COLOR00CC22; STICKLINE(C<O,C,O,5,0),COLOR00DD33; STICKLINE(C<O,C,O,4,0),COLOR00EE44; STICKLINE(C<O,C,O,3,0),COLOR00FF55; STICKLINE(C<O,C,O,2,0),COLOR00FF66; STICKLINE(C<O,C,O,1,0),COLOR00FF77; STICKLINE(Cma5,1,0); tmp2:=if(c>ma13,1,0); tmp3:=if(c>ma21,1,0); tmp4:=if(c>ma34,1,0); tmp5:=if(c>ma55,1,0); tmp6:=if(c>ma89,1,0); tmp7:=if(c>ma144,1,0); tmp8:=if(c>ma233,1,0); LEI:tmp1+tmp2+tmp3+tmp4+tmp5+tmp6+tmp7+tmp8+1,linethick0; N1:=BARSLAST(var20); x1:=llv(LEI,N2); x2:=HHV(LEI,N1); 类别:if(N1>N2,X1,X2),LINETHICK0; DRAWTEXT(ISLASTBAR,h*1.1,'类别 '+NUMTOSTR(类别,0) +' ')ALIGN2,LINETHICK3,Color00fff {原文链接:http://www.55188.com/thread-6885035-1-1.html}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值