Solved

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

Posted on 2003-10-25
7
947 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SUM 2 INTEGER ARRAYS INTO 1 10 101
ESXCLI upgrade command 1 100
Change the background and font colors in Notepad++ 5 90
linux - yum package installation issue 2 16
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

832 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