CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Windows SDK Application: How to get the application directory?

    Q: How to get the application directory?

    A:

    Code:
    // With STL string
    #include <string>
    
    char        szAppPath[MAX_PATH] = "";
    std::string strAppDirectory;
    
    ::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);
    
    // Extract directory
    strAppDirectory = szAppPath;
    strAppDirectory = strAppDirectory.substr(0, strAppDirectory.rfind("\\"));
    
    
    
    // With CString
    char    szAppPath[MAX_PATH] = "";
    CString strAppDirectory;
    
    ::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);
    
    // Extract directory
    strAppDirectory = szAppPath;
    strAppDirectory = strAppDirectory.Left(strAppDirectory.ReverseFind('\\'));
    
    
    
    // With standard string
    char szAppPath[MAX_PATH]      = "";
    char szAppDirectory[MAX_PATH] = "";
    
    ::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);
    
    // Extract directory
    strncpy(szAppDirectory, szAppPath, strrchr(szAppPath, '\\') - szAppPath);
    szAppDirectory[strlen(szAppDirectory)] = '\0';
    Last edited by Andreas Masur; July 24th, 2005 at 04:54 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured