Link to home
Start Free TrialLog in
Avatar of Phil Chapman
Phil ChapmanFlag for United States of America

asked on

Application Name

Application Name
How can I get the name of an application within a DLL.

Example:
Application Name = MyApplication.EXE
Dll Name = MyDLL.DLL

MyDLL.DLL is accessed by MyApplication.EXE
If you do MsgBox App.EXEName within MyDLL.DLL it returns MyDLL .  How can I get the name of the application?  In this case MyApplication

Avatar of appari
appari
Flag of India image

you have to use getcallingassembly method from reflection
try, example from MSDN
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getcallingassembly.aspx

Dim SampleAssembly As [Assembly]
    ' Instantiate a target object.
    Dim Integer1 As New Int32()
    Dim Type1 As Type
    ' Set the Type instance to the target class type.
    Type1 = Integer1.GetType()
    ' Instantiate an Assembly class to the assembly housing the Integer type.  
    SampleAssembly = [Assembly].GetAssembly(Integer1.GetType())
    ' Display the name of the assembly that is calling the method.
    Console.WriteLine(("GetCallingAssembly=" + [Assembly].GetCallingAssembly().FullName))

I am sorry ignore my comment, that is for VB.Net
Avatar of Phil Chapman

ASKER

Epaclm,
This code appears to return the language identifier  and not the EXE parent name.
Sorry, the best I could find. Maybe it can be updated to find more info. Try and see what you get with this file.

Curt
Find-DLL-details.xls
ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

' Here is the so called correct way to do this thanks for the points.

Option Explicit
 
Private Declare Function GetModuleFileNameExW Lib "Psapi" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As Long, 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
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
 
Private Const PROCESS_QUERY_INFORMATION     As Long = &H400
Private Const PROCESS_VM_READ               As Long = &H10
 
Public Function ModuleFilename() As String
    
    Dim Buffer(1024)    As Byte
    Dim hProcess        As Long
    Dim dwLength        As Long
    Dim dwPid           As Long
    
    ' * get this applications process id
    dwPid = GetCurrentProcessId
    ' * get the real process handle with correct access masks.
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, dwPid)
    ' * get the name of the application
    dwLength = GetModuleFileNameExW(hProcess, 0, VarPtr(Buffer(0)), UBound(Buffer))
    ' * ToString
    ModuleFilename = Left$(Buffer, dwLength)
    ' * cleanup the process handle
    CloseHandle hProcess
    ' * cleanup the buffer
    Erase Buffer
    
End Function

Open in new window

Thanks