C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 29th, 2005, 08:36 AM
mage mage is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 8 mage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 27 sec
Reputation Power: 0
Send a message via ICQ to mage
Path to program's directory

hi,
can anyone tell me how can i get full path to directory, where is my program placed?

Thanks

Reply With Quote
  #2  
Old December 29th, 2005, 09:55 AM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,202 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 2 Days 23 h 26 m 58 sec
Reputation Power: 889
For which OS/Compiler?

You could look at argv[0], but it's contents depend on your shell, and can be easily spoofed if you manipulate say execl() calls.

Reply With Quote
  #3  
Old December 29th, 2005, 10:42 AM
Mareq's Avatar
Mareq Mareq is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: Bratislava
Posts: 186 Mareq User rank is Corporal (100 - 500 Reputation Level)Mareq User rank is Corporal (100 - 500 Reputation Level)Mareq User rank is Corporal (100 - 500 Reputation Level)Mareq User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 15 h 27 sec
Reputation Power: 7
Send a message via ICQ to Mareq
On DOS/Windows there are functions
Code:
getcwd, getdisk, getcurdir
(defined in dir.h and dos.h)

Code:
#include <stdio.h>
#include <dir.h>

int main(void)
{
   char buffer[MAXPATH];

   getcwd(buffer, MAXPATH);
   printf("The current directory is: %s\n", buffer);
   return 0;
}




On Linux platform try
Code:
man 3 getcwd
(defined in unistd.h)

Reply With Quote
  #4  
Old December 29th, 2005, 10:52 AM
JSClark JSClark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Amherst, MA, USA
Posts: 271 JSClark User rank is Sergeant (500 - 2000 Reputation Level)JSClark User rank is Sergeant (500 - 2000 Reputation Level)JSClark User rank is Sergeant (500 - 2000 Reputation Level)JSClark User rank is Sergeant (500 - 2000 Reputation Level)JSClark User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 5 h 49 m 46 sec
Reputation Power: 20
Is this for any Visual C / C++ Applications by any odd chance?
__________________
-Jason Clark

Reply With Quote
  #5  
Old December 29th, 2005, 11:01 AM
Mareq's Avatar
Mareq Mareq is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: Bratislava
Posts: 186 Mareq User rank is Corporal (100 - 500 Reputation Level)Mareq User rank is Corporal (100 - 500 Reputation Level)Mareq User rank is Corporal (100 - 500 Reputation Level)Mareq User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 15 h 27 sec
Reputation Power: 7
Send a message via ICQ to Mareq
Quote:
Originally Posted by JSClark
Is this for any Visual C / C++ Applications by any odd chance?


I do not have much experience with MSVS, but I think, my DOS/Windows example should work. You can find some information in MSDN. (Plz let me know, if it works...)

Reply With Quote
  #6  
Old December 29th, 2005, 12:15 PM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: May 2001
Location: Ft Myers, FL
Posts: 4,667 kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)  Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 4 Weeks 1 Day 18 h 20 m 54 sec
Reputation Power: 1409
Send a message via ICQ to kicken Send a message via AIM to kicken Send a message via MSN to kicken
Try looking over this page: http://autopackage.org/docs/binreloc/
See if that helps you any.
__________________
Spidermonkey Tutorial; Fight me: http://aoeex.mybrute.com
http://www.aoeex.com/gmap.php - Put yourself on the map (Now Updated!)


Reply With Quote
  #7  
Old December 29th, 2005, 12:15 PM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,202 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 2 Days 23 h 26 m 58 sec
Reputation Power: 889
Mareq, your answer only tells the current directory, not where the program is found. Most programs are found in one of the directories listed in the PATH variable.

Even then, you can add extra complexity to the problem by specifying a relative path to a program which isn't on the PATH, eg
..\foo\bar\prog.exe

Reply With Quote
  #8  
Old December 29th, 2005, 12:25 PM
Mareq's Avatar
Mareq Mareq is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: Bratislava
Posts: 186 Mareq User rank is Corporal (100 - 500 Reputation Level)Mareq User rank is Corporal (100 - 500 Reputation Level)Mareq User rank is Corporal (100 - 500 Reputation Level)Mareq User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 15 h 27 sec
Reputation Power: 7
Send a message via ICQ to Mareq
Quote:
Originally Posted by salem
Mareq, your answer only tells the current directory, not where the program is found. Most programs are found in one of the directories listed in the PATH variable.

Even then, you can add extra complexity to the problem by specifying a relative path to a program which isn't on the PATH, eg
..\foo\bar\prog.exe


