Re: [Dev-C++] How to get application directory?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Josep S. <sub...@ar...> - 2004-03-09 19:53:27
|
Hi Per,
>> strcpy(flag,"--datadir=");
>> getcwd(flag+strlen(flag),((size_t)size) - strlen(flag));
>> if (strlen(flag) < size-5)
>> strcat(flag,"\\data");
>> else
>> fail - too small buffer.
Thank you for streamlining the code! Your version is much more elegant than
my "beginner" one.
I will try now to use the following function, in order to assure that the
size with which the buffer char array is defined, is enough to contain a
path...
size = pathconf(".", _PC_PATH_MAX);
Thanks,
Josep
-----Ursprungliche Nachricht-----
Von: dev...@li...
[mailto:dev...@li...]Im Auftrag von Per
Westermark
Gesendet: Dienstag, 9. Marz 2004 19:35
An: Josep Subirana
Cc: DEV-C++
Betreff: Re: AW: [Dev-C++] How to get application directory?
Wrong use of getcwd - it doesn't allocate any memory. You have to supply
the memory it should use.
When you create a shortcut, you can specify the working directory, i.e.
where the program should be started.
getcwd() will - on startup of the program - always return this directory.
However, using getcwd() you may use multiple shortcuts that specifies
different working directories. getcwd() will then return different paths.
However, if a user has specified two different working directories, I
thinkt that is an expected behaviour.
/Per W
On Tue, 9 Mar 2004, Josep Subirana wrote:
> >>in Win32 I get
> >>the path to the running application like this:
> >>
> >>char szBuf[512];
> >>GetModuleFileName(NULL, szBuf, 512);
> >>
> >>(if the call succeeded, szBuf now contains the full path to the current
> >>module (executable))
>
> Hi Paul,
>
> Thanks alot for your hint! I'm a bit new to API and I don't know with c++
if
> I have to declare this function (GetModuleFileName()) in a header or what
I
> do have to do in order to being able to use this function you describe.
> Could you help me on that too? (If I need a function declaration in my
> header, could you please post it with the right variable types or point me
> to a website where i can find those API function declarations???)
>
> Thanks again,
>
> Josep
>
> BTW,
> I've tried with the following code, but I don't know if the *working
> directory* is always, at the beginning, the same as the directory where
the
> .exe file lies!!!
>
> const long size = 1000L;
> //char *buf;
char flag[size];
> //char *cwd;
> //size = pathconf(".", _PC_PATH_MAX);
>
strcpy(flag,"--datadir=");
getcwd(flag+strlen(flag),((size_t)size) - strlen(flag));
if (strlen(flag) < size-5)
strcat(flag,"\\data");
else
fail - too small buffer.
> //cwd = getcwd(buf, (size_t)size) ;
>
> //char flag[size] = "--datadir=";
> //char *subdir = "\\data";
> //char *foo = strcat (buf, subdir);
> //strcat(flag, foo);
>
> int argn = 3;
> char *arg[] = {
> argv[0], "c++", flag
> };
>
> mysql_server_init(argn, arg, (char **)server_groups);
>
> -----Ursprungliche Nachricht-----
> Von: dev...@li...
> [mailto:dev...@li...]Im Auftrag von P.D.
> van Zelst
> Gesendet: Dienstag, 9. Marz 2004 17:33
> Cc: dev...@li...
> Betreff: RE: [Dev-C++] How to get application directory?
>
>
> For the original poster: Don't know if it will help you, but in Win32 I
get
> the path to the running application like this:
>
> char szBuf[512];
> GetModuleFileName(NULL, szBuf, 512);
>
> (if the call succeeded, szBuf now contains the full path to the current
> module (executable))
>
> To Per:
> Thanks :D I forgot that lots of things are file (or pseudo-file) based in
> Linux. But it appears that /proc/self/exe is a symlink to the current
> binary... which brings me to a second problem: How do I get the target of
a
> symlink as a string or something :) Whenever I'm programming for Linux I
> constantly run into problems, cause I have no clue where to get MSDN-like
> information for Linux or POSIX.
>
> Regards,
>
>
> Paul
>
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of Per
> Westermark
> Sent: Tuesday, March 09, 2004 1:14 PM
> To: sub...@ar...
> Cc: dev...@li...
> Subject: Re: [Dev-C++] How to get application directory?
>
> If the application was started with a path, you might retrieve it as
> the arg[0] parameter of main().
>
> If not, you have to search the path for an application with the correct
> name. I don't think there is a standard function to perform this. If I'm
> wrong, I very much want to know the name of the function, since it would
> be useful :)
>
> On Linux, it is possible to heck in the /proc filesystem. There exists a
> symbolic link:
> /proc/<process_id>/exe -> /my/running/binary
> That will specify which binary that is running.
>
> It may also be accessed as:
> /proc/self/exe -> /my/running/binary
> to avoid the extra trouble of retrieving process id and dynamically create
> the path to the symbolic link.
>
> /Per W
>
> On Tue, 9 Mar 2004 sub...@ar... wrote:
>
> > What I actually wanted is the directory where the binary application is
> stored...
> > Anybody knows how to get it?
> >
> > Thanks again,
> > Josep
> >
> > ----- Original Nachricht ----
> > Von: Per Westermark <pw...@ia...>
> > An: Kevin Wood <ke...@me...>
> > Datum: 09.03.2004 10:28
> > Betreff: Re: [Dev-C++] How to get application directory?
> >
> > > The question was which directory the application is running in - not
> where
> > > the application binary is stored.
> > >
> > > /Per W
> > >
> > > On Tue, 9 Mar 2004, Kevin Wood wrote:
> > >
> > > > getcwd() returns the current working directory, which is not
> necessarily
> > > > the same as the directory in which the application resides.
> > > >
> > > > At 08:10 09/03/04, Per Westermark wrote:
> > > > >getcwd() or _getcwd().
> > > > >
> > > > >/Per W
> > > > >
> > > > >On Tue, 9 Mar 2004, Josep Subirana wrote:
> > > > >
> > > > > > Hi,
> > > > > >
> > > > > > Do you know how to get the directory where your c++ application
is
> > > running
> > > > > > in?
> > > > > > This should be a simple question for most experienced c++
> > > programmers!
> > > > > >
> > > > > > Thanks,
> > > > > >
> > > > > > Josep
> > > > > >
> > > >
> > > >
> > > >
> > > > -------------------------------------------------------
> > > > This SF.Net email is sponsored by: IBM Linux Tutorials
> > > > Free Linux tutorial presented by Daniel Robbins, President and CEO
of
> > > > GenToo technologies. Learn everything from fundamentals to system
> > > >
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
> > > > _______________________________________________
> > > > Dev-cpp-users mailing list
> > > > Dev...@li...
> > > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> > > >
> > >
> > >
> > >
> > > -------------------------------------------------------
> > > This SF.Net email is sponsored by: IBM Linux Tutorials
> > > Free Linux tutorial presented by Daniel Robbins, President and CEO of
> > > GenToo technologies. Learn everything from fundamentals to system
> > > administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
> > > _______________________________________________
> > > Dev-cpp-users mailing list
> > > Dev...@li...
> > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> > >
> >
> >
> >
> >
> >
> >
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: IBM Linux Tutorials
> Free Linux tutorial presented by Daniel Robbins, President and CEO of
> GenToo technologies. Learn everything from fundamentals to system
> administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: IBM Linux Tutorials
> Free Linux tutorial presented by Daniel Robbins, President and CEO of
> GenToo technologies. Learn everything from fundamentals to system
> administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: IBM Linux Tutorials
> Free Linux tutorial presented by Daniel Robbins, President and CEO of
> GenToo technologies. Learn everything from fundamentals to system
> administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|