Solved

Application Name

Posted on 2008-10-14
8
462 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
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 22711910
Epaclm,
This code appears to return the language identifier  and not the EXE parent name.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now