Click here to Skip to main content
15,900,258 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: File Path from handle Pin
ThatsAlok11-Nov-08 20:57
ThatsAlok11-Nov-08 20:57 
AnswerRe: File Path from handle Pin
Randor 11-Nov-08 22:47
professional Randor 11-Nov-08 22:47 
GeneralRe: File Path from handle Pin
revanth198511-Nov-08 23:17
revanth198511-Nov-08 23:17 
GeneralRe: File Path from handle Pin
Randor 11-Nov-08 23:31
professional Randor 11-Nov-08 23:31 
GeneralRe: File Path from handle Pin
revanth198511-Nov-08 23:43
revanth198511-Nov-08 23:43 
GeneralRe: File Path from handle Pin
SRKSHOME12-Nov-08 0:47
SRKSHOME12-Nov-08 0:47 
QuestionRe: File Path from handle Pin
David Crow12-Nov-08 3:15
David Crow12-Nov-08 3:15 
GeneralRe: File Path from handle Pin
Randor 12-Nov-08 1:21
professional Randor 12-Nov-08 1:21 
If you are asking how to find the filename/path from a FILE handle then perhaps the following code will help. Unfortunately it involves usage of some advanced undocumented functions. The following code is for Windows 2000 and above and completely unsupported.

typedef LONG NTSTATUS;
#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0 && Status < 0x3FFFFFFF)


typedef struct _IO_STATUS_BLOCK {
    union {
        NTSTATUS Status;
        PVOID Pointer;
    };
    ULONG_PTR Information;
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;


typedef struct _UNICODE_STRING
{
	USHORT Length;
	USHORT MaximumLength;
	PWSTR Buffer;
} UNICODE_STRING, *PUNICODE_STRING;


typedef enum _FILE_INFORMATION_CLASS
{
	FileNameInformation=9,            
} FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS;


typedef struct _FILE_NAME_INFORMATION
{
	ULONG FileNameLength;
	WCHAR FileName[MAX_PATH];
} FILE_NAME_INFORMATION, *PFILE_NAME_INFORMATION;


NTSTATUS (NTAPI *NtQueryInformationFile)(HANDLE,PIO_STATUS_BLOCK,PVOID,LONG,FILE_INFORMATION_CLASS);


CString GetFileNameFromHandle(HANDLE hFile)
{
	wchar_t wszFilename[MAX_PATH];
	IO_STATUS_BLOCK stb;
	FILE_NAME_INFORMATION str;
	*(FARPROC*)&NtQueryInformationFile = GetProcAddress(GetModuleHandle(_T("ntdll.dll")),"NtQueryInformationFile");
	DWORD Status=NtQueryInformationFile(hFile,&stb,&str,MAX_PATH,FileNameInformation);
	return NT_SUCCESS(Status)?CString(str.FileName, str.FileNameLength / sizeof(WCHAR)):NULL;
}



Sample MFC usage:


CFile f;
f.Open(_T("C:\\Test.txt"),CFile::modeRead,0);
HANDLE hFile = f.m_hFile;
MessageBox(GetFileNameFromHandle(hFile));
f.Close();



Best Wishes,
-David Delaune
QuestionCPU stimulation C++ Pin
124211-Nov-08 18:46
124211-Nov-08 18:46 
AnswerRe: CPU stimulation C++ Pin
David Crow12-Nov-08 3:35
David Crow12-Nov-08 3:35 
GeneralRe: CPU stimulation C++ Pin
124212-Nov-08 6:45
124212-Nov-08 6:45 
GeneralRe: CPU stimulation C++ Pin
124212-Nov-08 6:54
124212-Nov-08 6:54 
QuestionProblem with IDispatch::GetTypeInfo Pin
mvkvibin11-Nov-08 17:44
mvkvibin11-Nov-08 17:44 
QuestionConvert doc/txt file to PDF using C++. Pin
Subrat 470826611-Nov-08 17:26
Subrat 470826611-Nov-08 17:26 
AnswerRe: Convert doc/txt file to PDF using C++. Pin
bob1697211-Nov-08 18:32
bob1697211-Nov-08 18:32 
GeneralRe: Convert doc/txt file to PDF using C++. Pin
Saurabh.Garg11-Nov-08 22:41
Saurabh.Garg11-Nov-08 22:41 
GeneralRe: Convert doc/txt file to PDF using C++. Pin
bob1697212-Nov-08 3:18
bob1697212-Nov-08 3:18 
AnswerRe: Convert doc/txt file to PDF using C++. Pin
Cedric Moonen11-Nov-08 20:32
Cedric Moonen11-Nov-08 20:32 
QuestionHow to pass data from MFC to Win 32 Console through windows messaging [modified] Pin
Darrel Q Pham11-Nov-08 15:45
Darrel Q Pham11-Nov-08 15:45 
AnswerRe: How to pass data from MFC to Win 32 Console through windows messaging Pin
Darrel Q Pham17-Nov-08 15:34
Darrel Q Pham17-Nov-08 15:34 
QuestionNeed help converting raw PCM data into streaming audio Pin
lavagoblin11-Nov-08 12:42
lavagoblin11-Nov-08 12:42 
AnswerRe: Need help converting raw PCM data into streaming audio Pin
enhzflep11-Nov-08 14:00
enhzflep11-Nov-08 14:00 
AnswerRe: Need help converting raw PCM data into streaming audio Pin
Mark Salsbery11-Nov-08 15:35
Mark Salsbery11-Nov-08 15:35 
QuestionPull Downs Menu Items Pin
BobInNJ11-Nov-08 11:33
BobInNJ11-Nov-08 11:33 
AnswerRe: Pull Downs Menu Items Pin
lavagoblin11-Nov-08 11:59
lavagoblin11-Nov-08 11:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.