相关文章推荐
私奔的山楂  ·  @echo off echo ...·  4 月前    · 

I am using GetFileInformationByHandleEx() to get a filename from an handle and I have issues getting the filename, below is a sample of the code that I am using and the output is

2 [C:\PMR] cfile.exe
File's HANDLE [foo.log] handle 36 size 528 MAX_PATH 260 sizeof 8 is OK!
File Name is [\] Length 24

I am using Visual Studio 10 on Windows 7 64bit

Thanks to help me to find what is wrong.

#include <windows.h>
#include <stdio.h>
#include <restartmanager.h>
int main(void)
HANDLE hFile;
FILE_NAME_INFO *FileInformation;
LPCTSTR fname = "foo.log";
int err = 0;
size_t size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR)*MAX_PATH;
hFile = CreateFile(fname, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL |FILE_ATTRIBUTE_ARCHIVE | SECURITY_IMPERSONATION, NULL);
if(hFile == INVALID_HANDLE_VALUE)
printf("Could not open %s file, error %d\n", fname, GetLastError());
printf("File's HANDLE [%s] handle %d size %d MAX_PATH %d sizeof %d is OK!\n", fname, hFile, size, MAX_PATH, sizeof(FILE_NAME_INFO));
FileInformation = (FILE_NAME_INFO *)(malloc (size));
FileInformation->FileNameLength = MAX_PATH;
if (!GetFileInformationByHandleEx(hFile, FileNameInfo, FileInformation, size))
err = GetLastError();
printf("GetFileInformationByHandleEx failed hFile %d FileName is [%s], err %d, %e\n", hFile, FileInformation->FileName, err, err);
printf("File Name is [%s] Length %d\n", FileInformation->FileName, FileInformation->FileNameLength);
CloseHandle(hFile);
DeleteFile(fname);
return 0;
Harassment is any behavior intended to disturb or upset a person or group of people. Threats include any threat of suicide, violence, or harm to another. Any content of an adult theme or inappropriate to a community web site. Any image, link, or discussion of nudity. Any behavior that is insulting, rude, vulgar, desecrating, or showing disrespect. Any behavior that appears to violate End user license agreements, including providing product keys or links to pirated software. Any image, link, or discussion related to child pornography, child nudity, or other child abuse or exploitation. Check with the programming experts and developers at MSDN which is the support venue.
MSDN - Forums
http://social.msdn.microsoft.com/Forums/en/categories/ MSDN - Where is the Forum For…?
http://social.msdn.microsoft.com/Forums/en/whatforum/threads
Hope this helps.

Rob Brown - Microsoft MVP <- profile - Windows Expert - Consumer : Bicycle <- Mark Twain said it right!

Rob Brown - past Microsoft MVP - Windows Insider MVP 2016 - 2021
Microsoft MVP Windows and Devices for IT 2009 - 2020
Harassment is any behavior intended to disturb or upset a person or group of people. Threats include any threat of suicide, violence, or harm to another. Any content of an adult theme or inappropriate to a community web site. Any image, link, or discussion of nudity. Any behavior that is insulting, rude, vulgar, desecrating, or showing disrespect. Any behavior that appears to violate End user license agreements, including providing product keys or links to pirated software. Any image, link, or discussion related to child pornography, child nudity, or other child abuse or exploitation. Harassment is any behavior intended to disturb or upset a person or group of people. Threats include any threat of suicide, violence, or harm to another. Any content of an adult theme or inappropriate to a community web site. Any image, link, or discussion of nudity. Any behavior that is insulting, rude, vulgar, desecrating, or showing disrespect. Any behavior that appears to violate End user license agreements, including providing product keys or links to pirated software. Any image, link, or discussion related to child pornography, child nudity, or other child abuse or exploitation. Yes that should help them help you.
Here are some more links to Visual Studio Support at MSDN.
MSDN - Visual Studio Forums
http://social.msdn.microsoft.com/Forums/en-US/category/visualstudio,vsarch,vsdbg,vstest,vstfs,vsdata,vsappdev,netdevelopment,visualbasic,visualcsharp,visualc,visualfsharp,vslightswitch,parallelcomputing
MSDN - Visual Studio Developer Center
http://msdn.microsoft.com/en-us/vstudio/default
MSDN Visual Studio Forums
http://social.msdn.microsoft.com/Forums/en-US/category/visualstudio
MSDN all Visual Studio and related Forums
http://social.msdn.microsoft.com/Forums/en-US/category/vsvnext,visualstudio,vsarch,vsdbg,vstest,vstfs,vsdata,vsappdev,visualbasic,visualcsharp,visualc,visualfsharp
Hope this helps.

Rob Brown - Microsoft MVP <- profile - Windows Expert - Consumer : Bicycle - Mark Twain said it right.

Rob Brown - past Microsoft MVP - Windows Insider MVP 2016 - 2021
Microsoft MVP Windows and Devices for IT 2009 - 2020
Harassment is any behavior intended to disturb or upset a person or group of people. Threats include any threat of suicide, violence, or harm to another. Any content of an adult theme or inappropriate to a community web site. Any image, link, or discussion of nudity. Any behavior that is insulting, rude, vulgar, desecrating, or showing disrespect. Any behavior that appears to violate End user license agreements, including providing product keys or links to pirated software. Any image, link, or discussion related to child pornography, child nudity, or other child abuse or exploitation.