Classic Win32 code from the file handle to the file name

zhaozj2021-02-16  60

#include

#include

#include

#include

#include

#define buffsize 512

Bool getFileNameFromHandle (Handle Hfile)

{

Bool bsuccess = false;

Tchar PSZFileName [MAX_PATH 1];

// Get the file size.

DWORD DWFILESIZEHI = 0;

DWORD DWFILESZELO = GetFileSize (Hfile, & DwFileSizehi);

// CREATE A File Mapping Object.

Handle HfileMap = CreateFilemapping (Hfile,

NULL,

Page_readonly,

0,

Dwfilesizelo,

NULL);

IF (HFILEMAP)

{

// CREATE A File Mapping to get the file name.

Void * Pmem = MapViewOffile (HFilemap, File_Map_read, 0, 0, 1);

IF (PMEM)

{

IF (getCurrentProcess (),

PMEM,

PSZFileName,

MAX_PATH))))

{

// Translate path with device name to drive letters.

Tchar sztemp [buffsize];

SzTemp [0] = '/ 0';

IF (getLogicalDriveStrings (bufsize-1, sztemp))

{

Tchar szname [MAX_PATH];

TCHAR SZDRIVE [3] = text (":");

Bool bfound = false;

TCHAR * P = SzTemp;

DO

{

// Copy The Drive Letter to the Template String

* szdrive = * p;

// Look Up Each Device Name

QueryDosDevice (szdrive, szname, bufsize)

{

Uint unamelen = _tcslen (szname);

IF (unamelen

{

BFOUND = _TCSNICMP (pszfilename, szname,

unamelen == 0;

IF (bFound)

{

// Reconstruct PSZFileName Using SzTemp

// Replace Device Path with dos pat

Tchar sztempfile [MAX_PATH];

_stprintf (sztempfile,

Text ("% s% s"),

Szdrive,

PSZFileName unamelen;

_TCSNCPY (PSZFileName, SztempFile, Max_Path);

}

}

}

// go to the next null character.

While (* p );

} While (! bfound && * p); // end of string

}

}

Bsuccess = true;

UnmapViewoffile (PMEM);

}

CloseHandle (HFILEMAP);

Printf ("File Name IS% S / N", PSZFileName);

Return (BSuccess);

}

转载请注明原文地址:https://www.9cbs.com/read-19711.html

New Post(0)