Get all the files specified in the .exe directory without knowing the location

I guess that my question is: how do I get the exe location directory as LPCWSTR so that I can enter it in my code

#include <iostream>
#include <Windows.h>
int main(int argc, char **argv)
{
WIN32_FIND_DATA a;
HANDLE swap = FindFirstFile(/*(LPCWSTR)__exe_directory__*/,&a);
if (swap!=INVALID_HANDLE_VALUE)
{
    do
    {
        char *sptn = new char [lstrlen(a.cFileName)+1];
        for (int c=0;c<lstrlen(a.cFileName);c++)
        {
            sptn[c]=char(a.cFileName[c]);
        }
        sptn[lstrlen(a.cFileName)]='\0';
        std::cout<<sptn<<std::endl;
    }
    while (FindNextFile(swap,&a));
}
else std::cout<<"undetected file\n";
FindClose(swap);
system("pause");
}

And it will return the listed files to the directory without errors. I know that my code already works with the directory, I already tested it.

+4
source share
2 answers

The key is to use (passing as a module descriptor to access the current EXE process) and then call (or if you do not need versions of Windows prior to Windows 8) to remove the spec file from the path. GetModuleFileName()nullptr PathRemoveFileSpec()PathCchRemoveFileSpec()

PathRemoveFileSpec(), Shlwapi.lib, MSDN.

. :

#include <iostream>     // For console output
#include <exception>    // For std::exception
#include <stdexcept>    // For std::runtime_error
#include <string>       // For std::wstring
#include <Windows.h>    // For Win32 SDK
#include <Shlwapi.h>    // For PathRemoveFileSpec()

#pragma comment(lib, "Shlwapi.lib")

// Represents an error in a call to a Win32 API.
class win32_error : public std::runtime_error 
{
public:
    win32_error(const char * msg, DWORD error) 
        : std::runtime_error(msg)
        , _error(error)
    { }

    DWORD error() const 
    {
        return _error;
    }

private:
    DWORD _error;
};

// Returns the path without the filename for current process EXE.
std::wstring GetPathOfExe() 
{
    // Get filename with full path for current process EXE
    wchar_t filename[MAX_PATH];
    DWORD result = ::GetModuleFileName(
        nullptr,    // retrieve path of current process .EXE
        filename,
        _countof(filename)
    );
    if (result == 0) 
    {
        // Error
        const DWORD error = ::GetLastError();
        throw win32_error("Error in getting module filename.", 
                          error);
    }

    // Remove the file spec from the full path
    ::PathRemoveFileSpec(filename);

    return filename;
}

int main() 
{
    try 
    {
        std::wcout << "Path for current EXE:\n"
                   << GetPathOfExe() 
                   << std::endl;
    } 
    catch (const win32_error & e) 
    {
        std::cerr << "\n*** ERROR: " << e.what()
                  << " (error code: " << e.error() << ")" 
                  << std::endl;
    } 
    catch (const std::exception& e) 
    {
        std::cerr << "\n*** ERROR: " << e.what() << std::endl;
    }
}

:

C:\Temp\CppTests>cl /EHsc /W4 /nologo /DUNICODE /D_UNICODE get_exe_path.cpp
get_exe_path.cpp

C:\Temp\CppTests>get_exe_path.exe
Path for current EXE:
C:\Temp\CppTests

PS
, , Unicode FindFirtFile() (.. FindFirstFileW(), LPCWSTR, const wchar_t*), ANSI/MBCS (.. char*).

Unicode UTF-16 wchar_t* Windows ++: , API Win32 Unicode.

, , ++, (, std::wstring Unicode UTF-16 Microsoft Visual ++) C- . API ( Win32 API C), std::wstring.

+5

GetModuleFileName UNICODE .

'\' 0.

.

#include <Windows.h>
#include <stdio.h>

int main( void ) {
    wchar_t szExeFullPath[ MAX_PATH ];
    if ( GetModuleFileName( NULL, szExeFullPath, _countof( szExeFullPath ) ) ) {
        wchar_t * pszLastAntiSlash = wcsrchr( szExeFullPath, L'\\' );
        if ( pszLastAntiSlash ) {
            *pszLastAntiSlash = 0;
            wprintf( L"Exe full path is %s\n", szExeFullPath );
        }
    }
    return 0;
}
+3

Source: https://habr.com/ru/post/1531512/


All Articles