Solved

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

Posted on 2003-10-25
7
949 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …

726 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