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

如何从Windows中使用C的文件句柄获取文件的名称?,第1张

概述如何从Windows中使用C的文件句柄获取文件的名称?

我试图从给定的文件句柄检索文件名。

我已经看到, GetfileinformationByHandle可能是有用的,但它返回的结构不包含任何文件名信息( http://msdn.microsoft.com/en-us/library/aa363788%28v=VS.85%29.aspx )。

我怎样才能做到这一点?

编辑:

在windows中从文件句柄获取文件path

处理结束后`/ dev / ttyS *`上的QSerialPort效应?

windows:subprocess制作新的控制台窗口,丢失stdin / out

当进程被终止时,放弃打开的文件

在windows上确定打开的文件句柄模式

我已经尝试安装windows fileID API来获取GetfileinformationByHandleEx在windows XP上工作。 但是,当包含fileextd.h我得到以下错误

c:programmimicrosoft visual studio 9.0vcincludefileextd.h(16) : error C2011: '_file_INFO_BY_HANDLE_CLASS' : 'enum' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13900) : see declaration of '_file_INFO_BY_HANDLE_CLASS' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(32) : error C2011: '_file_BASIC_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13917) : see declaration of '_file_BASIC_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(40) : error C2011: '_file_STANDARD_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13925) : see declaration of '_file_STANDARD_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(48) : error C2011: '_file_name_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13933) : see declaration of '_file_name_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(53) : error C2011: '_file_REname_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13938) : see declaration of '_file_REname_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(60) : error C2011: '_file_ALLOCATION_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13945) : see declaration of '_file_ALLOCATION_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(64) : error C2011: '_file_END_OF_file_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13949) : see declaration of '_file_END_OF_file_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(68) : error C2011: '_file_STREAM_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13953) : see declaration of '_file_STREAM_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(76) : error C2011: '_file_COMPRESSION_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13961) : see declaration of '_file_COMPRESSION_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(85) : error C2011: '_file_ATTRIBUTE_TAG_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13970) : see declaration of '_file_ATTRIBUTE_TAG_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(90) : error C2011: '_file_disposition_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13975) : see declaration of '_file_disposition_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(94) : error C2011: '_file_ID_BOTH_DIR_INFO' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(13979) : see declaration of '_file_ID_BOTH_DIR_INFO' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(130) : error C2011: '_file_ID_TYPE' : 'enum' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(14026) : see declaration of '_file_ID_TYPE' c:programmimicrosoft visual studio 9.0vcincludefileextd.h(135) : error C2011: 'file_ID_DESCRIPTOR' : 'struct' type redeFinition c:programmimicrosoft sdkswindowsv6.1includewinbase.h(14032) : see declaration of 'file_ID_DESCRIPTOR' c:documents and settingslabdocumentivisual studio 2008projectssandBoxsandBoxfuncs_files.cpp(26) : error C2079: 'lpfileinformation' uses undefined struct '_file_name_INFO' c:documents and settingslabdocumentivisual studio 2008projectssandBoxsandBoxfuncs_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; }

linux可以在打开的文件句柄上使用inotify吗?

列出当前打开的文件句柄?

检查文件是否被进程文件句柄locking

程序文件句柄

为什么打开的DLL句柄不能保护文件不被移动?

嗨,如果你需要从处理文件的名称,你可以阅读这个MS文章http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx或使用GetfileinformationByHandleEx

BOol GetfilenameFromHandle(HANDLE hfile,TCHAR *pszfilename,const unsigned int uiMaxLen) { pszfilename[0]=0; std::unique_ptr<TCHAR> ptrcFni(new TCHAR[_MAX_PATH + sizeof(file_name_INFO)]); file_name_INFO *pFni = reinterpret_cast<file_name_INFO *>(ptrcFni.get()); BOol b = GetfileinformationByHandleEx(hfile,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,(pFni->filenameLength / sizeof(pFni->filename[0])) + 1),CW2A(pFni->filename),_TruncATE); #endif } return b; }

有一个正确的方法来做到这一点,在windows XP上,在文件和目录; 我在另一个帖子里解释过

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

对于Vista和后来看看GetFinalPathnameByHandle (就像mehrdad写的)

这是比较方便的国际海事组织,并允许更多的定制比GetfileinformationByHandleEx ,它消除了分配自定义大小file_name_INFO结构的麻烦。

例:

DWORD size = GetFinalPathnameByHandleW(handle,NulL,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来满足您的需求)。

总结

以上是内存溢出为你收集整理的如何从Windows中使用C的文件句柄获取文件的名称?全部内容,希望文章能够帮你解决如何从Windows中使用C的文件句柄获取文件的名称?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1285631.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存