VC动态调用DLL

CyTangTP.dll 共提供三个函数,此文件拷贝在运行目录下。
在VB环境下声明如下:
在Module1中定义动态库函数;
Declare Function OpenComm Lib "CyTangTP.dll" (ByVal ComNo As Integer, ByVal Baud As Integer) As Long
Declare Function CloseComm Lib "CyTangTP.dll" (ByVal Handle_Of_Com As Long) As Integer
Declare Function SendMsg Lib "CyTangTP.dll" (ByVal Handle_Of_Com As Long, ByVal TPSet As String, ByVal Msg As String) As Integer

现在,我们要在VC中调用这三个函数,实现代码如下:

None.gif HMODULE module = LoadLibrary( " CyTangTP.dll " );
None.gif typedef long (CALLBACK * pOpen)( int , int ); // 注意这里
None.gif typedef long (CALLBACK * pSend)( long ,CString,CString); // 注意这里
None.gif typedef int (CALLBACK * pClose)( long ); // 注意这里
None.gif
None.gif if (module)
ExpandedBlockStart.gifContractedBlock.gif ... {
InBlock.gif//调用OpenComm
InBlock.gif pOpen Open = (pOpen)GetProcAddress(module,"OpenComm");
InBlock.giflong port;
InBlock.gifif(Open!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif port = Open(1,9600);
InBlock.gif MessageBox("Open");
ExpandedSubBlockEnd.gif }

InBlock.gifelse
InBlock.gifreturn;
InBlock.gif
InBlock.gif//调用SendMsg
InBlock.gif pSend Send = (pSend)GetProcAddress(module,"SendMsg");
InBlock.gifint nSend = 1;
InBlock.gifif(Send!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif nSend = Send(port,"0,20,2,1,1,1,1,0,1","RabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN12345678901234567890欢迎光临东方信通科技");
InBlock.gif CString str;
InBlock.gif str.Format("%d",nSend);
InBlock.gif MessageBox(str);
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif//调用CloseComm
InBlock.gif pClose Close = (pClose)GetProcAddress(module,"CloseComm");
InBlock.gifint nClose;
InBlock.gifif(Close!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif nClose = Close(port);
InBlock.gif CString str;
InBlock.gif str.Format("%d",nClose);
InBlock.gif MessageBox(str);
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif FreeLibrary(module);
ExpandedBlockEnd.gif }

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/631872/viewspace-911296/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/631872/viewspace-911296/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值