Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize 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
Dim P As String
Private Function GetPath() As String
    Dim PID As Long, hPss As Long
    Dim FileName As String
    FileName = Space(1024)
    Call GetWindowThreadProcessId(GetForegroundWindow, PID)
    hPss = OpenProcess(&HF0000 Or &H100000 Or &HFFF, False, PID)
    Call GetModuleFileNameExA(hPss, 0, FileName, 1024)
    GetPath = Trim(FileName)
    CloseHandle hPss
End Function当调用Text1.Text=GetPath & "AAAAAA",但Text1中显示的还是完整路径,后面的"AAAAAA"却显示不出来;
若调用Debug.Print GetPath & "AAAAAA",会显示出完整路径与"AAAAAA".请问为什么?

解决方案 »

  1.   

    GetPath = Trim(FileName) '这句不对
    改为
    Getpath=left(FileName,instr(FileName,chr(0))-1)
      

  2.   

    以前写过这类代码。Option ExplicitPrivate Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Const GWL_HINSTANCE = (-6)
    Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As LongSub Main()
        MsgBox GetFilePath(GetForegroundWindow)
    End SubFunction GetFilePath(ByVal hWnd As Long) As String
        Dim hInst As Long
        
        GetFilePath = String(256, vbNullChar)
        hInst = GetWindowLong(hWnd, GWL_HINSTANCE)
        GetModuleFileName hInst, GetFilePath, Len(GetFilePath)
        GetFilePath = Left(GetFilePath, InStr(GetFilePath, vbNullChar) - 1)
    End Function