Solved

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

Posted on 2003-10-25
7
948 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

860 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