Solved

Application Name

Posted on 2008-10-14
8
475 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

839 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