?
Solved

Application Name

Posted on 2008-10-14
8
Medium Priority
?
492 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:Phil Chapman
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:Phil Chapman
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 2000 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:Phil Chapman
ID: 22720449
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

850 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