[try Beta version]
Not logged in

 
GetModuleFileName ( NULL, szEXEPath, 2048 );

Dec 17, 2008 at 11:50pm
Long story made short, I felt the need for my program to be able to identify it's executable location. I stumbled across this function: GetModuleFileName ( NULL, szEXEPath, 2048 ); and from what I read was under the impression that this returned the full path of the executable. I did not experience this. Can someone please tell me what I am doing wrong?

This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <tchar.h>
#include <iostream>
#include <Windows.h>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	/* I thought this should display the path to the executable, but I am wrong */
	TCHAR szEXEPath[2048];
	cout << GetModuleFileName ( NULL, szEXEPath, 2048 );

	/* PAUSE PROGRAM */
	int i;
	cin >> i;
	return 0;
}


This is my output:
103

From some of what I read, it said this returned the length of the path (is that useful??) and other places I read that this actually returns the path. Given that I got a number, it is likely it is returning the length. What can I do to get this right?

Thank you so much for your help!!
Dec 18, 2008 at 12:05am
Ok, so I found that if I looked to see what was in szEXEPath, each array index had an ASCII number in it... so I figured maybe it was returning the ASCII value instead. I re-wrote the code like so and it does what I think it should...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <tchar.h>
#include <iostream>
#include <Windows.h>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	TCHAR szEXEPath[2048];
	char actualpath[2048];
	GetModuleFileName ( NULL, szEXEPath, 2048 );


/* Prints the full path */
	for(int j=0; szEXEPath[j]!=0; j++)
	{
		actualpath[j]=szEXEPath[j];
		cout << actualpath[j];
	}
	/* PAUSE PROGRAM */
	int i;
	cin >> i;
	return 0;
}


Is this how this is intended to be used? It appears a bit cumbersome to me...
Last edited on Dec 18, 2008 at 12:05am
Dec 18, 2008 at 11:46am
It helps to re-read the documentation for GetModuleFileName()
http://msdn.microsoft.com/en-us/library/ms683197(VS.85).aspx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <tchar.h>
#include <iostream>
#include <limits>
#include <windows.h>
using namespace std;

int _tmain( int argc, _TCHAR* argv[] )
{
	TCHAR szEXEPath[ 2048 ];
	DWORD nChars = GetModuleFileName( NULL, szEXEPath, 2048 );

	cout << '\n';
	for (DWORD n = 10; n <= nChars; n += 10)
		cout << "         " << (n / 10);
	cout << '\n';
	for (DWORD n = 1; n <= nChars; n++)
		cout << (n % 10);
	cout << '\n';
	cout << szEXEPath << '\n';
	cout << nChars << " characters.\n";

	// Pause
	cout << "\n\nPress Enter to quit..." << flush;
	cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
	return 0;
}

Hope this helps.
Dec 28, 2008 at 4:24am
Yes, Duoas has it correct, but to put it more simply:

GetModuleFileName() takes 3 arguments:
1.) A handle to a module (Just use NULL to retrieve the "current" prog's path)
2.) A pointer to a char[] buffer
3.) The size of the buffer
And it returns the number of characters that were copied to the buffer.

So to use it you would write something like this:
1
2
3
char buffer[MAX_PATH];//always use MAX_PATH for filepaths
GetModuleFileName(NULL,buffer,sizeof(buffer));
cout << "Filepath:" << buffer << "\n";


And it will print the filepath of the current exe.

Hope that it works for you.

SigmaSecurity


Dec 29, 2008 at 9:30am
SigmaSecurity (4)
if the IDE is VS2005
your code have one error
error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char [260]' to 'LPWCH'
Last edited on Dec 30, 2008 at 3:40am
Topic archived. No new replies allowed.