Solved

How to get the complete path to my file in my file.

Posted on 2003-10-25
7
946 Views
Last Modified: 2010-04-17
Hi experts,
I am working in vc++ 6 mfc and I need code to get the full path to my program from within my program. My program uses a registry key to start up when the computer starts. If the user installs my program to the default directory in my installer all is fine. The problem is when they install to a different directory the registry key points to the wrong path. I need a way to find the new path. I looked at the GetFilePath fuction but it doesn't take any parameters. How can it return the path if it doesn't
take any parameters? Vc++ help doesn't give much on it.

        Thanks,
         AJ.
0
Comment
Question by:arnold100
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:_nn_
ID: 9621175
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9621179
Just pass NULL as module handle.

<quote>
hModule
[in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process.
</quote>
0
 

Author Comment

by:arnold100
ID: 9622543
This works: lstrcpy( (char *) szFilePath ,LPCTSTR("c:\\Dir\\file.exe") );
But the path is known.

This doesn't: lstrcpy( (char *) szFilePath ,LPCTSTR(GetModuleFileName(NULL,file.exe,20)) );
Unknown path.
How do I make this return the full path? (c:\Dir\file.exe).

     Thanks,
      AJ.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:_nn_
ID: 9623089
Just use it directly :

  char szFilePath[MAX_PATH];
  GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));
0
 

Author Comment

by:arnold100
ID: 9623479
I have to use lstrcpy( (char *) szFilePath ,LPCTSTR("c:\\Dir\\file.exe") ); because I need unsigned char szFilePath; for RegSetValueEx.
I get errors when I try to use unsigned char in GetModuleFileName.
Any ideas?

        Thanks,
         AJ.
0
 
LVL 16

Accepted Solution

by:
_nn_ earned 200 total points
ID: 9623501
Well then, following should compile ok I think :

  char szModulePath[MAX_PATH];
  GetModuleFileName(NULL, szModulePath, sizeof(szModulePath));
  lstrcpy( (char *) szFilePath ,LPCTSTR(szModulePath) );
0
 

Author Comment

by:arnold100
ID: 9623532
Got it by copying a char in to unsigned char and using the value in RegSetValueEx.
Works perfect.

  Thank you very much for your help, Excellent,
     AJ.
 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article will show, step by step, how to integrate R code into a R Sweave document
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now