VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - VB调用DLL的参数类型匹配问题
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:21119, 日期:2000-12-23
VB调用DLL的参数类型匹配问题(一)                                     
                                                                                
    几天前, 网友提到了VB中的参数与标准的C语言                                   
的参数的对应关系的问题,说老实话,我对次也是七                                  
窍只通了六窍。这几天查了些资料,总结如下,希望                                  
能对大家编程有所帮助。                                                          
                                                                                
    VB调用DLL的缺省调用方式是:传引用调用。即VB                                 
传一个指向参数变量的指针,DLL中便可以修改实际参                                 
数的值。若要传值方式,则需要在函数申明时在参数前                                
加上ByVal关键字。                                                               
                                                                                
    下面谈谈字符串调用的约定。 事实上对字符串无                                 
论是否使用ByVal关键字,在DLL中均可以修改传入的参                                
数。但,用不用ByVal关键字,还是有区别的。因为VB                                 
与DLL接口支持两种类型的字符串,VB是用如下的规则                                 
来匹配类型的,若没用关键字ByVal,则传一个VB字符                                 
串句柄(数据类型为HLSTR),这种类型或字符串句柄                                 
只有专为VB设计的DLL才能识别。所以,通常的DLL如                                  
果不是专门为VB设计的,就只有用ByVal关键字了。因                                 
为加了ByVal关键字后,VB会在字符串尾加上Null字符,                               
即自动将VB字符串转换成C-style字符串,然后将指向                                 
该串的指针传给DLL。
VB调用DLL关于参数的匹(二)                                            
                                                                                
    这次谈谈VB与C语言的其他参数类型间的转换。                 
包括布尔型、16位整数、16位无符号整数、8                                    
位有和无符号整数、32位有和无符号整数、VB                                    
中Variant类型及结构指针等。                                              
                                                                                
  〈布尔量〉VB和C中都定义0为“假”,非0                                  
为“真”,但C语言逻辑表达式的结果用1表示“真”,                              
而相应的VB返回-1做为“真”。这是二者的区别。                                
                                                                                
  〈16位符号整数〉C语言中的int和short                              
类型与VB中的integer完全对应匹配,所以可以                              
大胆使用。:)                                                                    
                                                                                
    〈16位无符号整数〉VB中没有相应的类型,所以                              
就存在转换的问题了。关键是对于从32768-65535这一区间                             
的数如何处理。事实上可以用一个范围更大的类型(如long)                          
数做“中间代理”,而达到目的。举例如下:                                        
 SignedEquivalent% = CInt( UnsighedNumber& - &h10000&)                         
 其中UnsighedNumber&就是我们要的“代理”。这里的诀                             
窍是:DLL希望一个无符号的16位整数,即使VB把                              
SighedEquivalent解释为负数,DLL也会将它解释为我们所                          
需要的在32768-65535间的正整数。当然,反过来的逆变换,                            
如下: LongResult& = (CLng( IntegerResult%) and &h0ffff&)
VB中调用DLL关于参数匹配的问题(三)                                 
                                                                                
     * 8位有符号和无符号整数                                                    
      只要VB的无符号整数在正确的范围内,即-128~+127                            
  或0~255之间,就没有任何问题,直接用Integer类型就可                           
  以了。                                                                        
                                                                                
     * 32位有符号整数                                                           
      与VB中的Long 类型完全兼容。真是好运气。呵呵。                             
                                                                                
     * 32位无符号整数                                                           
      有要转换了,因为VB中无相应的类型。这种转换的需要                          
  主要发生在使用的变量值在 2,147,483,647 ~ 4,294,967,295                       
  范围内。这里可以采用与前面提到的16位无符号整数类型转                          
  换相同的技巧,不过要用 Currency 或 Double 数据类型做                          
  中间的“代理”。方法相同,这里就不再罗嗦了。                                  
                                                                                
    * VB中的 Variant 类型                                                       
     这种类型似乎只有VB中有,是不是有点太排他的感觉?                           
  所以碰到有这种类型的DLL函数,放心用吧,那DLL肯定是                            
  专门为VB而设计的。                                                            
                                                                                
    * 结构体指针                                                                
     在很多场合,要构造出一个与DLL(C-Style)完全吻合的                         
  结构体是有可能的,但,也存在走不通的情况。下面列出了                          
  一些在结构体中常常用到的类型,希望对你有所帮助。                              
      C-Style                VB-Style                            
        BYTE                         长度为1的字符串:string*1                  
        16位整数                     Integer                                    
        32位整数                     Long                                       
        位域                         把位域里每16位合成一个整数                 
        字符数组(定长)             定长字符串                                 
        FARPROC(函数指针)          Long, 事实上VB不直接支持函数指针:)         
        句柄                         Integer                                    
        LPSTR(C字符串)             Long                                       
        数值数组                     相应的数值数组                             
        结构指针                     Long                                       
        Single或Double               Single或Double                             
                                                                                
    * VB中的 Currency 类型                                                      
      事实是 C 语言中也没有这种类型, 当可以构造出与之相                        
  当的类型,如下结构可以实现:                                                  
       typedef struct currencystruct{                                           
                 char currency_buf[8];                                          
                  } Currency;                                                   
                                                                                
    * vb中的 Any 类型的应用                                                     
      这种类型申明的参数不进行任何类型的匹配检查,传递                          
   的参数类型总是函数调用时传进的参数的类型。当然这就要                         
   求调用程序保证传入正确的类型。实际上,这种类型是很有                         
   用的。例如,Windows API中很多的函数就支持多种类型的参                        
   数的调用,一个典型的例子如下:                                               
      HCURSOR  LoadCursor(hInstance, lpCursorName)                              
   其中的第二个参数 lpCursorName 既可以是字符串指针,也                         
   可以是一个整数(即是光标在资源文件中的ID)。对于这种                         
   可容许多类型的函数参数,可将相应参数申明为 Any。                             
                                                                                
       全文到此结束了,其中也许有很多细节没考虑周到,有                         
   问题欢迎提出。                                                               
       祝 大家   编程快乐!                                                     
                                                                                
        < Copy right by Luster Witty, 1998.11.27 >                              
    < Any suggestion please mail to : luster@188.net>                           
                                                                                
                                                                                
                                                                                
--                                                                              
☆ 为了我爱的人,为了爱我的人 ★                                               
                                                                                
※ 来源: 中国科大BBS站 [bbs.ustc.edu.cn]