🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #6775

Re: Retrieving the Start from directory

#6775
From: Remy Lebeau (TeamB)
Date: Wed, 20 Apr 2005 21:08
44 lines
2583 bytes
"Marc" <bukwheat_NS@axess.com> wrote in message
news:4266bf60@newsgroups.borland.com...

> I am having an issue with retrieving the directory from
> where my application is started from, let me explain...

That is because you are making assumptions that you should not be making.

> GetModuleFileName and GetCurrentDirectory aren't cutting it for
> me.

GetModuleFileName() works fine, you just have to strip off the FileName
portion after calling it.  The TApplication::ExeName property uses
GetModuleFileName().  You can use the VCL's ExtractFilePath() function to
remove the filename from the returned value:

    AnsiString folder = ExtractFilePath(Application->ExeName);

Or, if you don't want to use the VCL, then just place a '\0' character after
the last '\\' character in the string buffer that you pass to
GetModuleFileName():

    char folder[MAX_PATH+1] = {0};

    DWORD dwCount = GetModuleFileName(NULL, folder, MAX_PATH);
    for(DWORD dw = dwCount; dw > 0; --dw)
    {
        if( folder[dw-1] == '\\' )
        {
            folder[dw] = '\0';
            break;
    }

As for GetCurrentDirectory(), that returns the calling process's current
working directory, which can (and usually does) change dynamically (even
multiple times) during the process's lifetime.  You should not rely on the
current working directory at all to determine the folder the application is
executed from.  You cannot even rely on it as the first call in WinMain(),
as the current working directory can be set to a different folder by a
shortcut that is launching the application, before WinMain() is even called.


Gambit
Technical Details
Message-ID: <4266c43b@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!newsread.com!news-xfer.newsread.com!nntp.abs.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4266bf60@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,d14a13f082d61e40
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4266bf60@newsgroups.borland.com>
Subject: Re: Retrieving the Start from directory
Date: Wed, 20 Apr 2005 14:08:09 -0700
Lines: 46
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <4266c43b@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1114031163 68.69.17.41 (20 Apr 2005 14:06:03 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!newsread.com!news-xfer.newsread.com!nntp.abs.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail