Solved

Find actual path of program (EXE)

Posted on 1998-11-29
8
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 

Author Comment

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

Accepted Solution

by:
shlomoy earned 70 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
Industry Leaders: 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!

 
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

Industry Leaders: 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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

739 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