Click here to Skip to main content
15,889,335 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Mozilla Firefox Pin
szcococut19-Jun-05 21:59
szcococut19-Jun-05 21:59 
GeneralListing files in a folder Pin
Tommy2d27-Feb-05 9:23
Tommy2d27-Feb-05 9:23 
GeneralRe: Listing files in a folder Pin
Ravi Bhavnani27-Feb-05 9:28
professionalRavi Bhavnani27-Feb-05 9:28 
GeneralRe: Listing files in a folder Pin
User 665827-Feb-05 9:41
User 665827-Feb-05 9:41 
GeneralDefining a new library in VC++ Pin
Anonymous27-Feb-05 9:21
Anonymous27-Feb-05 9:21 
GeneralRedirecting an ostream to a message box Pin
JS_H27-Feb-05 8:31
JS_H27-Feb-05 8:31 
GeneralRe: Redirecting an ostream to a message box Pin
Aamir Butt27-Feb-05 19:58
Aamir Butt27-Feb-05 19:58 
GeneralWhy cannot get filename by handle directly Pin
sprewellkobe27-Feb-05 7:37
sprewellkobe27-Feb-05 7:37 
i tried to get filename by a filehandle that was obtained by CreatFile func.

but after checking all MSDN reference,i found there is no API func can implement it directly.

the only way to do is that use the CreateFileMapping and MapViewOfFile functions to create the mapping and then use the GetMappedFileName function to obtain the file name;

such as the following code(copied from MSDN):

BOOL GetFileNameFromHandle(HANDLE hFile)
{
BOOL bSuccess = FALSE;
TCHAR pszFilename[MAX_PATH+1];
HANDLE hFileMap;

// Get the file size.
DWORD dwFileSizeHi = 0;
DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);

if( dwFileSizeLo == 0 && dwFileSizeHi == 0 )
{
printf("Cannot map a file with a length of zero.\n");
return FALSE;
}

// Create a file mapping object.
hFileMap = CreateFileMapping(hFile,
NULL,
PAGE_READONLY,
0,
1,
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 (GetMappedFileName (GetCurrentProcess(),
pMem,
pszFilename,
MAX_PATH))
{

// Translate path with device name to drive letters.
TCHAR szTemp[BUFSIZE];
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
if (QueryDosDevice(szDrive, szName, BUFSIZE))
{
UINT uNameLen = _tcslen(szName);

if (uNameLen < MAX_PATH)
{
bFound = _tcsnicmp(pszFilename, szName,
uNameLen) == 0;

if (bFound)
{
// Reconstruct pszFilename using szTemp
// Replace device path with DOS path
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);
}


i wanna know why MS not provide a func to get filename by file handle directly but to use MapView,I guess that must be with some reasons.

Thanks!;P

Stick to C++
GeneralRe: Why cannot get filename by handle directly Pin
Ravi Bhavnani27-Feb-05 10:47
professionalRavi Bhavnani27-Feb-05 10:47 
GeneralRe: Why cannot get filename by handle directly Pin
sprewellkobe27-Feb-05 16:03
sprewellkobe27-Feb-05 16:03 
QuestionHow can I get the version information about a DLL? Pin
jparsons27-Feb-05 7:15
jparsons27-Feb-05 7:15 
AnswerRe: How can I get the version information about a DLL? Pin
geo_m27-Feb-05 7:46
geo_m27-Feb-05 7:46 
AnswerRe: How can I get the version information about a DLL? Pin
Ravi Bhavnani27-Feb-05 9:20
professionalRavi Bhavnani27-Feb-05 9:20 
QuestionCan two thread run the same thread function at the same time? Pin
tttyip27-Feb-05 6:37
tttyip27-Feb-05 6:37 
AnswerRe: Can two thread run the same thread function at the same time? Pin
Ravi Bhavnani27-Feb-05 6:56
professionalRavi Bhavnani27-Feb-05 6:56 
AnswerRe: Can two thread run the same thread function at the same time? Pin
geo_m27-Feb-05 7:51
geo_m27-Feb-05 7:51 
GeneralWhat's The Best Way... Pin
LighthouseJ27-Feb-05 3:15
LighthouseJ27-Feb-05 3:15 
GeneralRe: What's The Best Way... Pin
Blake Miller28-Feb-05 5:58
Blake Miller28-Feb-05 5:58 
GeneralRe: What's The Best Way... Pin
LighthouseJ1-Mar-05 11:11
LighthouseJ1-Mar-05 11:11 
GeneralRe: What's The Best Way... Pin
Blake Miller1-Mar-05 12:21
Blake Miller1-Mar-05 12:21 
GeneralRe: What's The Best Way... Pin
LighthouseJ1-Mar-05 14:47
LighthouseJ1-Mar-05 14:47 
GeneralRe: What's The Best Way... Pin
PJ Arends28-Feb-05 6:02
professionalPJ Arends28-Feb-05 6:02 
GeneralWinHTTP, error 12175, howto Pin
qdzhulf27-Feb-05 3:10
qdzhulf27-Feb-05 3:10 
GeneralRe: WinHTTP, error 12175, howto Pin
KitchenMaster26-Feb-09 12:02
KitchenMaster26-Feb-09 12:02 
QuestionHow to catch events? Pin
Roozbeh6926-Feb-05 20:13
professionalRoozbeh6926-Feb-05 20:13 

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.