中文字幕理论片,69视频免费在线观看,亚洲成人app,国产1级毛片,刘涛最大尺度戏视频,欧美亚洲美女视频,2021韩国美女仙女屋vip视频

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
vba 如何獲取dll文件的路徑

本文說(shuō)明在vba中如何獲取進(jìn)程加載的dll文件的路徑。

工具/原料

  • 32位Office 2007以上或VB6

方法/步驟

  1. 1

    新建VBA工程

    打開(kāi)Excel,按下Atl + F11,打開(kāi)VBA工程。

  2. 2

    增加一個(gè)Form

    增加一個(gè)Form,F(xiàn)orm上有一個(gè)Label、一個(gè)文本框、一個(gè)按鈕和一個(gè)ListBox控件。文本框用來(lái)輸入進(jìn)程的名稱(chēng),ListBox用來(lái)顯示dll的路徑

  3. 3

    增加一個(gè)模塊

    新增加一個(gè)VBA模塊,貼入如下代碼。

    Option Explicit

    Public Const PROCESS_QUERY_INFORMATION = 1024

    Public Const PROCESS_VM_READ = 16

    Public Const MAX_PATH = 260

    Public Const WINNT_System_Found = 2

    Type PROCESS_MEMORY_COUNTERS

        cb As Long

        PageFaultCount As Long

        PeakWorkingSetSize As Long

        WorkingSetSize As Long

        QuotaPeakPagedPoolUsage As Long

        QuotaPagedPoolUsage As Long

        QuotaPeakNonPagedPoolUsage As Long

        QuotaNonPagedPoolUsage As Long

        PagefileUsage As Long

        PeakPagefileUsage As Long

    End Type

    Public Type OSVERSIONINFO

        dwOSVersionInfoSize As Long

        dwMajorVersion As Long

        dwMinorVersion As Long

        dwBuildNumber As Long

        dwPlatformId As Long '1 = Windows 95.

        '2 = Windows NT

        szCSDVersion As String * 128

    End Type

    Public Declare Function GetProcessMemoryInfo Lib "PSAPI.DLL" (ByVal hProcess As Long, ppsmemCounters As PROCESS_MEMORY_COUNTERS, ByVal cb As Long) As Long

    Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long

    Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long

    Public Declare Function EnumProcesses Lib "PSAPI.DLL" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long

    Public Declare Function GetModuleFileNameExA Lib "PSAPI.DLL" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long

    Public Declare Function EnumProcessModules Lib "PSAPI.DLL" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long

    Public Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer

    Public Declare Function GetCurrentProcessId Lib "kernel32" () As Long

    Public Sub GetDLLs(ByVal EXEName As String, list As Collection)

        Dim lngLength               As Long

        Dim strProcessName          As String

        Dim lngCBSize               As Long 'Specifies the size, In bytes, of the lpidProcess array

        Dim lngCBSizeReturned       As Long 'Receives the number of bytes returned

        Dim lngNumElements          As Long

        Dim lngProcessIDs()         As Long

        Dim lngCBSize2              As Long

        Dim lngModules(1 To 200)    As Long

        Dim lngReturn               As Long

        Dim strModuleName           As String

        Dim lngSize                 As Long

        Dim lngHwndProcess          As Long

        Dim lngLoop                 As Long

        Dim pmc                     As PROCESS_MEMORY_COUNTERS

        Dim lRet                    As Long

        Dim strProcName2            As String

        Dim llLoop                  As Long

        Dim llEnd                   As Long

        'Turn on Error handler

    On Error GoTo Error_handler

        EXEName = UCase$(Trim$(EXEName))

        lngLength = Len(EXEName)

        lngCBSize = 8 ' Really needs To be 16, but Loop will increment prior to calling API

        lngCBSizeReturned = 96

        If EXEName <> "" Then

            Do While lngCBSize <= lngCBSizeReturned

                DoEvents

                'Increment Size

                lngCBSize = lngCBSize * 2

                'Allocate Memory for Array

                ReDim lngProcessIDs(lngCBSize / 4) As Long

                'Get Process ID's

                lngReturn = EnumProcesses(lngProcessIDs(1), lngCBSize, lngCBSizeReturned)

            Loop

            lngNumElements = lngCBSizeReturned / 4

        Else

            ReDim lngProcessIDs(1) As Long

            lngProcessIDs(1) = GetCurrentProcessId

            lngNumElements = 1

        End If

        'Count number of processes returned

        'Loop thru each process

        For lngLoop = 1 To lngNumElements

            'Get a handle to the Process and Open

            lngHwndProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lngProcessIDs(lngLoop))

            If lngHwndProcess <> 0 Then

                'Get an array of the module handles for the specified process

                lngReturn = EnumProcessModules(lngHwndProcess, lngModules(1), 200, lngCBSize2)

                'If the Module Array is retrieved, Get the ModuleFileName

                If lngReturn <> 0 Then

                    llEnd = lngCBSize2 / 4

                    'Buffer with spaces first to allocate memory for byte array

                    strModuleName = Space(MAX_PATH)

                    'Must be set prior to calling API

                    lngSize = 500

                    'Get Process Name

                    lngReturn = GetModuleFileNameExA(lngHwndProcess, lngModules(1), strModuleName, lngSize)

                    'Remove trailing spaces

                    strProcessName = Left(strModuleName, lngReturn)

                    'Check for Matching Upper case result

                    strProcessName = UCase$(Trim$(strProcessName))

                    strProcName2 = GetElement(Trim(Replace(strProcessName, Chr$(0), "")), "\", 0, 0, _

                                GetNumElements(Trim(Replace(strProcessName, Chr$(0), "")), "\") - 1)

                    'All the items for the process

                    If EXEName = "" Or strProcName2 = ExtractFileName(EXEName) Then

                        For llLoop = 1 To llEnd

                            lngReturn = GetModuleFileNameExA(lngHwndProcess, lngModules(llLoop), strModuleName, lngSize)

                            'Remove trailing spaces

                            strProcessName = Left(strModuleName, lngReturn)

                            'Check for Matching Upper case result

                            strProcessName = UCase$(Trim$(strProcessName))

    '                                strProcName2 = GetElement(Trim(Replace(strProcessName, Chr$(0), "")), "\", 0, 0, _

                                        GetNumElements(Trim(Replace(strProcessName, Chr$(0), "")), "\") - 1)

                            'Add path to the Collection

                            If Right$(strProcessName, 4) = ".DLL" Then list.Add strProcessName

                        Next

                        'Get the Site of the Memory Structure

                        pmc.cb = LenB(pmc)

                        lRet = GetProcessMemoryInfo(lngHwndProcess, pmc, pmc.cb)

                    End If

                End If

            End If

            'Close the handle to this process

            lngReturn = CloseHandle(lngHwndProcess)

            'DoEvents

        Next

    IsProcessRunning_Exit:

        'Exit early to avoid error handler

        Exit Sub

    Error_handler:

        Err.Raise Err, Err.Source, "ProcessInfo", Error

        Resume Next

    End Sub

    Private Function ExtractFileName(ByVal vStrFullPath As String) As String

       Dim intPos As Integer

       intPos = InStrRev(vStrFullPath, "\")

       ExtractFileName = UCase$(Mid$(vStrFullPath, intPos + 1))

    End Function

    Private Function getOsVersion() As Long

        Dim osinfo As OSVERSIONINFO

        Dim retvalue As Integer

        osinfo.dwOSVersionInfoSize = 148

        osinfo.szCSDVersion = Space$(128)

        retvalue = GetVersionExA(osinfo)

        getOsVersion = osinfo.dwPlatformId

    End Function

    Public Function GetElement(ByVal strList As String, ByVal strDelimiter As String, ByVal lngNumColumns As Long, ByVal lngRow As Long, ByVal lngColumn As Long) As String

        Dim lngCounter As Long

        ' Append delimiter text to the end of the list as a terminator.

        strList = strList & strDelimiter

        ' Calculate the offset for the item required based on the number of columns the list

        ' 'strList' has i.e. 'lngNumColumns' and from which row the element is to be

        ' selected i.e. 'lngRow'.

        lngColumn = IIf(lngRow = 0, lngColumn, (lngRow * lngNumColumns) + lngColumn)

        ' Search for the 'lngColumn' item from the list 'strList'.

        For lngCounter = 0 To lngColumn - 1

            ' Remove each item from the list.

            strList = Mid$(strList, InStr(strList, strDelimiter) + Len(strDelimiter), Len(strList))

            ' If list becomes empty before 'lngColumn' is found then just

            ' return an empty string.

            If Len(strList) = 0 Then

                GetElement = ""

                Exit Function

            End If

        Next lngCounter

        ' Return the sought list element.

        GetElement = Left$(strList, InStr(strList, strDelimiter) - 1)

    End Function

    Public Function GetNumElements(ByVal strList As String, ByVal strDelimiter As String) As Integer

        Dim intElementCount As Integer

        ' If no elements in the list 'strList' then just return 0.

        If Len(strList) = 0 Then

            GetNumElements = 0

            Exit Function

        End If

        ' Append delimiter text to the end of the list as a terminator.

        strList = strList & strDelimiter

        ' Count the number of elements in 'strlist'

        While InStr(strList, strDelimiter) > 0

            intElementCount = intElementCount + 1

            strList = Mid$(strList, InStr(strList, strDelimiter) + 1, Len(strList))

        Wend

        ' Return the number of elements in 'strList'.

        GetNumElements = intElementCount

    End Function

  4. 4

    增加Command按鈕事件

    雙擊Command按鈕,增加如下代碼:

    Private Sub CommandButton1_Click()

        Dim objDlls As New Collection

        Dim lngIndex    As Long

        GetDLLs TextBox1.Text, objDlls

        ListBox1.Clear

        For lngIndex = 1 To objDlls.Count

            ListBox1.AddItem objDlls(lngIndex)

        Next

    End Sub

  5. 5

    獲取當(dāng)前進(jìn)程的dll路徑

    選擇UserForm1,按F5運(yùn)行,點(diǎn)擊按鈕,可以獲取當(dāng)前進(jìn)程所有dll的路徑。

  6. 6

    獲取指定進(jìn)程的dll路徑

    選擇UserForm1,按F5運(yùn)行,在本文框中輸入進(jìn)程名稱(chēng),點(diǎn)擊按鈕,就可以獲取和進(jìn)程名稱(chēng)匹配的進(jìn)程的所有dll的路徑。

  7. 7

    下載測(cè)試工程代碼

    測(cè)試用的Excel及VBA代碼共享在如下位置,可以自行下來(lái)測(cè)試。

    下載路徑:http://pan.baidu.com/s/1bpAnpcN

    END

注意事項(xiàng)

  • 本經(jīng)驗(yàn)還會(huì)不斷補(bǔ)充和完善,直到有一天我們發(fā)現(xiàn)這篇經(jīng)驗(yàn)已無(wú)存在價(jià)值。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Excel中調(diào)用VBA選擇目標(biāo)文件夾
教大家獲取程序的所有模塊及模塊基址(附代碼)
使用VB設(shè)置IE代理
VB與FTP編程
【黒客編程】VB木馬也瘋狂
[VBA][高階應(yīng)用] DLL動(dòng)態(tài)調(diào)用
更多類(lèi)似文章 >>
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服