vb.net编程中调用Window AP函数

本文介绍如何在VB.NET中通过两种方式声明Windows API函数,实现读取文件图标的功能。文章详细解释了平台调用、托管DLL和非托管DLL的概念,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Windows API就是Windows系统的API函数简称(Application Program Interface,即:应用程序接口函数),它是Windows操作系统提供给各种开发基于Windows平台应用 软件的开发语言的一些公用的函数,这些函数一般都比较底层,所以当各种开发语言使用自带的函数或类库已无法解决问题时,调用Windows API函数往往是一种非常直接、有效的解决方法。但由于在程序中调用Windows API函数一般都很复杂,所以对于程序员来说,是否能够灵活的使用Windows API函数,往往也是其水平高低的一个重要标志。但自从微软推出.Net框架以后,各种开发基于.Net平台下的程序语言,如Visual Basic .Net、Visual C#等却很少使用Windows API函数,并且微软公司也不像以前那样,提倡在这些.Net开发语言中使用
  
  Windows API函数,其主要的原因如下:

  1. Net框架所附带的类库.Net FrameWork SDK是一个内容丰富、功能强大的软件开发包,各种Windows API函数所实现的各种功能几乎都可以在这个软件开发包中找到与之对应的部分。
  
  2.微软Visual Basic .Net、Visual C#等目的是编写跨平台的应用程序,如果在Visual Basic .Net、Visual C#等中使用Windows API函数,这也就注定了编写出的程序只能运行于Windows平台之下,而弱化了.NET的跨平台能力。这也就是为什么微软不提倡在.Net平台调用Windows API函数的原因。
  
  虽然微软并不提倡在Visual Basic .Net、Visual C#中调用Windows API函数,但由于目前.Net 框架推出时间较短,.Net FrameWork SDK中提供的类库还并不足以完全替换Windows API函数的所有功能,所以在某些时候,.Net开发语言还是必须要调用Windows API函数。
  
  一.平台调用、托管DLL、非托管DLL简介:
  托管DLL就是能够在公共语言运行库(Common Language Runtime,简称CLR)中能够直接引用的,并且扩展名为“DLL”的文件。具体所指就是封装各种命名空间所在的DLL文件,如System.dll等。非托管DLL就是平常所的动态链接库等,其中就包括了封装所有Windows API函数的DLL文件。各种非托管DLL中的函数在公共语言运行库中不能直接被调用,而需要经过.Net框架提供的“平台调用”服务后才可以。
  
  “平台调用”是.Net框架为Visual Basic .Net、Visual C#等.Net开发语言提供的一种服务,用以在托管代码中引入各种非托管DLL中封装的函数(其中包括Windows API函数)。“平台调用”依赖于元数据在运行时查找导出函数并封装其参数。图01公共语言运行库利用“平台服务”调用非托管DLL中的函数的流程图:
  

图01:“平台服务”的调用非托管函数的流程图


  在托管代码中使用“平台调用”服务调用非托管DLL中封装的函数时,“平台服务”将依次执行以下操作:
  
  1.查找包含该函数所在的DLL文件。
  
  2.如果找到,则将该DLL文件 加载到内存中。
  
  3.查找函数在内存中的地址并将其参数推到堆栈上,并封送所需的数据。
  
  4.将控制权转移给非托管函数。 这样整个函数调用完成。
  
  在Visual Basic .Net中使用“平台调用”服务,申明Windows API函数主要有二种具体的实现方法:
  
  1.使用DllImport特征类来申明Windows API函数。
  
  2.使用“Declare”语句来申明Windows API函数。
  
  这二种方法虽有异曲同工之效,但在繁简上却有很大差异,第一种方法申明过程比较复杂,很容易在申明Windows API函数时出错,所以并不提倡。而第二种方法相对简单,并且又保存了以前Visual Basic中的很多语法,所以在平常时大都使用这种方法来申明Windows API函数。
  
  二. vb.net查看文件中图标的函数及申明Windows API的方法:
  Visual Basic .Net要实现查看文件中的图标,目前只使用.Net FrameWork SDK是无法实现这种功能的,正如前面所说,主要是由于.Net FrameWork SDK推出的时间较短,其功能还不可能面面俱到。解决问题的关键是正确使用Windows API函数,其中所涉及到的Windows API函数主要有二个:其一是获得指定文件中的图标数目;其二是从指定文件的指定位置导出图标的Windows句柄。这二个函数都位于“Shell32.dll”文件中,并且函数的入口点都为“ExtractIcon”。下面是在Visual Basic .Net中分别使用DllImport特征类和“Declare”语句申明这二个Windows API函数的具体方法。
  
  (1).使用DllImport特征类来申明Windows API函数:
  
  下面是在Visual Basic .Net中使用DllImport特征类申明二个Windows API函数的具体示例:
  
  '函数ExtractIcon,其功能是是从指定文件的指定位置导出图标的Windows句柄。
