HANDLE hFile = CreateFile( "c:\\App\\test.txt", ... );在hFile成功后,如何获得test.txt呢?我在网上找了半天,MSDN也查了,还是没找到,呵呵。

解决方案 »

  1.   

    对了findFileFirst函数就不用了,因为它返回的句柄在创建映射文件时是不行的,所以我只想通过CreateFile返回的那个HANDLE来获取。谢谢!!
      

  2.   

    http://topic.csdn.net/u/20080531/22/0b8b4f76-2575-4826-a7d4-e05c66715108.html参考此贴
      

  3.   

    #define BUFSIZE 512BOOL 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);
    }
      

  4.   

    老大,我只想知道有没有相关的API啊,文件映射我自己也是可以写的。如果没有我就自己解析一下算了。
      

  5.   

    CFile   file((int)hFile);   
      CString   FileName=file.GetFileName();  
      

  6.   

    或者
    GetFileInformationByHandle
      

  7.   

    一个API实现不了,如果你的程序是MFC程序#6的方法也不错,如果不是MFC就只能用那段代码了