473,396 Members | 1,724 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,396 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 11331
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?...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.