Current Filename and File path

  • Thread starter Thread starter Mike C#
  • Start date Start date
M

Mike C#

Hi all,

Probably a really simple answer to this, but so far I'm having trouble
locating the answer. I'm writing an unmanaged VC++ app and I need to grab
the currently executing filename and file path from inside the application.
So if I am in the directory C:\Program Files\Test_Dir and I run TestApp
(.exe file), I need to grab the current directory ("C:\Program
Files\Test_Dir") and the application's full filename ("TestApp.exe"). Any
ideas are appreciated.

Thanks
 
Mike C# said:
I'm writing an unmanaged VC++ app and I need to grab the currently
executing filename and file path from inside the application.

Check the docs for GetModuleFileName(0, ...);

Regards,
Will
 
Thanks, I'll look into it. I was afraid I would have to do something funky
with the process id for a minute :) Thanks
 
Thanks Will, it works great!

By the way, is there a method for grabbing resources (i.e., version number,
copyright, etc.) from within the current process? Currently I'm having to
open the file again (hence the GetModuleFileName call) to read the
resources. It seems like there should be a way to grab the resources from
memory, since the file is already loaded and executing. Thanks again!
 
Mike C# said:
Thanks Will,

You are welcome.
it works great!

I try hard not to give bogus information. :-)
By the way, is there a method for grabbing resources (i.e., version
number, copyright, etc.) from within the current process? Currently I'm
having to open the file again (hence the GetModuleFileName call) to read
the resources.

How are you opening it? If it is with GetFileVersionInfo() then I think that
that is the canonical way.

Regards,
Will
 
William DePalo said:
You are welcome.


I try hard not to give bogus information. :-)


How are you opening it? If it is with GetFileVersionInfo() then I think
that that is the canonical way.

Yeah I'm using GetFileVersionInfo(), but it just seems odd that I have to
grab the file by pathname to get the resource information of itself! :) I
thought there might be a method out there to grab the resources from memory
without all the hocus-pocus, but hey this method works!

Thanks again!
 
Mike C# said:
Yeah I'm using GetFileVersionInfo(), but it just seems odd that I have to
grab the file by pathname to get the resource information of itself! :)

Well, it is a resource so there just may be, but being the lazy &^%$# that I
am, I have never researched an alternative. :-)

Seriously, with the application running, at least sone of its image file
must be loaded in memory. I am happy to let the cache manager sort it out.

Are you building a resource editor or something?

Regards,
Will
 
William DePalo said:
Well, it is a resource so there just may be, but being the lazy &^%$# that
I am, I have never researched an alternative. :-)

Seriously, with the application running, at least sone of its image file
must be loaded in memory. I am happy to let the cache manager sort it out.

Are you building a resource editor or something?

Nah, nothing that fancy :) I'm re-writing some applications here at work,
and I don't like the fact that all the "metadata" is hard-coded into the
apps. The previous designer hard-coded app name, version, etc. using
#define style constants. Some of the app names and versions changed, but
the hard-coded info wasn't updated along the way in many cases -- so I
figured I'd use resources to add some flexibility and as a better method of
keeping all this metadata in sync, since I'm rewriting these things from
scratch anyway. :)
 
Mike C# said:
so I figured I'd use resources to add some flexibility and as a better
method of keeping all this metadata in sync, since I'm rewriting these
things from scratch anyway. :)

I see. As I said, I've always used GetFileVersionInfo...() functions.

But I just tried this quick and awful hack

HRSRC hrsrc = FindResource(hInst, VS_VERSION_INFO, VERSIONINFO);
HGLOBAL hglbl = LoadResource(hInst, hrsrc);
LPVOID pv = LockResource(hglbl);

UINT u;
LPCSTR p;
VerQueryValue(pv, "\\StringFileInfo\\040904B0\\FileDescription", (LPVOID
*)&p, &u);

and it seems to do the right thing.

Regards,
Will
 
William DePalo said:
I see. As I said, I've always used GetFileVersionInfo...() functions.

But I just tried this quick and awful hack

HRSRC hrsrc = FindResource(hInst, VS_VERSION_INFO, VERSIONINFO);
HGLOBAL hglbl = LoadResource(hInst, hrsrc);
LPVOID pv = LockResource(hglbl);

UINT u;
LPCSTR p;
VerQueryValue(pv, "\\StringFileInfo\\040904B0\\FileDescription",
(LPVOID *)&p, &u);

Thanks William! I ran your sample, and ran into a problem or two. Finally
got it to work but I think you're right - reading the resources from the
file is probably my best bet right now :) Now I'm running into another
problem with a possible memory leak... will the fun never end?
 
Mike C# said:
Thanks William!

You are welcome.
I ran your sample, and ran into a problem or two.

It was an awful hack. I did work here. Just by the way, the IDE assigns an
ID of 1 (VS_VERSION_INFO) to the resource. I'm not sure if that is a
convention or a requirement.
Now I'm running into another problem with a possible memory leak... will
the fun never end?

:-)

Regards,
Will
 
William DePalo said:
It was an awful hack. I did work here. Just by the way, the IDE assigns an
ID of 1 (VS_VERSION_INFO) to the resource. I'm not sure if that is a
convention or a requirement.

Yeah to get it to work here I had to use a string identifier "#1". It did
work, but I have more confidence in your original suggestion :) Thanks!
 
Back
Top