在VC中使用DLL一般都是采用動態(tài)聲明的方式,函數(shù)說明中給出的是Delphi的函數(shù)原型,
 
在VC中聲明時只要注意一下類型的對應(yīng)即可,Delphi中的longint類型對應(yīng)VC中的int類型
 
Delphi中的Pchar對應(yīng)VC中的char* ,下面給出主要函數(shù)的聲明:
 
在使用的文件的cpp中聲明一個句柄:
 
HINSTANCE hinstDLL;
 
用來標(biāo)識導(dǎo)入的動態(tài)鏈接庫。
 
1)、按下例說明聲明相關(guān)各個函數(shù):(在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 *pReadBit)(int nport, int node, char* element, int address, int Bit);
typedef int (_stdcall *pReadByte)(int nport, int node, char* element, int address, int Count, int* RxdBuffer);
typedef int (_stdcall *pReadInt)(int nport, int node, char* element, int address, int Count, int* RxdBuffer);
typedef int (_stdcall *pReadDInt)(int nport, int node, char* element, int address, int Count, int* RxdBuffer);
typedef int (_stdcall *pReadFloat)(int nport, int node, char* element, int address, int Count, float* RxdBuffer);
 
typedef int (_stdcall *pBitWrite)(int nport, int node, char* element, int address, int Bit, int value);
typedef int (_stdcall *pBitEWrite)(int nport, int node, char* element, int address, int Bit, int value);
typedef int (_stdcall *pEbitCancel)(int nport, int node, char* element, int address, int Bit);
 
typedef int (_stdcall *pWriteByte)(int nport, int node, char* element, int address, int Count, int* TxdBuffer);
typedef int (_stdcall *pWriteInt)(int nport, int node, char* element, int address, int Count, int* TxdBuffer);
typedef int (_stdcall *pWriteDInt)(int nport, int node, char* element, int address, int Count, int* TxdBuffer);
typedef int (_stdcall *pWriteFloat)(int nport, int node, char* element, int address, int Count, float* TxdBuffer);
 
typedef int (_stdcall *pPlcRun)(int nport, int node);
typedef int (_stdcall *pPlcStop)(int nport, int node);
 
typedef int (_stdcall *pBitBin)(int value, int Bitaddress);
typedef int (_stdcall *p16I_8h)(int value);
typedef int (_stdcall *p16I_8l)(int value);
typedef int (_stdcall *p8I_16I)(int valueH, int valueL);
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);
 
2)、建立動態(tài)鏈接庫的新函數(shù)名:(在cpp文件的頭處聲明,上面的聲明之后)
 
pOpen mOpen; 
pClose mClose;
pSetDelay mSetDelay;
pComTrue mComTrue;
pComWork mComWork;
 
pReadBit mReadBit;
pReadByte mReadByte;
pReadInt mReadInt;
pReadDInt mReadDInt;
pReadFloat mReadFloat;
 
pBitWrite mBitWrite;
pBitEWrite mBitEWrite;
pEbitCancel mEbitCancel;
 
pWriteByte mWriteByte;
pWriteInt mWriteInt;
pWriteDInt mWriteDInt;
pWriteFloat mWriteFloat;
 
pPlcRun mPlcRun;
pPlcStop mPlcStop;
 
pBitBin mBitBin;
p16I_8h m16I_8h;
p16I_8l m16I_8l;
p8I_16I m8I_16I;
p32I_16h m32I_16h;
p32I_16l m32I_16l;
p16I_32I m16I_32I;
p32f_16h m32f_16h;
p32f_16l m32f_16l;
p16I_32f m16I_32f;
 
3)、導(dǎo)入動態(tài)鏈接庫,如例所示:(在cpp文件的OnInitDialog過程建立):
 
hinstDLL = LoadLibrary("S7200_PPI.dll");
 
4)、判斷dll文件是否存在并聲明并建立動態(tài)鏈接庫中的函數(shù)與新函數(shù)名的對應(yīng)關(guān)系,
 
如下:(在cpp文件的OnInitDialog過程建立):
 
if (hinstDLL)
 {
   mOpen = (pOpen)GetProcAddress (hinstDLL,"S7200ComOpen");
   mClose = (pClose)GetProcAddress (hinstDLL,"S7200ComClose");
   mSetDelay = (pSetDelay)GetProcAddress (hinstDLL,"S7200SetDelay");
   mComTrue = (pComTrue)GetProcAddress (hinstDLL,"S7200ComTrue");
   mComWork = (pComWork)GetProcAddress (hinstDLL,"S7200ComWork");
 
   mReadBit = (pReadBit)GetProcAddress (hinstDLL,"S7200BitRead");
   mReadByte = (pReadByte)GetProcAddress (hinstDLL,"S7200ByteRead");
   mReadInt = (pReadInt)GetProcAddress (hinstDLL,"S7200WordRead");
   mReadDInt = (pReadDInt)GetProcAddress (hinstDLL,"S7200DwordRead");
   mReadFloat = (pReadFloat)GetProcAddress (hinstDLL,"S7200FloatRead");
 
   mBitWrite = (pBitWrite)GetProcAddress (hinstDLL,"S7200BitWrite");
   mBitEWrite = (pBitEWrite)GetProcAddress (hinstDLL,"S7200BitEWrite");
   mEbitCancel = (pEbitCancel)GetProcAddress (hinstDLL,"S7200EbitCancel");
      
   mWriteByte = (pWriteByte)GetProcAddress (hinstDLL,"S7200ByteWrite");
   mWriteInt = (pWriteInt)GetProcAddress (hinstDLL,"S7200WordWrite");
   mWriteDInt = (pWriteDInt)GetProcAddress (hinstDLL,"S7200DwordWrite");
   mWriteFloat = (pWriteFloat)GetProcAddress (hinstDLL,"S7200FloatWrite");
  
   mPlcRun = (pPlcRun)GetProcAddress (hinstDLL,"S7200PlcRun");
   mPlcStop = (pPlcStop)GetProcAddress (hinstDLL,"S7200PlcStop"); 
      
   mBitBin = (pBitBin)GetProcAddress (hinstDLL,"DecBitBin");
   m16I_8h = (p16I_8h)GetProcAddress (hinstDLL,"Int16ToInt_8h");
   m16I_8l = (p16I_8l)GetProcAddress (hinstDLL,"Int16ToInt_8l");
   m8I_16I= (p8I_16I)GetProcAddress (hinstDLL,"Int8ToInt16");
   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");
  
   AfxMessageBox("S7200_PPI.dll已成功載入!");
 }
 else
 {
  AfxMessageBox("沒找到S7200_PPI.dll!");
  SendMessage(WM_CLOSE); 
 }
 
注:雙引號中為動態(tài)鏈接庫中的原有函數(shù)名。
 
函數(shù)中用到了char*型參數(shù),這里介紹下char*與Cstring的相互轉(zhuǎn)換的函數(shù):
 
(1)char*->CString
 
char* sz;
CString str;
str.Format("%s",sz);  //可以用此函數(shù)將讀取的值轉(zhuǎn)成字符串
 
(2) CString -> char*
 
CString str;
char* sz = str.GetBuffer(0);//可將字符串轉(zhuǎn)成char*給函數(shù)賦值
 
5)、當(dāng)不再需要使用DLL時記得關(guān)閉串口及釋放動態(tài)鏈接庫,(在OnDestroy事件中釋放)
 
 if(hinstDLL)
 {
   int k = mComTrue(mnport);
   if (k==1) 
   {
      mClose(mnport);
   }    
   FreeLibrary(hinstDLL);
 }