?
Solved

Find Filename

Posted on 2003-02-24
10
Medium Priority
?
594 Views
Last Modified: 2013-11-20
Ok im new to some of the advanced parts of mfc and was wondering how to/if posible to find the file name of my program

etc. the program was named test.exe by default and i changed it to "my program.exe" and i copy the file to a backup so nothing can go wrong

is there any way to get the filename so i can copy the right file and not a fixed file name ;)

thanks to anyone that helps
0
Comment
Question by:zeurx
[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
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 640 total points
ID: 8012918
You can use the functions GetCurrentProcess, EnumProcessModules, and GetModuleBaseName to get the name of the executable, like:
#include <windows.h>
#include <psapi.h>

int main()
{
  HANDLE process = GetCurrentProcess(); // Get the process handle
  HMODULE module;
  DWORD amount_needed;
  EnumProcessModules(process, &module, sizeof module, &amount_needed); // List out the process modules
  char process_name[1024] = {0};
  GetModuleBaseName(process, module, process_name, 1024); // Get the name
  MessageBox(NULL, process_name, "", MB_OK);

  return 0;
}

You also have to link to psapi.lib.
0
 

Expert Comment

by:mrbiggles
ID: 8012979
Hi,

An easy way to get your app's name is to use the MFC global function:

AfxGetAppName( );

This will give you the internal name of your app, and will not change even if the executable is manually renamed.  In your case you would always get "test" without the .exe extension.

If you wanted to get "my program" (again without .exe) you can do this:

AfxGetApp( )->m_pszExeName;

Hope this helps.
-TS
0
 

Author Comment

by:zeurx
ID: 8012981
mnashadka this helps alot but i get to linking errors:

Program error LNK2019: unresolved external symbol _GetModuleBaseNameA@16 referenced in function "protected: virtual int __thiscall CProgramDlg::OnInitDialog(void)" (?OnInitDialog@CProgramDlg@@MAEHXZ)

and

Program error LNK2019: unresolved external symbol _EnumProcessModules@16 referenced in function "protected: virtual int __thiscall CProgramDlg::OnInitDialog(void)" (?OnInitDialog@CProgramDeath2Dlg@@MAEHXZ)

then a ending error

please can you help... i included the files i needed.
thanks ;)

Program fatal error LNK1120: 2 unresolved externals
0
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!

 

Author Comment

by:zeurx
ID: 8013028
mnashadka this helps alot but i get to linking errors:

Program error LNK2019: unresolved external symbol _GetModuleBaseNameA@16 referenced in function "protected: virtual int __thiscall CProgramDlg::OnInitDialog(void)" (?OnInitDialog@CProgramDlg@@MAEHXZ)

and

Program error LNK2019: unresolved external symbol _EnumProcessModules@16 referenced in function "protected: virtual int __thiscall CProgramDlg::OnInitDialog(void)" (?OnInitDialog@CProgramDeath2Dlg@@MAEHXZ)

then a ending error

please can you help... i included the files i needed.
thanks ;)

Program fatal error LNK1120: 2 unresolved externals
0
 
LVL 86

Expert Comment

by:jkr
ID: 8013407
The easiest way:

char acModuleName [ MAX_PATH];

GetModuleFileName ( NULL, acModuleName, sizeof ( acModuleName));
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8013670
To use my way, you also need to link with psapi.lib.  jkr's is a little easier, but it returns the whole path and you have to parse past the '\'.  Good luck.
0
 

Author Comment

by:zeurx
ID: 8020157
how do i add psapi.lib to my source...

i tried #inculde <psapi.lib>
and     #inculde "psapi.lib"

now working... says it cant find the file
0
 
LVL 86

Expert Comment

by:jkr
ID: 8020190
>>#inculde "psapi.lib"

This should read

#pragma comment ( lib, "psapi.lib")
0
 

Author Comment

by:zeurx
ID: 8020203
how do i add psapi.lib to my source...

i tried #inculde <psapi.lib>
and     #inculde "psapi.lib"

now working... says it cant find the file
0
 

Author Comment

by:zeurx
ID: 8020260
just add #pragma comment ( lib, "psapi.lib")
0

Featured Post

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!

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 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