Solved

Application Name

Posted on 2008-10-14
8
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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

Technology Partners: 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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 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…

749 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