小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

delphi 根據(jù)文件句柄獲取文件路徑

 獨(dú)孤求財(cái) 2012-03-20

delphi 根據(jù)文件句柄獲取文件路徑

時(shí)間:2011-5-26來源:yang 作者: peng點(diǎn)擊: 18次

在寫一個(gè)小程序的時(shí)候,只能得到文件的句柄,需要轉(zhuǎn)換成文件路徑(包涵了整個(gè)文件名的),但是只能在同一個(gè)application中使用,夸了不同的application就不行了,下面貼出代碼:
第一個(gè)function:
const BUFSIZE = 512;
function GetFileNameFromHandle(hFile:THANDLE ):String;
var
  pszFilename: array [0..MAX_PATH] of char;
  hFileMap: THANDLE;
  szTemp: array [0..BUFSIZE - 1] of char;
  szName: array [0..MAX_PATH - 1] of char;
  // Get the file size.
  dwFileSizeHi: DWORD  ;
  dwFileSizeLo: DWORD;
  pMem: Pointer;
  szDrive: String;
  bFound: BOOL;
  P: PChar;
  uNameLen: UINT;
  szTempFile: String;
begin
  Result := ‘‘;
  dwFileSizeHi := 0;
  dwFileSizeLo := GetFileSize(hFile, @dwFileSizeHi);
  if( dwFileSizeLo = 0) and (dwFileSizeHi = 0 ) then begin
     ShowMessage(‘Cannot map a file with a length of zero.‘);
     Exit;
  end;

  // Create a file mapping object.
  hFileMap := CreateFileMapping(hFile,
                    Nil,
                    PAGE_READONLY,
                    0,
                    0,
                    Nil);

  if (hFileMap <> 0) then begin
    // Create a file mapping to get the file name.
    pMem := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);

    if (pMem <> NIl) then begin
      if (GetMappedFileName (GetCurrentProcess(),
                             pMem,
                             PChar(@pszFilename[0]),
                             MAX_PATH) <> 0) then
      begin

        // Translate path with device name to drive letters.
        szTemp[0] := #0;

        if (GetLogicalDriveStrings(BUFSIZE-1, @szTemp[0])) <> 0 then
        begin

          //szDrive := ‘ :‘#0;
          bFound := FALSE;
          p := @szTemp[0];

          repeat
            // Copy the drive letter to the template string
            //PChar(szDrive)^ := p^;
            szTemp[2] := #0;
            szDrive := StrPas(p);


            // Look up each device name
            fillchar(szName,Length(szName), 0);
            if (QueryDosDevice(PChar(szDrive), @szName[0], BUFSIZE) <> 0) then
            begin
              uNameLen := strlen(@szName[0]);

              if (uNameLen < MAX_PATH) then
              begin
                bFound := strlicomp(@pszFilename[0], @szName[0],
                    uNameLen) = 0;

                if (bFound) then
                begin
                  // Reconstruct pszFilename using szTempFile
                  // Replace device path with DOS path
                  szTempFile := format(‘%s%s‘,
                            [szDrive,
                            StrPas(PChar(Integer(PChar(@pszFilename[0]))+uNameLen))]);
                  //StringCchCopyN(pszFilename, MAX_PATH+1, szTempFile, _tcslen(szTempFile));
                  move(PChar(szTempFile)^, pszFilename, Length(szTempFile)+1);
                end;
              end;
            end;

            // Go to the next NULL character.
            while (p^<>#0) do Inc(P);

           until (bFound or (p^ = #0)); // end of string
        end;
      end;
      UnmapViewOfFile(pMem);
    end;

    CloseHandle(hFileMap);
  end;
  Result := String(pszFilename);
end;
另外一個(gè)function,也實(shí)現(xiàn)了同樣的功能:
function GetFileNameFromHandle(hFile : Cardinal) : String;
var
hFileMap : Cardinal;
pMem : Pointer;
FilePath : array [0..MAX_PATH - 1] of Char;
sFilePath : String;
sFileName : String;
begin
  Result := ‘‘;
  try
    hFileMap := CreateFileMapping(hFile, NIL, PAGE_READONLY, 0, 0, NIL);
    if hFileMap <> 0 then
    begin
      pMem := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
      if pMem <> NIL then
      begin
        if GetMappedFileName(GetCurrentProcess(), pMem,
                             @FilePath, SizeOf(FilePath)) <> 0 then
          begin
            sFilePath := StrPas(FilePath);
            sFileName := ExtractFileName(sFilePath);
            Result := sFilePath;
          end else Exit;
      end else Exit;
    end else Exit;
  finally
   UnMapViewOfFile(pMem);
   CloseHandle(hFileMap);
  end; // try...finally

end; // GetFileNameFromHandle

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多