< System.Runtime.InteropServices.DllImport (  " Shell32.dll "  , EntryPoint : =   " ExtractIcon "  ) > _
Public   Function _
ExtractIcon ( 
ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
End Function

  '函数Icon_Num,其功能是获得指定文件中的图标数目
< System.Runtime.InteropServices.DllImport (  " Shell32.dll "  , EntryPoint : =   " ExtractIcon "  ) > _
Public   Function _
Icon_Num ( 
ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As Integer ) As Integer
End Function

  
  在使用DllImport特征类申明Windows API函数时,如果申明的函数名称和函数的入口点相同,则可以在申明Windows API函数时,省略定义函数入口点对应的代码,即EntryPoint对象字段对应的代码,这样声明ExtractIcon函数的代码也可以简化为如下所示:
  
< System.Runtime.InteropServices.DllImport (  " Shell32.dll "  ) > _
Public   Function _
ExtractIcon ( 
ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
End Function

  
  (2).使用“Declare”语句来申明Windows API函数:
  
  使用“Declare”语句的确比使用DllImport特征类要简单了许多,下面是在Visual Basic .Net中使用“Declare”语句来声明上述二个Windows API函数的具体方法:
  
  
Declare   Auto   Function ExtractIcon Lib "Shell32.dll" Alias "ExtractIcon" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
'声明ExtractIcon函数

  
Declare   Auto   Function Icon_Num Lib "Shell32.dll" Alias "ExtractIcon" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As Integer ) As Integer
'声明Icon_Num函数

  
  在Visual Basic .Net中声明Windows API函数时,“Declare”语句中Alias关键字的作用相当于使用DllImport特征类中的EntryPoint对象字段。同样在使用“Declare”语句声明Windows API函数时,如果声明的函数和函数的入口点相同,也可以省略Alias关键字对应的代码,所以ExtractIcon函数也可以简化为如下:
  
  
Declare   Auto   Function ExtractIcon Lib "Shell32.dll" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
  
  下面就结合一个示例的编写过程来掌握的这二个Windows API函数的具体使用方法,这个示例的作用就是读取指定文件中的图标数目,并显示文件中的图标。
  
  三.本文中程序的编写、调试和运行环境:
  (1).视窗2000高级 服务器版。
  
  (2).Visual Studio .Net 2003企业结构设计版,.Net FrameWork SDK版本号4322。
  
  四.Visual Basic .Net读取文件中的图标的实现步骤:
  下面介绍的示例,其功能读取指定文件中包含的图标数目,并把这些图标全部显示出来。下面是这个示例的实现步骤:
  
  1.启动Visual Studio .Net。
  
  2.选择菜单【文件】|【新建】|【项目】后,弹出【新建项目】对话框。
  
  3.将【项目类型】设置为【Visual Basic项目】。
  
  4.将【模板】设置为【Windows应用程序】。
  
  5.在【名称】文本框中输入【Visual Basic .Net查看文件中的图标】。
  
  6. 在【位置】的文本框中输入【E:/VS.NET项目】,然后单击【确定】按钮,这样在【E:/VS.NET项目】目录中就产生了名称为【Visual Basic .Net查看文件中的图标】文件夹,里面存放着【Visual Basic .Net查看文件中的图标】项目的所有文件。具体如图02所示:
 
   

图02:【Visual Basic .Net查看文件中的图标】项目的【新建项目】对话框


  7. 选择菜单【项目】|【添加新项】,在弹出的对话框中的【模板】设置为【模块】,【名称】文本框设置为【Module1.vb】后。单击【打开】按钮,则在项目中增加了一个模板文件,名称为【Module1.vb】。
  
  8. 把Visual Stuido .Net的当前窗口切换到Module1.vb的代码编辑窗口,并在其Module1的代码区中添加下列代码,下列代码是用二种方式声明二个Windows API函数:
  
< System.Runtime.InteropServices.DllImport (  " Shell32.dll "  ) > _
Public   Function _
ExtractIcon ( 
ByVal src As System.IntPtr , ByVal strFileName As String , ByVal uiIconIndex As UInt32  

 
Starting C simulation ... D:/vitis202302/Vitis_HLS/2023.2/bin/vitis_hls.bat G:/vitis2023_project/SmoothProfileOnXAxisMean_NoGray_liangjing/SmoothProfileOnXAxisMean_NoGray/SmoothProfileOnXAxisMean_NoGray/csim.tcl INFO: [HLS 200-10] Running 'D:/vitis202302/Vitis_HLS/2023.2/bin/unwrapped/win64.o/vitis_hls.exe' INFO: [HLS 200-10] For user 'HP' on host 'desktop-qjs3vb7' (Windows NT_amd64 version 6.2) on Tue Jul 22 17:03:51 +0800 2025 INFO: [HLS 200-10] In directory 'G:/vitis2023_project/SmoothProfileOnXAxisMean_NoGray_liangjing' INFO: [HLS 200-2053] The vitis_hls executable is being deprecated. Consider using vitis-run --mode hls --tcl Sourcing Tcl script 'G:/vitis2023_project/SmoothProfileOnXAxisMean_NoGray_liangjing/SmoothProfileOnXAxisMean_NoGray/SmoothProfileOnXAxisMean_NoGray/csim.tcl' INFO: [HLS 200-1510] Running: source G:/vitis2023_project/SmoothProfileOnXAxisMean_NoGray_liangjing/SmoothProfileOnXAxisMean_NoGray/SmoothProfileOnXAxisMean_NoGray/csim.tcl INFO: [HLS 200-1510] Running: open_project SmoothProfileOnXAxisMean_NoGray INFO: [HLS 200-10] Opening project 'G:/vitis2023_project/SmoothProfileOnXAxisMean_NoGray_liangjing/SmoothProfileOnXAxisMean_NoGray'. INFO: [HLS 200-1510] Running: set_top SmoothProfileOnXAxisMean INFO: [HLS 200-1510] Running: add_files src/SmoothProfileOnXAxisMean.cpp INFO: [HLS 200-10] Adding design file 'src/SmoothProfileOnXAxisMean.cpp' to the project INFO: [HLS 200-1510] Running: add_files -tb src/SmoothProfileOnXAxisMean_test.cpp INFO: [HLS 200-10] Adding test bench file 'src/SmoothProfileOnXAxisMean_test.cpp' to the project INFO: [HLS 200-1510] Running: open_solution SmoothProfileOnXAxisMean_NoGray -flow_target vivado INFO: [HLS 200-10] Opening solution 'G:/vitis2023_project/SmoothProfileOnXAxisMean_NoGray_liangjing/SmoothProfileOnXAxisMean_NoGray/SmoothProfileOnXAxisMean_NoGray'. INFO: [SYN 201-201] Setting up clock 'default' with a period of 5ns. INFO: [HLS 200-1611] Setting target device to 'xcku060-ffva1156-2-e' INFO: [HLS 200-1505] Using flow_target 'vivado' Resolution: For help on HLS 200-1505 see docs.xilinx.com/access/sources/dita/topic?Doc_Version=2023.2%20English&url=ug1448-hls-guidance&resourceid=200-1505.html INFO: [HLS 200-1510] Running: set_part xcku060-ffva1156-2-e INFO: [HLS 200-1510] Running: create_clock -period 5 -name default INFO: [HLS 200-1510] Running: csim_design -quiet INFO: [SIM 211-2] *************** CSIM start *************** INFO: [SIM 211-4] CSIM will launch GCC as the compiler. INFO: [HLS 200-2036] Building debug C Simulation binaries Compiling ../../../../src/SmoothProfileOnXAxisMean.cpp in debug mode csim.mk:85: recipe for target 'obj/SmoothProfileOnXAxisMean.o' failed In file included from D:/vitis202302/Vitis_HLS/2023.2/include/floating_point_v7_1_bitacc_cmodel.h:150:0, from D:/vitis202302/Vitis_HLS/2023.2/include/hls_fpo.h:140, from D:/vitis202302/Vitis_HLS/2023.2/include/etc/hls_half_fpo.h:19, from D:/vitis202302/Vitis_HLS/2023.2/include/hls_half.h:26, from D:/vitis202302/Vitis_HLS/2023.2/include/etc/ap_private.h:52, from D:/vitis202302/Vitis_HLS/2023.2/include/etc/ap_common.h:666, from D:/vitis202302/Vitis_HLS/2023.2/include/ap_int.h:10, from ../../../../src/SmoothProfileOnXAxisMean.h:3, from ../../../../src/SmoothProfileOnXAxisMean.cpp:1: D:/vitis202302/Vitis_HLS/2023.2/include/gmp.h:58:0: warning: "__GMP_LIBGMP_DLL" redefined #define __GMP_LIBGMP_DLL 0 In file included from D:/vitis202302/Vitis_HLS/2023.2/include/hls_fpo.h:140:0, from D:/vitis202302/Vitis_HLS/2023.2/include/etc/hls_half_fpo.h:19, from D:/vitis202302/Vitis_HLS/2023.2/include/hls_half.h:26, from D:/vitis202302/Vitis_HLS/2023.2/include/etc/ap_private.h:52, from D:/vitis202302/Vitis_HLS/2023.2/include/etc/ap_common.h:666, from D:/vitis202302/Vitis_HLS/2023.2/include/ap_int.h:10, from ../../../../src/SmoothProfileOnXAxisMean.h:3, from ../../../../src/SmoothProfileOnXAxisMean.cpp:1: D:/vitis202302/Vitis_HLS/2023.2/include/floating_point_v7_1_bitacc_cmodel.h:142:0: note: this is the location of the previous definition #define __GMP_LIBGMP_DLL 1 ../../../../src/SmoothProfileOnXAxisMean.cpp: In function 'void SmoothProfileOnXAxisMean(hls::stream<float>&, hls::stream<float>&, ap_uint<6>, float)': ../../../../src/SmoothProfileOnXAxisMean.cpp:246:37: error: operands to ?: have different types 'int' and 'ap_int_base<6, false>::RType<32, true>::plus {aka ap_int<33>}' read_ptr = (read_ptr == 32) ? 0 : read_ptr + 1; ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~ ../../../../src/SmoothProfileOnXAxisMean.cpp:246:37: note: and each type can be converted to the other ../../../../src/SmoothProfileOnXAxisMean.cpp:249:35: error: operands to ?: have different types 'int' and 'ap_int_base<6, false>::RType<32, true>::plus {aka ap_int<33>}' write_ptr = (write_ptr == 32) ? 0 : write_ptr + 1; ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~ ../../../../src/SmoothProfileOnXAxisMean.cpp:249:35: note: and each type can be converted to the other make: *** [obj/SmoothProfileOnXAxisMean.o] Error 1 ERROR: [SIM 211-100] 'csim_design' failed: compilation error(s). INFO: [SIM 211-3] *************** CSIM finish *************** INFO: [HLS 200-111] Finished Command csim_design CPU user time: 0 seconds. CPU system time: 0 seconds. Elapsed time: 1.236 seconds; current allocated memory: 0.398 MB. 4 while executing "source G:/vitis2023_project/SmoothProfileOnXAxisMean_NoGray_liangjing/SmoothProfileOnXAxisMean_NoGray/SmoothProfileOnXAxisMean_NoGray/csim.tcl" invoked from within "hls::main G:/vitis2023_project/SmoothProfileOnXAxisMean_NoGray_liangjing/SmoothProfileOnXAxisMean_NoGray/SmoothProfileOnXAxisMean_NoGray/csim.tcl" ("uplevel" body line 1) invoked from within "uplevel 1 hls::main {*}$newargs" (procedure "hls_proc" line 16) invoked from within "hls_proc [info nameofexecutable] $argv" INFO: [HLS 200-112] Total CPU user time: 1 seconds. Total CPU system time: 1 seconds. Total elapsed time: 2.984 seconds; peak allocated memory: 199.363 MB. Finished C simulation.
最新发布
07-23
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值