473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Finding the executing C++ program location (win32)

yzz
Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Thanks

Apr 12 '07 #1
7 11330
On Apr 12, 1:48 pm, "yzz" <yza...@gmail.comwrote:
Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Thanks
Hi,

'CurrentDir' - is the function which will return the current directory
in which the program stands. Also there is one more method to find the
path, please refer K&R C for exact function names.

- vinod

Apr 12 '07 #2
En Thu, 12 Apr 2007 10:48:29 +0200, yzz <yz****@gmail.comescribió:
Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Thanks
Fist string array passed to main is the program name. Then you've to
substring to last '\' delimiter and you've got it.

int main(int argv,char *argc[]);

argc[0] -Is exe's path as null terminated strign.

--
Visita mi blog principal: http://rfog.blogsome.com
Y este sobre programación: http://geeks.ms/blogs/rfog
Libros, ciencia ficción y programación
========================================
Sólo en la filosofía es donde cada pensador, cuando es original, determina
no únicamente lo que quiere responder, sino lo que quiere preguntar...
para responder al concepto de filosofía.
-- Georg Simmel. (1858-1918) Filósofo alemán.
Apr 12 '07 #3
On 12 Apr, 11:25, "Zephryn Xirdal" <zephryn.xir...@quitame.gmail.com>
wrote:
En Thu, 12 Apr 2007 10:48:29 +0200, yzz <yza...@gmail.comescribió:
Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo
Thanks

Fist string array passed to main is the program name. Then you've to
substring to last '\' delimiter and you've got it.

int main(int argv,char *argc[]);

argc[0] -Is exe's path as null terminated strign.
Yes, but there's no guarantee that the actual path is included, just
the name the file was called with.

--
Erik Wikström

Apr 12 '07 #4
En Thu, 12 Apr 2007 11:48:00 +0200, Erik Wikström
<er****@student.chalmers.seescribió:
On 12 Apr, 11:25, "Zephryn Xirdal" <zephryn.xir...@quitame.gmail.com>
wrote:
>En Thu, 12 Apr 2007 10:48:29 +0200, yzz <yza...@gmail.comescribió:
Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo
Thanks

Fist string array passed to main is the program name. Then you've to
substring to last '\' delimiter and you've got it.

int main(int argv,char *argc[]);

argc[0] -Is exe's path as null terminated strign.

Yes, but there's no guarantee that the actual path is included, just
the name the file was called with.

--
Erik Wikström
Then use GetCommandLine win32 API function. SDK says: "The operating
system may prepend a fully qualified path to an executable name that is
provided without a fully qualified path"

--
Visita mi blog principal: http://rfog.blogsome.com
Y este sobre programación: http://geeks.ms/blogs/rfog
Libros, ciencia ficción y programación
========================================
Sólo en la filosofía es donde cada pensador, cuando es original, determina
no únicamente lo que quiere responder, sino lo que quiere preguntar...
para responder al concepto de filosofía.
-- Georg Simmel. (1858-1918) Filósofo alemán.
Apr 12 '07 #5
yzz wrote:
Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Thanks
There is no C++ standard way to do this and so totally off topic here !

In win32, the best way is to use:

char l_name[ MAX_PATH + 1 ];
l_name[ sizeof( l_name ) -1] = 0;
::GetModuleFileName( NULL, l_name, sizeof( l_name ) -1 );

In unix, the not 100% reliable but mostly good way is to take argv[0]
and if the name does not start with "/" then get the working directory
and slap it in front.
if ( argv[ 0 ][ 0 ] == '/' )
{
at::FilePath l_filepath( argv[ 0 ] );
return l_filepath.Head().StlString();
}

at::FilePath l_current_dir = at::CurrentDirectory();
return (
l_current_dir / std::string( argv[ 0 ] )
).Head().Clean().StlString();

(with the austria C++ file path stuff in the alpha
http://netcabletv.org/public_releases/)

Apr 12 '07 #6
Zephryn Xirdal wrote:
En Thu, 12 Apr 2007 11:48:00 +0200, Erik Wikström
<er****@student.chalmers.seescribió:
>On 12 Apr, 11:25, "Zephryn Xirdal" <zephryn.xir...@quitame.gmail.com>
wrote:
>>En Thu, 12 Apr 2007 10:48:29 +0200, yzz <yza...@gmail.comescribió:

Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Thanks

Fist string array passed to main is the program name. Then you've to
substring to last '\' delimiter and you've got it.

int main(int argv,char *argc[]);

argc[0] -Is exe's path as null terminated strign.

Yes, but there's no guarantee that the actual path is included, just
the name the file was called with.

--
Erik Wikström

Then use GetCommandLine win32 API function. SDK says: "The operating
system may prepend a fully qualified path to an executable name that is
provided without a fully qualified path"
"May" is different from "Must". You have no guarantee, again.

P.S. Me gusta tu blog

--
Regards
Miguel Giménez
Apr 12 '07 #7
yzz wrote:
Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo
Short answer: you can't in standard C++, so you are off topic.

A little bit longer answer: you can't in standard C++, however there are
some nice libraries called "boost", some of those will probably inserted
on the next C++ standard. One of them, that probably is not yet in but
is in the Stroustrup wish list ( not the first guy around :) ) is the
filesystem library. Look at:
http://www.boost.org/libs/filesystem...m#current_path

probably this is the most portable and close to the standard way to do
what you are asking.

Regards,

Zeppe
Apr 12 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Twan Kennis | last post by:
Hi, I have a DB2 database on the IBM iSeries platform, on which I created several Stored Procedures with the SQLCODE as a return-parameter. These Stored Procedures are called from a Windows...
0
by: Rahul Chatterjee | last post by:
Hello All I have designed a dotnet application using VB which basically takes a selection and passes value to a crystal report which in turn passes the value to a Stored procedure. After the...
5
by: Charles Law | last post by:
I want to display the name and version of all assemblies (dlls?) used/required by my application in a Help | About box. Is there a way to do this? One of the problems I anticipate is that...
0
by: NSF12345 | last post by:
Iv developed a small program that looks for a file over our network, and copy it to the location of another computer. Im using the "If FileExists("\\oldpc\main share\Folder\file.txt") Then" way of...
6
by: ulillillia | last post by:
I've been following the tutorial here to learn Windows programming (along with the help file from the Windows Platform SDK): http://www.cprogramming.com/tutorial/opengl_windows_programming.html ...
5
by: =?Utf-8?B?Qkw=?= | last post by:
Hello friends In c# 2005 I have written a function to access "font file name" by "Font name" and it is working fine in all the windows version other than vista, in vista it is throughing an...
3
by: somuchh8 | last post by:
Hi, I'm having a lot of trouble with the Win32::Spawn module in perl. Here is my situation, I have a Win32::Spawn call which looks like this: my $success = undef; my $cmdline =...
7
by: mark | last post by:
I am writing a program that will be placed in the startup folder so that on login it executes, it also needs to delete itself so it creates a batch file which cleans up. After that it restarts the...
3
by: jim3371 | last post by:
Using wxPython, I'm looking to build a GUI app for a daemon-based app, on Win32 platform, how would I go about executing the daemon app so it stays in the background when the Py app is running?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.