Article View: borland.public.cppbuilder.nativeapi
Article #17587Re: How to get application's directory?
From: Rodolfo Frino - Stargate Softwar...
Date: Mon, 24 Mar 2003 00:30
Date: Mon, 24 Mar 2003 00:30
117 lines
4719 bytes
4719 bytes
> I know for a FACT that the Win3.1 components alter the current directory,
> because they all use the chdir() function internally, which alters the
> current working directory for the calling process. I know this because I
> looked at the source code for them. As for TOpenDialog, I also know it
can
> potentially change the current directory because it has an explicit
> "ofNoChangeDir" option specifically for the purpose of disabling that
> behavior.
That's OK.
This is your same example in which I added the following modifications:
1- CurrentDirectoryTest.cpp
----------------------------------
#include <vcl.h>
#pragma hdrstop
USERES("CurrentDirectoryTest.res");
USEFORM("CurrentDirectoryForm.cpp", Form1);
USEUNIT("CurrentDirectoryTest.cpp");
AnsiString ApplicationDirectory;
AnsiString GetApplicationDirectory()
{
const DWORD BUFF_SIZE = 256;
char Buffer[BUFF_SIZE+1];
GetCurrentDirectory(BUFF_SIZE, Buffer); //Win 32 API
return AnsiString(Buffer);
}
void Initialize()
{
ApplicationDirectory = GetApplicationDirectory();
ShowMessage(ApplicationDirectory); //Test line
Application->Initialize();
}
//--------------------------------------------------------------------------
-
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Initialize(); // See above function
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
//--------------------------------------------------------------------------
-
2. CurrentDirectoryForm.cpp
---------------------------------
#include <vcl.h>
#pragma hdrstop
#include "CurrentDirectoryForm.h"
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
extern AnsiString ApplicationDirectory; // Modification
//--------------------------------------------------------------------------
-
AnsiString __fastcall CurrentDir()
{
char buffer[MAX_PATH+1] = {0};
::GetCurrentDirectory(MAX_PATH+1, buffer);
return AnsiString(buffer);
}
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ShowMessage("Initial directory: " + CurrentDir());
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::DirectoryListBox1Change(TObject *Sender)
{
ShowMessage("New directory: " + CurrentDir());
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (OpenDialog1->Execute())
ShowMessage("New directory: " + CurrentDir());
// Modification
ShowMessage(ApplicationDirectory);
}
//--------------------------------------------------------------------------
-
The global variable ApplicationDirectory will get you the applications
directory,
even if you use TOpenDialog or any other Win3.1 component from the
component palette.
However, if, as Early mentioned, if Windows Explorer changes the
Current Directory internally when it launches the application (assuming we
use WE)
, the thing could be different. I'll have to investigate further :-)
Rodolfo
Technical Details
Message-ID:
<3e7e5a97@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailReferences:
<3e7a7800$1@newsgroups.borland.com> <3e7a8e39@newsgroups.borland.com> <3e7ae7af$1@newsgroups.borland.com> <3e7c7c8c@newsgroups.borland.com> <3e7cc472$1@newsgroups.borland.com> <3e7cd82a@newsgroups.borland.com> <3e7cfd40$1@newsgroups.borland.com> <3e7dba28@newsgroups.borland.com> <3e7e28c5$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,440cb76ba9be739c X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-03-23 22:56:29 PST From: "Rodolfo Frino - Stargate Software" <starcinema@eftel.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3e7a7800$1@newsgroups.borland.com> <3e7a8e39@newsgroups.borland.com> <3e7ae7af$1@newsgroups.borland.com> <3e7c7c8c@newsgroups.borland.com> <3e7cc472$1@newsgroups.borland.com> <3e7cd82a@newsgroups.borland.com> <3e7cfd40$1@newsgroups.borland.com> <3e7dba28@newsgroups.borland.com> <3e7e28c5$1@newsgroups.borland.com> Subject: Re: How to get application's directory? Date: Mon, 24 Mar 2003 11:30:23 +1100 Lines: 119 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 NNTP-Posting-Host: 203.129.132.54 Message-ID: <3e7e5a97@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1048468119 203.129.132.54 (23 Mar 2003 17:08:39 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail