is there anyway to get the name of the exe at runtime? Not through a console app so theres no Main the first sub ran is int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
thanks for helpin out a newbie
Printable View
is there anyway to get the name of the exe at runtime? Not through a console app so theres no Main the first sub ran is int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
thanks for helpin out a newbie
You can use either
orPHP Code:GetModuleFileName(HMODULE, LPTSTR, DWORD)
PHP Code:GetModuleFileNameEx(HANDLE, HMODULE, LPTSTR, DWORD)
ok that second post looks like it will work but how do you use it? Could someone give me an example or explain how to use it better, thanks
OK, Here is an example:
PHP Code:#include <iostream.h>
#include <windows.h>
int main()
{
int x;
char *exefile;
GetModuleFileName(GetModuleHandle(NULL), exefile, 200);
cout<<exefile;
cin>>x;
return 0;
}
my program keeps on crashing right away when i run that code, even the code alone crashes. Does this happen for anyone else? How could i possibly fix this? Err msg: "The instruction at "0x77f82cc5" referenced memory at "0xcccccccc". The memory could not be "written"." I dont know if that will help at all but its one of thoes illegal operation type error messages. Thanks
I works find for me. Try to run this code after restarting your machine:D;)
If it still does not work then reply again:)
yep it causes an illegal operation on my 2k box and my 98se box :(
Well, I am running windows 98 SE and it works fine for me. Try this now with a little change:
PHP Code:#include <iostream.h>
#include <windows.h>
int main()
{
int x;
char exefile[MAX_PATH];
GetModuleFileName((HMODULE)GetModuleHandle(NULL), exefile, MAX_PATH);
cout<<exefile;
cin>>x;
return 0;
}
The difference between the two is that in the first one you hadn't allocated a buffer, so the pointer was totally random. Therefore you get an Access Violation.
The second one has a buffer, which is why it works :)
ok I think I narrowed the illegal op down to the end of the app, whats the correct way to end it? Right now im using return 0; but thats causing it to crash for some reason, Ive also tried not putting anything and it still crashes. What to do?
Can you post the exact source of your main() function, please?