转来的,说是好用。DELPHI的。 ---------------------- function IsFileInUse(fName : string) : boolean; var HFileRes : HFILE; begin Result := false; if not FileExists(fName) then exit; HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE, 0 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); Result := (HFileRes = INVALID_HANDLE_VALUE); if not Result then CloseHandle(HFileRes); end;
procedure TForm1.FormCreate(Sender: TObject); var bool:boolean; function IsFileInUse(fName : string) : boolean; var HFileRes : HFILE; begin Result := false; if not FileExists(fName) then exit; HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE, 0 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); Result := (HFileRes = INVALID_HANDLE_VALUE); if not Result then CloseHandle(HFileRes); end; begin bool:=isfileinuse('C:\windows\system32\mmc.exe'); if bool then showmessage('in useing') else showmessage('not in useing'); end;
Option Explicit Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As LongPrivate Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Const TH32CS_SNAPPROCESS = &H2 Private Const TH32CS_SNAPheaplist = &H1 Private Const TH32CS_SNAPthread = &H4 Private Const TH32CS_SNAPmodule = &H8 Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule Private Const MAX_PATH As Integer = 260 Private Const PROCESS_TERMINATE = &H1Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long th32ParentProcessID As Long pcPriClassBase As Long dwFlags As Long szExeFile As String * MAX_PATH End TypePrivate Sub Command1_Click() Dim i As Long Dim proc As PROCESSENTRY32 Dim snap As Long Dim exename As String Dim theloop As Long Dim ret As ListItem ListView1.ListItems.Clear '清空所有内容 snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄 proc.dwSize = Len(proc) theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值 i = 0 While theloop <> 0 '当返回值非零时继续获取下一个进程 exename = proc.szExeFile Set ret = ListView1.ListItems.Add(, "first" & CStr(i), exename) '将进程名添加到第一项中 ListView1.ListItems("first" & CStr(i)).SubItems(1) = proc.th32ProcessID '将进程ID添加到第二项中 i = i + 1 theloop = ProcessNext(snap, proc) Wend CloseHandle snap '关闭进程“快照”句柄 End Sub'终止进程 Private Sub Command2_Click() Dim i As Long Dim hand As Long hand = OpenProcess(PROCESS_TERMINATE, True, CLng(ListView1.SelectedItem.SubItems(1))) '获取进程句柄 TerminateProcess hand, 0 '关闭进程 Call Command1_Click '调用查看进程来刷新进程列表 End SubPrivate Sub Form_Load() Dim header As ColumnHeader ListView1.View = lvwReport ListView1.ColumnHeaders.Clear Set header = ListView1.ColumnHeaders.Add(, "first", "进程", 4000) '设置ListView中项目的宽度,读者也可自行设置 Set header = ListView1.ColumnHeaders.Add(, "second", "ID", 1400) ListView1.Refresh End Sub 点击command1获得进程列表,点击command2关闭选择的进程。
转成VB版的是: ------------------ '一定能用,我试过! Option ExplicitPrivate Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As String, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Const GENERIC_READ = &H80000000 Private Const GENERIC_WRITE = &H40000000 Private Const OPEN_EXISTING = 3 Private Const FILE_ATTRIBUTE_NORMAL = &H80 Private Const INVALID_HANDLE_VALUE = -1Private Sub Command1_Click() MsgBox IsFileRun(Text1.Text) End SubPrivate Function IsFileRun(ByVal pFile As String) As Boolean Dim Ret As Long Ret = CreateFile(pFile, GENERIC_READ Or GENERIC_WRITE, 0&, vbNullString, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&) IsFileRun = (Ret = INVALID_HANDLE_VALUE) CloseHandle Ret End Function
别人的程序不知道能不能用上 枚举所有窗口,如果找不到可以用shell函数运行那个程序Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Declare Function EnumWindows Lib "user32" (ByVal wndenmprc As Long, ByVal lParam As Long) As Long Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Public Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long Dim buf As String * 256 Dim title As String Dim length As Long Dim mbuf As String * 256 Dim istr As Long Dim i As Long ' Get the window's title. length = GetWindowText(app_hWnd, buf, Len(buf)) title = Left$(buf, length) ' See if this is the target window. If InStr(title, Target) <> 0 Then ' Find Window
' Continue searching. EnumCallback = 1 End Function' Ask Windows for the list of tasks. Public Sub TerminateTask(app_name As String) Target = app_name EnumWindows AddressOf EnumCallback, 0 End Sub
----------------------
function IsFileInUse(fName : string) : boolean;
var
HFileRes : HFILE;
begin
Result := false;
if not FileExists(fName) then
exit;
HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,
0 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;
结合SDK中对CreateFile函数的定义,该函数的似乎是可以实现判断文件在被使用的功能的,然而却没有。有没有其他方法
---------------------------------------------------------------
以可写方式打开文件,如果抱错则正在使用
---------------------------------------------------------------
这个函数完全可以判断出程序是否正被使用,测了一下
procedure TForm1.FormCreate(Sender: TObject);
var
bool:boolean;
function IsFileInUse(fName : string) : boolean;
var
HFileRes : HFILE;
begin
Result := false;
if not FileExists(fName) then
exit;
HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,
0 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;
begin
bool:=isfileinuse('C:\windows\system32\mmc.exe');
if bool then
showmessage('in useing')
else
showmessage('not in useing');
end;
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As LongPrivate Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPmodule = &H8
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
Private Const MAX_PATH As Integer = 260
Private Const PROCESS_TERMINATE = &H1Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End TypePrivate Sub Command1_Click()
Dim i As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename As String
Dim theloop As Long
Dim ret As ListItem
ListView1.ListItems.Clear '清空所有内容
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
i = 0
While theloop <> 0 '当返回值非零时继续获取下一个进程
exename = proc.szExeFile
Set ret = ListView1.ListItems.Add(, "first" & CStr(i), exename) '将进程名添加到第一项中
ListView1.ListItems("first" & CStr(i)).SubItems(1) = proc.th32ProcessID '将进程ID添加到第二项中
i = i + 1
theloop = ProcessNext(snap, proc)
Wend
CloseHandle snap '关闭进程“快照”句柄
End Sub'终止进程
Private Sub Command2_Click()
Dim i As Long
Dim hand As Long
hand = OpenProcess(PROCESS_TERMINATE, True, CLng(ListView1.SelectedItem.SubItems(1))) '获取进程句柄
TerminateProcess hand, 0 '关闭进程
Call Command1_Click '调用查看进程来刷新进程列表
End SubPrivate Sub Form_Load()
Dim header As ColumnHeader
ListView1.View = lvwReport
ListView1.ColumnHeaders.Clear
Set header = ListView1.ColumnHeaders.Add(, "first", "进程", 4000) '设置ListView中项目的宽度,读者也可自行设置
Set header = ListView1.ColumnHeaders.Add(, "second", "ID", 1400)
ListView1.Refresh
End Sub
点击command1获得进程列表,点击command2关闭选择的进程。
------------------
'一定能用,我试过!
Option ExplicitPrivate Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As String, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const INVALID_HANDLE_VALUE = -1Private Sub Command1_Click()
MsgBox IsFileRun(Text1.Text)
End SubPrivate Function IsFileRun(ByVal pFile As String) As Boolean
Dim Ret As Long
Ret = CreateFile(pFile, GENERIC_READ Or GENERIC_WRITE, 0&, vbNullString, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
IsFileRun = (Ret = INVALID_HANDLE_VALUE)
CloseHandle Ret
End Function
枚举所有窗口,如果找不到可以用shell函数运行那个程序Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal wndenmprc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long
Dim mbuf As String * 256
Dim istr As Long
Dim i As Long
' Get the window's title.
length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left$(buf, length) ' See if this is the target window.
If InStr(title, Target) <> 0 Then
' Find Window
' Continue searching.
EnumCallback = 1
End Function' Ask Windows for the list of tasks.
Public Sub TerminateTask(app_name As String)
Target = app_name
EnumWindows AddressOf EnumCallback, 0
End Sub