Option Explicit Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Private Const SW_NORMAL = 1Private Sub Form_Load() ShellExecute Me.hwnd, vbNullString, "C:\WINDOWS\system32\winmine.exe", vbNullString, vbNullString, SW_NORMAL End Sub
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
说明
查找与指定文件关联在一起的程序的文件名
返回值
Long,非零表示成功,零表示失败。会设置GetLastError
参数表
参数 类型及说明
hwnd Long,指定一个窗口的句柄,有时候,windows程序有必要在创建自己的主窗口前显示一个消息框
lpOperation String,指定字串“open”来打开lpFlie文档,或指定“Print”来打印它
lpFile String,想用关联程序打印或打开一个程序名或文件名
lpParameters String,如lpszFlie是可执行文件,则这个字串包含传递给执行程序的参数
lpDirectory String,想使用的完整路径
nShowCmd Long,定义了如何显示启动程序的常数值。参考ShowWindow函数的nCmdShow参数
把下面的内容存为 AutoRun.inf 文件,放到光盘目录下,open 后面是要自动运行的程序 icon 是光盘的图片
[Autorun]
open= Programer.exe
icon= PUBCD.ICO
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_NORMAL = 1Private Sub Form_Load()
ShellExecute Me.hwnd, vbNullString, "C:\WINDOWS\system32\winmine.exe", vbNullString, vbNullString, SW_NORMAL
End Sub
在光盘中放一个autorun.inf文件
[autorun]
open= 程序名
icon=光盘显示图标2.
word文档
shell "winword " & 文件名.doc
excel文档
shell "excel " & 文件名.xlt
应用程序
shell 应用程序名.exe