如何调用这个DLL,最好有示例!

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

原dll是这样写的:  
  using   system;  
  using   system.componentmodel;  
  using   system.collections;  
  using   system.diagnostics;  
  using   system.runtime.interopservices;  
  namespace   commlib  
  {  
  ///   <summary>  
  ///   serialport   的摘要说明。  
  ///   </summary>  
  public   class   serialport   :   system.componentmodel.component  
  {  
  ///   <summary>  
  ///   必需的设计器变量。  
  ///   </summary>  
  private   system.componentmodel.container   components   =   null;  
  public   serialport(system.componentmodel.icontainer   container)  
  {  
  ///   <summary>  
  ///   windows.forms   类撰写设计器支持所必需的  
  ///   </summary>  
  container.add(this);  
  initializecomponent();  
   
  //  
  //   todo:   在   initializecomponent   调用后添加任何构造函数代码  
  //  
  }  
   
  public   serialport()  
  {  
  ///   <summary>  
  ///   windows.forms   类撰写设计器支持所必需的  
  ///   </summary>  
  initializecomponent();  
   
  //  
  //   todo:   在   initializecomponent   调用后添加任何构造函数代码  
  //  
  portnum   =   "com1:";       //   1                                                       端口号  
  baudrate   =   9600;           //   9600                                                       波特率        
  bytesize   =   8;                 //   8位                                                         数据位  
  parity   =   0;                     //   0-4=no,odd,even,mark,space           校验位  
  stopbits   =   1;                 //   0,1,2   =   1,   1.5,   2                             停止位  
  }  
   
  #region   申明api  
  //win32   api   constants    
  private   const   uint   generic_read   =   0x80000000;    
  private   const   uint   generic_write   =   0x40000000;    
  private   const   int   open_existing   =   3;                
  private   const   int   invalid_handle_value   =   -1;    
                 
  [structlayout(layoutkind.sequential)]    
  private   struct   dcb      
  {    
  //taken   from   c   struct   in   platform   sdk      
  public   int   dcblength;                       //   sizeof(dcb)      
  public   int   baudrate;                         //   current   baud   rate      
  public   int   fbinary;                     //   binary   mode,   no   eof   check      
  public   int   fparity;                     //   enable   parity   checking      
  public   int   foutxctsflow;             //   cts   output   flow   control      
  public   int   foutxdsrflow;             //   dsr   output   flow   control      
  public   int   fdtrcontrol;               //   dtr   flow   control   type      
  public   int   fdsrsensitivity;       //   dsr   sensitivity      
  public   int   ftxcontinueonxoff;   //   xoff   continues   tx      
  public   int   foutx;                     //   xon/xoff   out   flow   control      
  public   int   finx;                       //   xon/xoff   in   flow   control      
  public   int   ferrorchar;           //   enable   error   replacement      
  public   int   fnull;                     //   enable   null   stripping      
  public   int   frtscontrol;           //   rts   flow   control      
  public   int   fabortonerror;       //   abort   on   error      
  public   int   fdummy2;                 //   reserved      
  public   ushort   wreserved;                     //   not   currently   used      
  public   ushort   xonlim;                           //   transmit   xon   threshold      
  public   ushort   xofflim;                         //   transmit   xoff   threshold      
  public   byte   bytesize;                       //   number   of   bits/byte,   4-8      
  public   byte   parity;                           //   0-4=no,odd,even,mark,space      
  public   byte   stopbits;                       //   0,1,2   =   1,   1.5,   2      
  public   char   xonchar;                         //   tx   and   rx   xon   character      
  public   char   xoffchar;                       //   tx   and   rx   xoff   character      
  public   char   errorchar;                     //   error   replacement   character      
  public   char   eofchar;                         //   end   of   input   character      
  public   char   evtchar;                         //   received   event   character      
  public   ushort   wreserved1;                   //   reserved;   do   not   use      
  }    
   
  [structlayout(layoutkind.sequential)]    
  private   struct   commtimeouts      
  {        
  public   int   readintervaltimeout;      
  public   int   readtotaltimeoutmultiplier;      
  public   int   readtotaltimeoutconstant;      
  public   int   writetotaltimeoutmultiplier;      
  public   int   writetotaltimeoutconstant;      
  }            
   
  [structlayout(layoutkind.sequential)]          
  private   struct   overlapped      
  {      
  public   int     internal;      
  public   int     internalhigh;      
  public   int     offset;      
  public   int     offsethigh;      
  public   int   hevent;      
  }        
                 
  [dllimport("kernel32")]    
  private   static   extern   int   createfile(    
  string   lpfilename,                                                   //   file   name    
  uint   dwdesiredaccess,                                             //   access   mode    
  int   dwsharemode,                                                     //   share   mode    
  int   lpsecurityattributes,   //   sd    
  int   dwcreationdisposition,                                 //   how   to   create    
  int   dwflagsandattributes,                                   //   file   attributes    
  int   htemplatefile                                                 //   handle   to   template   file    
  );    
  [dllimport("kernel32")]    
  private   static   extern   bool   getcommstate(    
  int   hfile,     //   handle   to   communications   device    
  ref   dcb   lpdcb         //   device-control   block    
  );          
  [dllimport("kernel32")]    
  private   static   extern   bool   buildcommdcb(    
  string   lpdef,     //   device-control   string    
  ref   dcb   lpdcb           //   device-control   block    
  );    
  [dllimport("kernel32")]    
  private   static   extern   bool   setcommstate(    
  int   hfile,     //   handle   to   communications   device    
  ref   dcb   lpdcb         //   device-control   block    
  );    
  [dllimport("kernel32")]    
  private   static   extern   bool   getcommtimeouts(    
  int   hfile,                                     //   handle   to   comm   device    
  ref   commtimeouts   lpcommtimeouts     //   time-out   values    
  );          
   
 

· 网友精彩回答:

发表者:sunangle

我知道点,你参考下,你把你的文件写成.cs   用csc编译放到你用的解决方氨下的bin目录中,然后在你用到的程序中继承dll中的类

发表者:sywcf

在别的命名空间里先“添加引用”,引用这个dll,然后再using   commlib就能用里面的方法了

发表者:zhongwanli

yun   ,   代码太多??  
   
  如果仅仅是调用不到,请引用!  
   
 

.
处理 SSI 文件时出错
© 2006-2008 All Rights Reserved