正文

dll一些情况2007-08-20 11:23:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/28670.html

分享到:

dll class.globals #include "stdafx.h"#include "lib.h" //#pragma data_seg("Shared")   HANDLE handle=NULL;   //必须在定义的同时进行初始化!!!!//#pragma data_seg()//#pragma   comment(linker, "/section:Shared,rws"   )   int add(int x){  //CreateFile switch(x) { case 0:  x=1;break; case 1:  x=2;break; case 2:  x=3;break; case 3:  x=4;break;  case 4:  x=5;break; default : ;   }  return x;} bool ComOpen(int x,CString c){  CString str; // AfxMessageBox(c);  switch(x) { case 0:  str="COM1";break; case 1:  str="COM2";break; case 2:  str="COM3";break; case 3:  str="COM4";break;  case 4:  str="COM5";break; default : ; }  HANDLE m_hCom; DWORD dwError=0;  m_hCom = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );  if(m_hCom==(HANDLE)-1) { AfxMessageBox("打开COM失败!"); return FALSE; } //设置com DCB dcb; GetCommState(m_hCom,&dcb);  CString s1; s1.Format("%d",dcb.StopBits);  AfxMessageBox(s1); dcb.BaudRate = 9600; //波特率为9600dcb.ByteSize = 8; //数据位数为8位dcb.Parity = NOPARITY; //偶校验dcb.StopBits = 2; //两个停止位 if (!SetCommState(m_hCom, &dcb)){AfxMessageBox("串口设置出错1!");} else{AfxMessageBox("成功了!");} dcb.fBinary = TRUE;dcb.fParity = TRUE; COMMTIMEOUTS TimeOuts; //设定读超时 TimeOuts.ReadIntervalTimeout=MAXDWORD; TimeOuts.ReadTotalTimeoutMultiplier=0; TimeOuts.ReadTotalTimeoutConstant=0;//在读一次输入缓冲区的内容后读操作就立即返回,//而不管是否读入了要求的字符。  //设定写超时TimeOuts.WriteTotalTimeoutMultiplier=100;TimeOuts.WriteTotalTimeoutConstant=500;SetCommTimeouts(m_hCom,&TimeOuts); //设置超时 SetupComm(m_hCom, 1024, 1024);PurgeComm(m_hCom,PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); if (!SetCommState(m_hCom, &dcb)){AfxMessageBox("串口设置出错2!");}  handle=m_hCom;   return true;} void out(){ HANDLE h=handle; CloseHandle(h); } CString GetComData(BYTE *x){  return "true";}   CString SendDataToCom(CString c){ CString s1,s2; BYTE   cByte[1024]   = {0};   BYTE   c2[1024]; DWORD   dwReadCount   =   128;    DWORD   dwWriteCount   =   0;     LPTSTR lp;  for(int   i   =0;   i   <  c.GetLength();   i++)    {   cByte[i]   =   c.GetAt(i);    } // while(dwReadCount==128)   //  {   WriteFile(handle,cByte,128,   &dwReadCount,   NULL); //  }    ReadFile(handle, c2,   128,   &dwReadCount,   NULL);  s2.Format("%d",dwReadCount); AfxMessageBox("dwReadCount="+s2);  CloseHandle(handle);  lp=(LPTSTR)c2; s1.Format("%s",c2);//转换 return lp; } ---------------------------------------------------- .h #ifndef LIB_H#define LIB_H extern "C" int __declspec(dllexport)add(int x);//extern "C" bool __declspec(dllexport)ComOpen(int x); extern "C" bool __declspec(dllexport)ComOpen(int x,CString c);  CString __declspec(dllexport)GetComData(BYTE * x); extern "C" CString __declspec(dllexport)SendDataToCom(CString x); extern "C" bool __declspec(dllexport)ComClose(int x); void out();   #endif           ------------------------------------------------------------------------------ .def ; comDll.def : Declares the module parameters for the DLL. LIBRARY      "comDll"DESCRIPTION  'comDll Windows Dynamic Link Library' EXPORTS  SendDataToCom ----------------------------------------------------------------------------------------------------- maindialog cpp typedef bool(*lpAddFun)(int x,CString c);  typedef CString(*lpSendFun)(CString c); HINSTANCE hDll;//DLL句柄 lpAddFun addFun;//函数指针lpSendFun   sendFun; -------------------------- oninitdialog hDll = LoadLibrary("..\\comDll\\Debug\\comDll.dll"); ---------------------------- createfile void CSCommTestDlg::OnButton_CreateFile() { CString str;  if (hDll != NULL) { addFun = (lpAddFun)GetProcAddress(hDll, "ComOpen");  if (addFun != NULL) {  CString se;  GetDlgItemText(IDC_EDIT3,se);  addFun(m_combo1.GetCurSel(),se) ;  }  else   AfxMessageBox("为NULL,请检查CPP文件"); // FreeLibrary(hDll); }  } ----------------------------------------- sendtocom void CSCommTestDlg::OnButton_sendtocom() { CString str;   if (hDll!= NULL) { sendFun = (lpSendFun)GetProcAddress(hDll, "SendDataToCom");  if (sendFun != NULL) { CString se;  GetDlgItemText(IDC_EDIT3,se);  str=sendFun(se) ;  }  else   AfxMessageBox("为NULL,请检查CPP文件"); // str.Format("%d",b);  m_edit2+=str; UpdateData(false);   }  }

阅读(7559) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册