Solved

Application Name

Posted on 2008-10-14
8
474 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:PhilChapmanJr
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 39

Expert Comment

by:appari
ID: 22711140
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))
0
 
LVL 39

Expert Comment

by:appari
ID: 22711147

I am sorry ignore my comment, that is for VB.Net
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 22711262
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 22711910
Epaclm,
This code appears to return the language identifier  and not the EXE parent name.
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 22715995
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
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 22716893
Depending on exactly what operating system your targeting.  GetProcessImageFileName or QueryFullProcessImageName function. This is more efficient and more reliable than calling the GetModuleFileNameEx function with a NULL module handle.

QueryFullProcessImageName - is for vista only.
GetProcessImageFileName - is for Windows Vista or Windows XP.
GetModuleFileNameEx - Windows Vista, Windows XP, or Windows 2000 Professional.

Just a side note to be safe its probrably best to use OpenProcess and specify the access masks for the process. The handle must have the PROCESS_QUERY_INFORMATION and PROCESS_VM_READ access rights. However for my example I will just use GetCurrentProcess to point you into the right direction.


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 GetCurrentProcess Lib "kernel32" () As Long
 
Public Function ModuleFileName() As String
    
    Dim Buffer(1024)        As Byte
    Dim dwLength            As Long
    
    dwLength = GetModuleFileNameExW(GetCurrentProcess, 0, VarPtr(Buffer(0)), UBound(Buffer))
    ModuleFileName = Left$(Buffer, dwLength)
    Erase Buffer
    
End Function

Open in new window

0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 22717185
' 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

0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 22720449
Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question