Results 1 to 2 of 2

Thread: Dll

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    Dll

    Hi

    Does anyone have any ideas on how to retrieve the exe path & name of a calling executable from within a .dll file. I want to be able to identify what exe file called a dll procedure...

    Thanks

  2. #2
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Dll

    The following will give you a tree structure of all the processes on your system, and what process spawned what process. It may still need some interpretation in order to get what you need:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    4. Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    5. Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    6. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    7.  
    8. Private Type PROCESS_INFORMATION
    9.    hProcess As Long
    10.    hThread As Long
    11.    dwProcessId As Long
    12.    dwThreadId As Long
    13. End Type
    14.  
    15. Private Type PROCESS_TREE
    16.     ProcessId As Long
    17.     ParentProcessId As Long
    18. End Type
    19.  
    20. Private Type PROCESSENTRY32
    21.     dwSize As Long
    22.     cntUsage As Long
    23.     th32ProcessID As Long
    24.     th32DefaultHeapID As Long
    25.     th32ModuleID As Long
    26.     cntThreads As Long
    27.     th32ParentProcessID As Long
    28.     pcPriClassBase As Long
    29.     dwFlags As Long
    30.     szexeFile As String * 6400
    31. End Type
    32.  
    33. Private Const TH32CS_SNAPPROCESS As Long = 2&
    34. Private Const INVALID_HANDLE_VALUE As Long = -1
    35. Private Const WINAPI_TRUE = 1
    36.  
    37. Private Sub Form_Load()
    38.  
    39.     Dim ProcessTree() As PROCESS_TREE
    40.     Dim n As Long
    41.    
    42.     ProcessTree = GetProcessList()
    43.    
    44. End Sub
    45.  
    46. Private Function GetProcessList() As PROCESS_TREE()
    47.  
    48.     On Error GoTo ERR_GetProcessTree
    49.  
    50.     Dim hSnapShot As Long
    51.     Dim hProcess As Long
    52.     Dim uProcessEntry As PROCESSENTRY32
    53.     Dim lSuccess As Long
    54.     Dim ProcessTree() As PROCESS_TREE
    55.     Dim lCtr As Long
    56.    
    57.     '************************************************************
    58.     '* Get a snapshot of all of the processes in the system . . .
    59.     '************************************************************
    60.     hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    61.    
    62.     '***********************************************
    63.     '* If we don't have a snapshot then finish . . .
    64.     '***********************************************
    65.     If hSnapShot = INVALID_HANDLE_VALUE Then
    66.         Err.Raise vbObjectError + 512, , "Unable To Get Process Snapshot"
    67.     Else
    68.    
    69.         '*********************************
    70.         '* Get first process in list . . .
    71.         '*********************************
    72.         uProcessEntry.dwSize = Len(uProcessEntry)
    73.         lSuccess = ProcessFirst(hSnapShot, uProcessEntry)
    74.        
    75.         If lSuccess = WINAPI_TRUE Then
    76.        
    77.             lCtr = 0
    78.            
    79.             '**********************************
    80.             '* Loop through all processes . . .
    81.             '**********************************
    82.             Do Until lSuccess <> WINAPI_TRUE
    83.            
    84.                 ReDim Preserve ProcessTree(lCtr)
    85.                 With ProcessTree(lCtr)
    86.                     .ParentProcessId = uProcessEntry.th32ParentProcessID
    87.                     .ProcessId = uProcessEntry.th32ProcessID
    88.                    
    89.                     Debug.Print .ParentProcessId, .ProcessId, Replace(uProcessEntry.szexeFile, Chr(0), vbNullString)
    90.                    
    91.                 End With
    92.                
    93.                
    94.                
    95.                 lCtr = lCtr + 1
    96.                 lSuccess = ProcessNext(hSnapShot, uProcessEntry)
    97.                
    98.             Loop
    99.        
    100.         Else
    101.             Err.Raise vbObjectError + 512, , "Unable To Get First Process In Snapshot"
    102.         End If
    103.    
    104.     End If
    105.    
    106.     '********************************
    107.     '* Release handle resources . . .
    108.     '********************************
    109.     CloseHandle (hSnapShot)
    110.    
    111.     GetProcessList = ProcessTree
    112.     Exit Function
    113.    
    114. ERR_GetProcessTree:
    115.  
    116.     CloseHandle (hSnapShot)
    117.     Err.Raise Err.Number, Err.Source, Err.Description
    118.    
    119. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width