ITPub博客

首页 > 应用开发 > IT综合 > VC动态调用DLL

VC动态调用DLL

原创 IT综合 作者:hljhrbsjf 时间:2007-04-17 21:46:34 0 删除 编辑
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中调用这三个函数,实现代码如下:

HMODULE module = LoadLibrary("CyTangTP.dll");
typedef
long (CALLBACK* pOpen)(int, int);//注意这里
typedef long (CALLBACK* pSend)(long,CString,CString);//注意这里
typedef int (CALLBACK* pClose)(long);//注意这里

if (module)
{
//调用OpenComm
pOpen Open = (pOpen)GetProcAddress(module,"OpenComm");
long port;
if(Open!=NULL)
{
port
= Open(1,9600);
MessageBox(
"Open");
}

else
return;

//调用SendMsg
pSend Send = (pSend)GetProcAddress(module,"SendMsg");
int nSend = 1;
if(Send!=NULL)
{
nSend
= Send(port,"0,20,2,1,1,1,1,0,1","RabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN12345678901234567890欢迎光临东方信通科技");
CString str;
str.Format(
"%d",nSend);
MessageBox(str);
}


//调用CloseComm
pClose Close = (pClose)GetProcAddress(module,"CloseComm");
int nClose;
if(Close!=NULL)
{
nClose
= Close(port);
CString str;
str.Format(
"%d",nClose);
MessageBox(str);
}


FreeLibrary(module);
}

[@more@]

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

请登录后发表评论 登录
全部评论
  • 博文量
    114
  • 访问量
    544669