Solved

Find actual path of program (EXE)

Posted on 1998-11-29
8
221 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

747 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

12 Experts available now in Live!

Get 1:1 Help Now