Hi

I'm wondering if there is a simple win32 API to retrieve
the current module filename (xxxx.exe)

I know about GetModuleFilename which gets the full path.
And I have read that argv[0] holds the exe name, but that also give me the full path.

What I'm after is an API to give me just the name, with or without an extension.

Any simple solutions?

Recommended Answers

Simply strip away the path from the names you get from GetModuleFilename or args[0]. As in:

#include <iostream>
#include <string>
#include <algorithm>

int main(int argc, char** argv) {

  std::string full_name = argv[0];

  std::cout << "Full name: " << full_name << std::endl;

  std::string exec_name(std::find(full_name.rbegin(),
                                  full_name.rend(),
                                  '\\').base(), …
Jump to Post

All 2 Replies

Simply strip away the path from the names you get from GetModuleFilename or args[0]. As in:

#include <iostream>
#include <string>
#include <algorithm>

int main(int argc, char** argv) {

  std::string full_name = argv[0];

  std::cout << "Full name: " << full_name << std::endl;

  std::string exec_name(std::find(full_name.rbegin(),
                                  full_name.rend(),
                                  '\\').base(), 
                        full_name.end());

  std::cout << "Exec name: " << exec_name << std::endl;

  return 0;
};

Thank you, I do appreciate your time.

I had been searching for quite some time for a simple solution
and of course only found one after I asked for help, typical.

What I found was GetModuleBaseName() http://msdn.microsoft.com/en-us/library/ms683196%28v=vs.85%29.aspx

Thank you again, I'm grateful for your answer.

commented: +1 for being thoughtful to post your found solution +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.