Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

Find Filename

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
zeurx
Asked:
zeurx
  • 5
  • 2
  • 2
  • +1
1 Solution
 
mnashadkaCommented:
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
 
mrbigglesCommented:
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
 
zeurxAuthor Commented:
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
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.

 
zeurxAuthor Commented:
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
 
jkrCommented:
The easiest way:

char acModuleName [ MAX_PATH];

GetModuleFileName ( NULL, acModuleName, sizeof ( acModuleName));
0
 
mnashadkaCommented:
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
 
zeurxAuthor Commented:
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
 
jkrCommented:
>>#inculde "psapi.lib"

This should read

#pragma comment ( lib, "psapi.lib")
0
 
zeurxAuthor Commented:
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
 
zeurxAuthor Commented:
just add #pragma comment ( lib, "psapi.lib")
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!

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now