www.pudn.com > ProgramBox.rar > Module1.bas, change:2013-08-24,size:4508b


Attribute VB_Name = "Module1" 
 '***********************************保存文件********************************************** 
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long 
Private Type OPENFILENAME 
 lStructSize As Long 
 hwndOwner As Long 
 hInstance As Long 
 lpstrFilter As String 
 lpstrCustomFilter As Stringn 
 MaxCustFilter As Longn 
 FilterIndex As Long 
 lpstrFile As Stringn 
 MaxFile As Long 
 lpstrFileTitle As String 
 nMaxFileTitle As Long 
 lpstrInitialDir As String 
 lpstrTitle As String 
 flags As Longn 
 FileOffset As Integer 
 nFileExtension As Integer 
 lpstrDefExt As String 
 lCustData As Long 
 lpfnHook As Long 
 lpTemplateName As String 
 End Type 
  
  
  
  
  '***********************************保存文件********************************************** 
  
  
 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_ALL_ACCESS = &H1F0FFF 
 
Private Const PROCESS_CREATE_PROCESS = &H80 
 
Private Const PROCESS_CREATE_THREAD = &H2 
 
Private Const PROCESS_DUP_HANDLE = &H40 
 
Private Const PROCESS_QUERY_INFORMATION = &H400 
 
Private Const PROCESS_QUERY_LIMITED_INFORMATION = &H1000 
 
Private Const PROCESS_SET_QUOTA = &H100 
 
Private Const PROCESS_SET_INFORMATION = &H200 
 
Private Const PROCESS_SUSPEND_RESUME = &H800 
 
Private Const PROCESS_TERMINATE = &H1 
 
Private Const PROCESS_VM_OPERATION = &H8 
 
Private Const PROCESS_VM_READ = &H10 
 
Private Const PROCESS_VM_WRITE = &H20 
 
Private Const SYNCHRONIZE = &H100000 
 
Private 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 Type 
 
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 Long 
 
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long 
 
 
 
 
Public Function GetTargetProcessID(ByVal lpProcess As String) As Long '自定义函数用于根据进程名称获得进程pid 
 
 
    Dim dwRet As Long, dBn As Boolean 
    dBn = False 
    dwRet = 0 
    Dim hSnapShot As Long 
 
    Dim pe32 As PROCESSENTRY32 
 
    hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0) '创建进程快照 
 
    pe32.dwSize = LenB(pe32) 
 
    ProcessFirst hSnapShot, pe32 
 
    Do 
 
       If InStr(pe32.szExeFile, lpProcess) > 0 Then  'pe32.szExeFile获取进程名称 
            
           If dBn = True Then 
           dwRet = pe32.th32ProcessID 'pe32.th32ProcessID 获取进程pid 
 
       Exit Do 
       Else 
       dBn = True 
       End If 
 
       Else 
 
           pe32.szExeFile = String(MAX_PATH, 0) 
 
       End If 
 
    Loop While (ProcessNext(hSnapShot, pe32)) 
 
    CloseHandle (hSnapShot)  '关闭进程快照句柄 
 
    GetTargetProcessID = dwRet 
 
End Function 
 
Public Function SaveFile() As String 
On Error GoTo cuowu 
Dim i As Integer 
Dim kuang As OPENFILENAME 
Dim filename As String 
kuang.lStructSize = Len(kuang) 
kuang.hwndOwner = Me.hwnd 
kuang.hInstance = App.hInstance 
kuang.lpstrFile = Space(254) 
kuang.nMaxFile = 255 
kuang.lpstrFileTitle = Space(254) 
kuang.nMaxFileTitle = 255 
kuang.lpstrInitialDir = App.Path 
kuang.flags = 6148 '过虑对话框文件类型 
kuang.lpstrFilter = "文本文件 (*.TXT)" + Chr$(0) + "*.TXT" + Chr$(0) + "所有文件 (*.*)" + Chr$(0) + "*.*" + Chr$(0) '对话框标题栏文字 
kuang.lpstrTitle = "保存文件的路径及文件名..." 
i = GetSaveFileName(kuang) '显示保存文件对话框 
If i >= 1 Then '取得对话中用户选择输入的文件名及路径 
filename = kuang.lpstr 
SaveFile = Left(filename, InStr(filename, Chr(0)) - 1) 
End If 
If Len(filename) = 0 Then Exit Function '保存代码 
Exit Function 
cuowu: 
Close #1 
MsgBox "未知原因导致操作失败!" 
 
End Function