Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Find actual path of program (EXE)

Posted on 1998-11-29
8
Medium Priority
?
230 Views
Last Modified: 2010-04-15
I am trying to determine the path that the exe file was called from, i.e. the compiled C program may be in a directory different from the current one.
How do I find the EXE's true path? (sounds like enlightment for programs :)
Compiler is BC++ 3.1 for DOS.
0
Comment
Question by:idek1
  • 4
  • 2
  • 2
8 Comments
 

Author Comment

by:idek1
ID: 1254723
Edited text of question
0
 
LVL 8

Accepted Solution

by:
shlomoy earned 210 total points
ID: 1254724
Here is an answer I've seen on a newsgroup regarding...
I hope this helps you. if you're stuck - tell me - I'll explain.
So, here is the question (just as yours) and the proposed ansewr:


---------------------------------------------------------------------------------------------------

>I am having problems in getting the full path of the application
>executable when the application comes up.
>
>m_pszExeName of the CWinApp contains only the name of the executable,
>without extention or the full path.
>
>How can i get the above with the full executable name, or atleast the
>full path the directory which contains the executable.

You'll need to use the Windows API for this one...

    HMODULE hModule = ::GetModuleHandle(NULL);
    if (hModule == 0)
    {
      // error, couldn't get module handle, call ::GetLastError
    }
    else
    {
      char szExeFileName[_MAX_PATH];
      DWORD result = ::GetModuleFileName(hModule, szExeFileName, _MAX_PATH);
      if (result == 0)
      {
        // error retrieving name, call ::GetLastError
      }
    }

At this point, if you're using MFC, it's easy enough to convert the
character buffer into a CString, if that's what you want.
----------------------------------------------------------------------------------------------------------------------

0
 
LVL 8

Expert Comment

by:shlomoy
ID: 1254725
You can also get the full path from __argv[0].
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

by:shlomoy
ID: 1254726
also try:

char path[MAX_PATH];
        int len=GetModuleFileName(AfxGetInstanceHandle(),path,sizeof(path)-1);
        m_AppDir=CString(path, len);



0
 
LVL 11

Expert Comment

by:alexo
ID: 1254727
>> Compiler is BC++ 3.1 for DOS.
argv[0] holds full program name.
0
 
LVL 8

Expert Comment

by:shlomoy
ID: 1254728
I did mention argv[0], on one of my comments.
I supplied several ways to get the result needed.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1254729
try the following:

    #include <stdio.h>
    void main(int argc, char* argv[])
    {
        puts(argv[0]);
    }

0
 

Author Comment

by:idek1
ID: 1254730
THank you for your help. ARGV[0] is what I required. THe windows code is irrelevant in this case, as I am compiling for DOS only, and the program needs to run in DOS mode without any windows support.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question