Solved

Find actual path of program (EXE)

Posted on 1998-11-29
8
222 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 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
 
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

864 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now