在VC中使用DLL一般都是采用動態聲明的方式,函數說明中給出的是Delphi的函數原型,
在VC中聲明時只要注意一下類型的對應即可,Delphi中的longint類型對應VC中的int類型
Delphi中的Pchar對應VC中的char* ,下面給出主要函數的聲明:
在使用的文件的cpp中聲明一個句柄:
HINSTANCE hinstDLL;
用來標識導入的動態鏈接庫。
1)、按下例說明聲明相關各個函數:(在cpp文件的頭處聲明)
typedef int (_stdcall *pOpen)(int nport, int BaudRate, int DataBits, char* Parity, int StopBits, char* User);
typedef int (_stdcall *pClose)(int nport);
typedef int (_stdcall *pSetDelay)(int value);
typedef int (_stdcall *pComTrue)(int nport);
typedef int (_stdcall *pComWork)(int nport);
typedef int (_stdcall *pRs)(int nport, int node);
typedef int (_stdcall *pWs)(int nport, int node, int State);
typedef int (_stdcall *pBitForced)(int nport, int node, int address, int Bit, int value, char* Area);
typedef int (_stdcall *pCancelAllF)(int nport, int node);
typedef int (_stdcall *pReadBit)(int nport, int node, int address, int Bit, int Count, char* Area, int* RxdBuffer);
typedef int (_stdcall *pReadInt)(int nport, int node, int address, int Count, char* Area, int* RxdBuffer);
typedef int (_stdcall *pWriteBit)(int nport, int node, int address, int Bit, int Count, char* Area, int* TxdBuffer);
typedef int (_stdcall *pWriteInt)(int nport, int node, int address, int Count, char* Area, int* TxdBuffer);
typedef int (_stdcall *pSet)(int nport, int node, int address, int Bit, char* Area);
typedef int (_stdcall *pReSet)(int nport, int node, int address, int Bit, char* Area);
typedef int (_stdcall *pReadDInt)(int nport, int node, int address, int Count, int* RxdBuffer);
typedef int (_stdcall *pReadFloat)(int nport, int node, int address, int Count, float* RxdBuffer);
typedef int (_stdcall *pWriteDInt)(int nport, int node, int address, int Count, int* TxdBuffer);
typedef int (_stdcall *pWriteFloat)(int nport, int node, int address, int Count, float* TxdBuffer);
typedef int (_stdcall *pBitBin)(int value, int Bitaddress);
typedef int (_stdcall *p32I_16h)(int value);
typedef int (_stdcall *p32I_16l)(int value);
typedef int (_stdcall *p16I_32I)(int valueH, int valueL);
typedef int (_stdcall *p32f_16h)(float value);
typedef int (_stdcall *p32f_16l)(float value);
typedef float (_stdcall *p16I_32f)(int valueH, int valueL);
typedef int (_stdcall *pInt16Unsign)(int value);
2)、建立動態鏈接庫的新函數名:(在cpp文件的頭處聲明)
pOpen mOpen;
pClose mClose;
pSetDelay mSetDelay;
pComTrue mComTrue;
pComWork mComWork;
pRs mRs;
pWs mWs;
pBitForced mBitForced;
pCancelAllF mCancelAllF;
pReadBit mReadBit;
pReadInt mReadInt;
pWriteBit mWriteBit;
pWriteInt mWriteInt;
pSet mSet;
pReSet mReSet;
pReadDInt mReadDInt;
pReadFloat mReadFloat;
pWriteDInt mWriteDInt;
pWriteFloat mWriteFloat;
pBitBin mBitBin;
p32I_16h m32I_16h;
p32I_16l m32I_16l;
p16I_32I m16I_32I;
p32f_16h m32f_16h;
p32f_16l m32f_16l;
p16I_32f m16I_32f;
pInt16Unsign mInt16Unsign;
3)、導入動態鏈接庫,如例所示:(在cpp文件的OnInitDialog過程建立):
hinstDLL = LoadLibrary("Omron_fins.dll");
4)、判斷dll文件是否存在并聲明并建立動態鏈接庫中的函數與新函數名的對應關系,
如下:(在cpp文件的OnInitDialog過程建立):
if (hinstDLL)
{
mOpen = (pOpen)GetProcAddress (hinstDLL,"finsComOpen");
//mOpen = (pOpen)GetProcAddress (hinstDLL,"finsCvComOpen"); CV模式plc使用這個打開串口
mClose = (pClose)GetProcAddress (hinstDLL,"finsComClose");
mSetDelay = (pSetDelay)GetProcAddress (hinstDLL,"finsSetDelay");
mComTrue = (pComTrue)GetProcAddress (hinstDLL,"finsComTrue");
mComWork = (pComWork)GetProcAddress (hinstDLL,"finsComWork");
mRs = (pRs)GetProcAddress (hinstDLL,"finsPlcRs");
mWs = (pWs)GetProcAddress (hinstDLL,"finsPlcWs");
mBitForced = (pBitForced)GetProcAddress (hinstDLL,"finsBitForced");
mCancelAllF = (pCancelAllF)GetProcAddress (hinstDLL,"finsCancelAllF");
mReadBit = (pReadBit)GetProcAddress (hinstDLL,"finsReadBit");
mReadInt = (pReadInt)GetProcAddress (hinstDLL,"finsReadInt");
mWriteBit = (pWriteBit)GetProcAddress (hinstDLL,"finsWriteBit");
mWriteInt = (pWriteInt)GetProcAddress (hinstDLL,"finsWriteInt");
mSet = (pSet)GetProcAddress (hinstDLL,"finsSet");
mReSet = (pReSet)GetProcAddress (hinstDLL,"finsReSet");
mReadDInt = (pReadDInt)GetProcAddress (hinstDLL,"finsReadDInt");
mReadFloat = (pReadFloat)GetProcAddress (hinstDLL,"finsReadFloat");
mWriteDInt = (pWriteDInt)GetProcAddress (hinstDLL,"finsWriteDInt");
mWriteFloat = (pWriteFloat)GetProcAddress (hinstDLL,"finsWriteFloat");
mBitBin = (pBitBin)GetProcAddress (hinstDLL,"DecBitBin");
m32I_16h = (p32I_16h)GetProcAddress (hinstDLL,"Int32ToInt_16h");
m32I_16l = (p32I_16l)GetProcAddress (hinstDLL,"Int32ToInt_16l");
m16I_32I= (p16I_32I)GetProcAddress (hinstDLL,"Int16ToInt32");
m32f_16h = (p32f_16h)GetProcAddress (hinstDLL,"Float32ToInt_16h");
m32f_16l = (p32f_16l)GetProcAddress (hinstDLL,"Float32ToInt_16l");
m16I_32f= (p16I_32f)GetProcAddress (hinstDLL,"Int16ToFloat32");
mInt16Unsign = (pInt16Unsign)GetProcAddress (hinstDLL,"Int16Unsign");
AfxMessageBox("Omron_fins.dll已成功載入!");
}
else
{
AfxMessageBox("沒找到Omron_fins.dll!");
SendMessage(WM_CLOSE);
}
注:雙引號中為動態鏈接庫中的原有函數名。
函數中用到了char*型參數,這里介紹下char*與Cstring的相互轉換的函數:
(1)char*->CString
char* sz;
CString str;
str.Format("%s",sz); //可以用此函數將讀取的值轉成字符串
(2) CString -> char*
CString str;
char* sz = str.GetBuffer(0);//可將字符串轉成char*給函數賦值
5)、當不再需要使用DLL時記得關閉串口及釋放動態鏈接庫,(在OnDestroy事件中釋放)
if(hinstDLL)
{
int k = mComTrue(mnport);
if (k==1)
{
mClose(mnport);
}
FreeLibrary(hinstDLL);
}