Current path is at start the path of progam, but yes, you're right, if you explicitly change current directory... (Is this true? I'm not sure.)

Reply With Quote
  #9  
Old December 29th, 2005, 12:35 PM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: May 2001
Location: Ft Myers, FL
Posts: 4,667 kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)  Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 4 Weeks 1 Day 18 h 20 m 54 sec
Reputation Power: 1409
Send a message via ICQ to kicken Send a message via AIM to kicken Send a message via MSN to kicken
Quote:
Originally Posted by Mareq
Current path is at start the path of progam


That's not true. The current path at the start of execution is whatever path you were in. For instance say you open up a command prompt in XP and go to some directory (say your home dir, c:\documents and settings\yourname) to do something, then you execute notepad. notepad.exe is in C:\Windows, but the current path as returned by getcwd in notepad would be c:\documents and settings\yourname.
Comments on this post
Mareq agrees: What am I thinkink about? I completely forgot about this situation, sorry...

Reply With Quote
  #10  
Old December 29th, 2005, 06:18 PM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: Israel
Posts: 217 Shadow Wizard User rank is Sergeant (500 - 2000 Reputation Level)Shadow Wizard User rank is Sergeant (500 - 2000 Reputation Level)Shadow Wizard User rank is Sergeant (500 - 2000 Reputation Level)Shadow Wizard User rank is Sergeant (500 - 2000 Reputation Level)Shadow Wizard User rank is Sergeant (500 - 2000 Reputation Level)  Folding Points: 576413 Folding Title: Super Ultimate Folder - Level 2Folding Points: 576413 Folding Title: Super Ultimate Folder - Level 2Folding Points: 576413 Folding Title: Super Ultimate Folder - Level 2Folding Points: 576413 Folding Title: Super Ultimate Folder - Level 2Folding Points: 576413 Folding Title: Super Ultimate Folder - Level 2Folding Points: 576413 Folding Title: Super Ultimate Folder - Level 2Folding Points: 576413 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 1 Day 11 h 56 m 39 sec
Reputation Power: 16
I use such code to get directory of my exe file:
C++ Code:
Original - C++ Code
  1.  
  2.     //get the instance of the application
  3.     HINSTANCE hInstance = AfxGetInstanceHandle ();
  4.  
  5.     //with the instance finf the full path and name of the application
  6.     char file[MAX_STRING_SIZE];
  7.     GetModuleFileName(hInstance, file, MAX_STRING_SIZE);
  8.  
  9.     //convert string to c++ style
  10.     CString str(file);
  11.  
  12.     //chop the tile of the string (the file name)
  13.     int pos = str.ReverseFind('\\');
  14.     return(str.Mid(0,pos));

along the way, there is the full path.

Reply With Quote
  #11  
Old December 30th, 2005, 06:29 AM
mage mage is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 8 mage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 27 sec
Reputation Power: 0
Send a message via ICQ to mage
thanks, it works on Windows, but I need it also on Linux ...

Reply With Quote
  #12  
Old December 30th, 2005, 09:08 AM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: May 2001
Location: Ft Myers, FL
Posts: 4,667 kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)kicken User rank is General 6th Grade (Above 100000 Reputation Level)  Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1Folding Points: 221067 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 4 Weeks 1 Day 18 h 20 m 54 sec
Reputation Power: 1409
Send a message via ICQ to kicken Send a message via AIM to kicken Send a message via MSN to kicken
Look into the binreloc package I linked to above. It should do what you want.

Reply With Quote
  #13  
Old December 30th, 2005, 09:14 AM
mage mage is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 8 mage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 27 sec
Reputation Power: 0
Send a message via ICQ to mage
I found the way how to get the path on Linux - it's in /proc/self/maps. So i wrote this function, which gets the path from this file:
Code:
        char *wd;
        wd=new char[100]; //alocate the memory
	FILE *f;
	char tmp[200];
	char *tmp2,*tmp3;
	f=fopen("/proc/self/maps","r"); //that file contains path to my aplication
	fgets(tmp,200,f); //read the first line
	fclose (f);
	tmp[strlen(tmp)-1]='\0'; //remove newline character
	tmp2=strchr(tmp,'/'); //find first slash
	tmp3=strrchr(tmp,'/'); //find the last slash
	*tmp3='\0'; //tarminating character replaces last slash of path
        sprintf(wd,"%s",tmp2); //copy path on the other place in memory
        return (wd);


Anyway, thasks for your help

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Path to program's directory


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek