如何在Windows中使用C从文件句柄中获取文件名?

cs7cruho  于 2023-08-07  发布在  Windows
关注(0)|答案(6)|浏览(140)

我正试图从给定的文件句柄中检索文件名。
我已经看到GetFileInformationByHandle可能很有用,但它返回的结构不包含任何文件名信息(http://msdn.microsoft.com/en-us/library/aa363788%28v=VS.85%29.aspx)。
我该怎么办?
编辑:
我已经尝试安装Windows FileID API以使GetFileInformationByHandleEx在Windows XP上工作。但是当包含fileextd. h时,我得到以下错误

c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(16) : error C2011: '_FILE_INFO_BY_HANDLE_CLASS' : 'enum' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13900) : see declaration of '_FILE_INFO_BY_HANDLE_CLASS'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(32) : error C2011: '_FILE_BASIC_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13917) : see declaration of '_FILE_BASIC_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(40) : error C2011: '_FILE_STANDARD_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13925) : see declaration of '_FILE_STANDARD_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(48) : error C2011: '_FILE_NAME_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13933) : see declaration of '_FILE_NAME_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(53) : error C2011: '_FILE_RENAME_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13938) : see declaration of '_FILE_RENAME_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(60) : error C2011: '_FILE_ALLOCATION_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13945) : see declaration of '_FILE_ALLOCATION_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(64) : error C2011: '_FILE_END_OF_FILE_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13949) : see declaration of '_FILE_END_OF_FILE_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(68) : error C2011: '_FILE_STREAM_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13953) : see declaration of '_FILE_STREAM_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(76) : error C2011: '_FILE_COMPRESSION_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13961) : see declaration of '_FILE_COMPRESSION_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(85) : error C2011: '_FILE_ATTRIBUTE_TAG_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13970) : see declaration of '_FILE_ATTRIBUTE_TAG_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(90) : error C2011: '_FILE_DISPOSITION_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13975) : see declaration of '_FILE_DISPOSITION_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(94) : error C2011: '_FILE_ID_BOTH_DIR_INFO' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(13979) : see declaration of '_FILE_ID_BOTH_DIR_INFO'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(130) : error C2011: '_FILE_ID_TYPE' : 'enum' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(14026) : see declaration of '_FILE_ID_TYPE'
c:\programmi\microsoft visual studio 9.0\vc\include\fileextd.h(135) : error C2011: 'FILE_ID_DESCRIPTOR' : 'struct' type redefinition
        c:\programmi\microsoft sdks\windows\v6.1\include\winbase.h(14032) : see declaration of 'FILE_ID_DESCRIPTOR'
c:\documents and settings\lab\documenti\visual studio 2008\projects\sandbox\sandbox\funcs_files.cpp(26) : error C2079: 'lpFileInformation' uses undefined struct '_FILE_NAME_INFO'
c:\documents and settings\lab\documenti\visual studio 2008\projects\sandbox\sandbox\funcs_files.cpp(35) : error C2228: left of '.FileName' must have class/struct/union
        type is 'int'

字符串
从下面的代码:

#include <windows.h>
#include <fileextd.h>

LPVOID GetFileNameFromHandle(HANDLE hFile) {
    FILE_NAME_INFO lpFileInformation;
    BOOL bWorked;

    bWorked = GetFileInformationByHandleEx(
        hFile,
        FileNameInfo,
        &lpFileInformation,
        sizeof(FILE_NAME_INFO));

    return lpFileInformation.FileName;
}

ehxuflar

ehxuflar1#

BOOL    GetFileNameFromHandle(HANDLE hFile, TCHAR *pszFileName, const unsigned int uiMaxLen)
{
    pszFileName[0]=0;

    std::unique_ptr<BYTE[]> ptrcFni(new BYTE[_MAX_PATH * sizeof(TCHAR) + sizeof(FILE_NAME_INFO)]);
    FILE_NAME_INFO *pFni = reinterpret_cast<FILE_NAME_INFO *>(ptrcFni.get());

    BOOL b = GetFileInformationByHandleEx(hFile, 
                                        FileNameInfo,
                                        pFni,
                                        sizeof(FILE_NAME_INFO) + (_MAX_PATH * sizeof(TCHAR)) );
    if ( b )
    {
#ifdef  _UNICODE
         wcsncpy_s(pszFileName, 
                min(uiMaxLen, (pFni->FileNameLength / sizeof(pFni->FileName[0])) + 1 ), 
                pFni->FileName, 
                _TRUNCATE);
#else
        strncpy_s(pszFileName, 
                min(uiMaxLen, (pFni->FileNameLength / sizeof(pFni->FileName[0])) + 1), 
                CW2A(pFni->FileName), 
                _TRUNCATE);
#endif
    }
    return b;
}

字符串

anauzrmj

anauzrmj2#

嗨,如果你需要文件名从句柄,你可以阅读这篇文章http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx或使用这个GetFileInformationByHandleEx

9rbhqvlz

9rbhqvlz3#

对于Vista和更高版本,请看GetFinalPathNameByHandle(如mehrdad所写)
它比GetFileInformationByHandleEx更方便,允许更多的定制,并且消除了分配自定义大小FILE_NAME_INFO结构的麻烦。
范例:

DWORD size = GetFinalPathNameByHandleW(handle, NULL, 0, VOLUME_NAME_DOS);
if (size == 0) return L"(NOT FOUND)";
std::wstring fname(size, L'0');
size = GetFinalPathNameByHandleW(handle, &fname.front(), size, VOLUME_NAME_DOS);

字符串

注意,它会在返回的名称前面加上\\?\

(我使用C++ std::wstring来避免C的内存处理样板。根据需要使用malloc)。

zfciruhq

zfciruhq4#

有一个正确的方法来做到这一点,在Windows XP上的作品,对文件和目录;我在这里的另一篇文章中解释过。

tjrkku2a

tjrkku2a5#

你从哪里得到的文件句柄?如果确定它不是命名管道句柄,可以使用NtQueryObject查询文件名。

prdp8dxp

prdp8dxp6#

我喜欢Sirotnikov的方法。但是,我们只使用UTF-8(在main的开头调用setlocale(LC_CTYPE, ".utf8");)。这需要使用GetFinalPathNameByHandleA,其中初始调用不包括用于终止空字符的空格。然后需要为该字符串分配一个更大的字符串。以下是我的电话:

DWORD size = GetFinalPathNameByHandleA((HANDLE)_get_osfhandle(_fileno(f)), nullptr, 0, FILE_NAME_OPENED);
if (size > 0)
{
    std::string fname(size+1, '\0');
    size = GetFinalPathNameByHandleA((HANDLE)_get_osfhandle(_fileno(f)), &fname.front(), size+1, FILE_NAME_OPENED);
    return fname;
}

字符串
还有一些额外的技巧,因为我需要从普通的C FILE*指针中获取文件名。(有关信息,请参阅https://stackoverflow.com/a/6064796/6954968。)由于_get_osfhandl(),除了Windows.h之外,还需要头io.h

相关问题