get this File name?

I've found some tutorials about getting all file names in directory or etc. But how to get current file name. For example if I open file "clientbla.exe" ir will cout in the concole "clientbla.exe", if I rename it, it would show different, renamed value.

Is this possible?
What do you mean? You want to get the filename of the program you're running (i.e. a program called "helloworld.exe" printing "Hello world from helloworld.exe!")?

Very simple.
Declare main like this:
int main(int argc, char** argv)
or
int main(int argc, char* argv[])
argv[0] is the name of the program. So in our "helloworld.exe" program:
1
2
3
4
5
#include <iostream>

int main(int argc, char** argv) {
    std::cout << "Hello world from " << argv[0] << "!\n";
}

that is, of course, if this is in fact what you're asking; because your question is very hard to interpret.
Last edited on
argv[0] gives me exact locatoin of the file and also the name, but it still might work but how can i convert argv[0] to LPCTSTR?

The old way
1
2
3
	TCHAR str[] = _T("myfile.exe");
	LPCTSTR self = str;
	SetFileAttributes(self, FILE_ATTRIBUTE_READONLY); 



but it now work like

TCHAR str[] = _T(argv[0]); or TCHAR str[] = argv[0];

Any solutions with that?
Ar, I'm in over my head here. I can only just remember what an LPCTSTR is and that was something I found on the fly when I was trying to help someone with something I forget. That was like a week ago.

Sorry I can't help you with this; it's just, I strive not to use windows except to play games, so naturally I've forgotten what little I did know about the API (I could create a basic window, nothing more). I was hoping, when I saw this, that it was one of those occasional threads on the windows programming board that belong in "General C++ programming". But as for LCPTSTRs; I know nothing about them.
Try using the ANSI version of SetFileAttributes function.

SetFileAttributesA(argv[0]),FILE_ATTRIBUTE_READONLY);
I managed to solve the problem, for anyone who still need this:

1
2
3
4
5
6
	int len = strlen(argv[0])+1;
	wchar_t *wText = new wchar_t[len];
	memset(wText,0,len);
	::MultiByteToWideChar(  CP_ACP, NULL,argv[0], -1, wText,len );

	SetFileAttributes(wText,  FILE_ATTRIBUTE_READONLY); 


thanks for argv[0] idea, lifesaver.
Surely that is the long way around - SetFileAttributesA is one line.
Topic archived. No new replies allowed.