C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

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
Contributed User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2005
Posts: 2,768 salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 4 Weeks 18 h 20 m 6 sec
Reputation Power: 1237
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 8 m 51 sec
Reputation Power: 9
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: Massachusetts
Posts: 278 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 7 h 56 m 55 sec
Reputation Power: 22
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 8 m 51 sec
Reputation Power: 9
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.
Click here for more information. Click here for more information
 
Join Date: May 2001
Location: Bonita Springs, FL
Posts: 5,550 kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)  Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 2 Months 2 Weeks 14 h 30 m 52 sec
Reputation Power: 3285
Try looking over this page: http://autopackage.org/docs/binreloc/
See if that helps you any.
__________________
Recycle your old CD's, don't just trash them


Spidermonkey Tutorial;

If I helped out out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

Reply With Quote
  #7  
Old December 29th, 2005, 12:15 PM
salem's Avatar
salem salem is offline
Contributed User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2005
Posts: 2,768 salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)salem User rank is General 4th Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 4 Weeks 18 h 20 m 6 sec
Reputation Power: 1237
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 8 m 51 sec
Reputation Power: 9
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.
Click here for more information. Click here for more information
 
Join Date: May 2001
Location: Bonita Springs, FL
Posts: 5,550 kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)  Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 2 Months 2 Weeks 14 h 30 m 52 sec
Reputation Power: 3285
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: 2454113 Folding Title: Super Ultimate Folder - Level 5Folding Points: 2454113 Folding Title: Super Ultimate Folder - Level 5Folding Points: 2454113 Folding Title: Super Ultimate Folder - Level 5Folding Points: 2454113 Folding Title: Super Ultimate Folder - Level 5Folding Points: 2454113 Folding Title: Super Ultimate Folder - Level 5Folding Points: 2454113 Folding Title: Super Ultimate Folder - Level 5Folding Points: 2454113 Folding Title: Super Ultimate Folder - Level 5Folding Points: 2454113 Folding Title: Super Ultimate Folder - Level 5Folding Points: 2454113 Folding Title: Super Ultimate Folder - Level 5Folding Points: 2454113 Folding Title: Super Ultimate Folder - Level 5
Time spent in forums: 1 Day 11 h 56 m 39 sec
Reputation Power: 18
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.
Click here for more information. Click here for more information
 
Join Date: May 2001
Location: Bonita Springs, FL
Posts: 5,550 kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)kicken User rank is General 35th Grade (Above 100000 Reputation Level)  Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1460498 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 2 Months 2 Weeks 14 h 30 m 52 sec
Reputation Power: 3285
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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap