FILE 就是一个结构,struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; typedef struct _iobuf FILE;
UINT uMaxLenDest = 0; // Get the file size. DWORD dwFileSizeHi = 0; DWORD dwFileSizeLo = 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 (GetMappedFileName (GetCurrentProcess(), pMem, (LPTSTR)pszFilename, MAX_PATH)) { // Attempt to translate the path with device name into drive // letters. TCHAR szTemp[512]; *szTemp = NULL; if (GetLogicalDriveStrings(sizeof(szTemp)-1, szTemp)) { TCHAR szName[MAX_PATH]; TCHAR szDrive[3] = TEXT(" :"); BOOL bFound = FALSE; TCHAR* p = szTemp; do { // Copy the drive letter into the template string, // removing the backslash. *szDrive = *p; // Look up each device name. if (QueryDosDevice(szDrive, szName, sizeof(szName))) { UINT uNameLen = _tcslen(szName); // If greater than file name length, it's not a match. if (uNameLen < uMaxLenDest) { bFound = _tcsnicmp((char *)pszFilename, szName, uNameLen) == 0; if (bFound) { // Reconstruct pszFilename using szTemp as scratch, // and replace device path with our DOS path. TCHAR szTempFile[MAX_PATH]; _stprintf(szTempFile, TEXT("%s%s"), szDrive, pszFilename+uNameLen); strncpy(pszFilename, szTempFile, uMaxLenDest); } } } // Go to the next NULL character. while (*p++); } while (!bFound && *p); // at the end of the string } } bSuccess = TRUE; if (!UnmapViewOfFile(pMem)) ASSERT(0); } if (!CloseHandle(hFileMap)) ASSERT(0); } return CString(pszFilename); }
通过 _get_osfhandle(_fileno(fp)) 可以获得 windows 的文件 HANDLE ,然后根据 neosu(neo) 的做法即可获得。===== 不懂~~~
UINT uMaxLenDest = sizeof(pszFilename); // Get the file size. DWORD dwFileSizeHi = 0; DWORD dwFileSizeLo = 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 (GetMappedFileName (GetCurrentProcess(), pMem, (LPTSTR)pszFilename, MAX_PATH)) { // Attempt to translate the path with device name into drive // letters. TCHAR szTemp[512]; *szTemp = NULL; if (GetLogicalDriveStrings(sizeof(szTemp)-1, szTemp)) { TCHAR szName[MAX_PATH]; TCHAR szDrive[3] = TEXT(" :"); BOOL bFound = FALSE; TCHAR* p = szTemp; do { // Copy the drive letter into the template string, // removing the backslash. *szDrive = *p; // Look up each device name. if (QueryDosDevice(szDrive, szName, sizeof(szName))) { UINT uNameLen = _tcslen(szName); // If greater than file name length, it's not a match. if (uNameLen < uMaxLenDest) { bFound = _tcsnicmp((char *)pszFilename, szName, uNameLen) == 0; if (bFound) { // Reconstruct pszFilename using szTemp as scratch, // and replace device path with our DOS path. TCHAR szTempFile[MAX_PATH]; _stprintf(szTempFile, TEXT("%s%s"), szDrive, pszFilename+uNameLen); strncpy(pszFilename, szTempFile, uMaxLenDest); } } } // Go to the next NULL character. while (*p++); } while (!bFound && *p); // at the end of the string } } bSuccess = TRUE; if (!UnmapViewOfFile(pMem)) ASSERT(0); } if (!CloseHandle(hFileMap)) ASSERT(0); } return CString(pszFilename); }
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
typedef struct _iobuf FILE;
CString m_szStr = hFile.GetFileName();
AfxMessageBox(m_szStr);
HMODULE hModule,
LPWSTR lpFilename,
DWORD nSize
);例:
TCHAR AppPath[MAX_PATH];
GetModuleFileName(m_hInstance,AppPath,MAX_PATH);
CString Path;
Path.Format(_T("%s"),AppPath);
再交给后面的函数处理
要include io.h psapi.h
输出的结果还不太正确. 为"\Device\HarddiskVolume1\文件名"
再改改就可以了.
CString GetFilenameFromHandle(HANDLE hFile)
{
BOOL bSuccess = FALSE;
// TCHAR* pszFilename[MAX_PATH+1];
TCHAR pszFilename[MAX_PATH+1];
ZeroMemory(pszFilename, sizeof(pszFilename));
UINT uMaxLenDest = 0; // Get the file size.
DWORD dwFileSizeHi = 0;
DWORD dwFileSizeLo = 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 (GetMappedFileName (GetCurrentProcess(),
pMem,
(LPTSTR)pszFilename,
MAX_PATH)) { // Attempt to translate the path with device name into drive
// letters.
TCHAR szTemp[512];
*szTemp = NULL; if (GetLogicalDriveStrings(sizeof(szTemp)-1,
szTemp)) {
TCHAR szName[MAX_PATH];
TCHAR szDrive[3] = TEXT(" :");
BOOL bFound = FALSE;
TCHAR* p = szTemp; do {
// Copy the drive letter into the template string,
// removing the backslash.
*szDrive = *p; // Look up each device name.
if (QueryDosDevice(szDrive, szName,
sizeof(szName))) {
UINT uNameLen = _tcslen(szName); // If greater than file name length, it's not a match.
if (uNameLen < uMaxLenDest) {
bFound = _tcsnicmp((char *)pszFilename, szName, uNameLen)
== 0; if (bFound) {
// Reconstruct pszFilename using szTemp as scratch,
// and replace device path with our DOS path.
TCHAR szTempFile[MAX_PATH];
_stprintf(szTempFile,
TEXT("%s%s"),
szDrive,
pszFilename+uNameLen);
strncpy(pszFilename, szTempFile, uMaxLenDest);
}
}
} // Go to the next NULL character.
while (*p++);
} while (!bFound && *p); // at the end of the string
}
}
bSuccess = TRUE;
if (!UnmapViewOfFile(pMem)) ASSERT(0);
} if (!CloseHandle(hFileMap)) ASSERT(0);
}
return CString(pszFilename);
}
不懂~~~
=====
懂了~~:)
{
BOOL bSuccess = FALSE;
// TCHAR* pszFilename[MAX_PATH+1];
TCHAR pszFilename[MAX_PATH+1];
ZeroMemory(pszFilename, sizeof(pszFilename));
UINT uMaxLenDest = sizeof(pszFilename); // Get the file size.
DWORD dwFileSizeHi = 0;
DWORD dwFileSizeLo = 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 (GetMappedFileName (GetCurrentProcess(),
pMem,
(LPTSTR)pszFilename,
MAX_PATH)) { // Attempt to translate the path with device name into drive
// letters.
TCHAR szTemp[512];
*szTemp = NULL; if (GetLogicalDriveStrings(sizeof(szTemp)-1,
szTemp)) {
TCHAR szName[MAX_PATH];
TCHAR szDrive[3] = TEXT(" :");
BOOL bFound = FALSE;
TCHAR* p = szTemp; do {
// Copy the drive letter into the template string,
// removing the backslash.
*szDrive = *p; // Look up each device name.
if (QueryDosDevice(szDrive, szName,
sizeof(szName))) {
UINT uNameLen = _tcslen(szName); // If greater than file name length, it's not a match.
if (uNameLen < uMaxLenDest) {
bFound = _tcsnicmp((char *)pszFilename, szName, uNameLen)
== 0; if (bFound) {
// Reconstruct pszFilename using szTemp as scratch,
// and replace device path with our DOS path.
TCHAR szTempFile[MAX_PATH];
_stprintf(szTempFile,
TEXT("%s%s"),
szDrive,
pszFilename+uNameLen);
strncpy(pszFilename, szTempFile, uMaxLenDest);
}
}
} // Go to the next NULL character.
while (*p++);
} while (!bFound && *p); // at the end of the string
}
}
bSuccess = TRUE;
if (!UnmapViewOfFile(pMem)) ASSERT(0);
} if (!CloseHandle(hFileMap)) ASSERT(0);
}
return CString(pszFilename);
}