PDA

View Full Version : How can I find the addrees of exe file in C++?



bahador.saket
July 8th, 2010, 12:26 PM
Dear all

I am going the get the address of my .exe files in windows , but unfortunately I can not.
Also I am using Code::Block.
I want a part of code that return the address of my exe file.


Regards,
Bahador Saket

dwhitney67
July 8th, 2010, 01:53 PM
Can you please elaborate a little further what you mean by "address"? Do you mean the directory (folder) location?

johnl
July 8th, 2010, 04:55 PM
Check out GetModuleFileName (http://msdn.microsoft.com/en-us/library/ms683197%28VS.85%29.aspx):




#include <windows.h>

// ...
TCHAR szExe[512];
DWORD dwLen;

dwLen = GetModuleFileName(NULL, szExe, sizeof(szExe));

if (dwLen == 0) {
// error: check GetLastError()
} else if (dwLen == sizeof(szExe)) {
// check GetLastError() for ERROR_INSUFFICIENT_BUFFER
// result might have been truncated
} else {
// success, exe now contains the path and name of your
// executable
}