获取.exe目录中列出的所有文件,不知道位置

use*_*146 4 c++ directory winapi

我想我的问题是这样的 - 如何将exe位置的目录作为LPCWSTR获取,以便我可以将其输入到我的代码中

#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");
}
Run Code Online (Sandbox Code Playgroud)

它会返回目录中列出的文件而不会出错.我知道我的代码已经在给定目录的情况下工作,我已经测试了它.

Mr.*_*C64 5

关键是使用GetModuleFileName()(nullptr作为模块句柄传递,引用当前进程EXE),然后调用PathRemoveFileSpec()(或者PathCchRemoveFileSpec(),如果您不关心Windows 8之前的Windows版本)从路径中删除文件规范.

要使用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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在控制台中:

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
Run Code Online (Sandbox Code Playgroud)

PS
在你的代码中,你似乎是指Unicode版本FindFirtFile()(即FindFirstFileW(),因为在你期望的评论中LPCWSTR,即const wchar_t*),但是在下面的代码中你使用ANSI/MBCS字符串(即char*).

我建议你在现代Windows C++代码中始终使用Unicode UTF-16 wchar_t*字符串:它更适合国际化,而现代Win32 API只带有Unicode版本.

另请注意,由于您使用的是C++,因此最好使用强大的方便的字符串类(例如std::wstring,使用Microsoft Visual C++的Unicode UTF-16字符串),而不是类似C的原始字符指针.使用API​​接口上的原始指针(因为Win32 API具有C接口),然后安全地转换为std::wstring.