请帮忙翻译小段C代码到delphi(55分)

  • 主题发起人 主题发起人 sadnui
  • 开始时间 开始时间
S

sadnui

Unregistered / Unconfirmed
GUEST, unregistred user!
#include <windows.h>
#pragma comment(lib, "ntdll.lib")
#pragma comment(linker, "/subsystem:windows")
#pragma comment(linker, "/entry:start")
#pragma comment(linker, "/filealign:0x200")


typedef struct _IO_STATUS_BLOCK
{
LONG Status;
LONG Information;
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;

typedef struct _FILE_NAME_INFORMATION
{
ULONG FileNameLength;
WCHAR FileName[MAX_PATH];
} FILE_NAME_INFORMATION;

__declspec(dllimport) LONG __stdcall ZwQueryInformationFile(
IN HANDLE FileHandle,
OUT PIO_STATUS_BLOCK IoStatusBlock,
OUT PVOID FileInformation,
IN ULONG FileInformationLength,
IN ULONG FileInformationClass
);


void start()
{
char szFileName[MAX_PATH];
HANDLE hFile;
IO_STATUS_BLOCK isb;
FILE_NAME_INFORMATION fni;



GetModuleFileName(NULL, szFileName, MAX_PATH);
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != (HANDLE)-1)
{
if (ZwQueryInformationFile(hFile, &isb, &fni, sizeof(fni), 9) == 0)
{
fni.FileName[fni.FileNameLength/2] = 0;
MessageBoxW(0, fni.FileName, L"hFile", 0);

}
CloseHandle(hFile);

}
ExitProcess(0);
}

这个是能从文件句柄返回文件名的代码。
我自己测试翻译了一份确总不能正确执行
还请高手们帮忙分析一下
(代码来自看雪论坛)
 
思路不同的另一種方案
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3638502
 
谢谢结帖
 
function GetFileNameFromHandle(hFile:THandle):String;
Const
BUFSIZE=512;
Var
pszFilename:Array[0..BUFSIZE+1] of CHAR;
hFileMap:THandle;
//dwFileSizeHi:DWORD;
//dwFileSizeLo:DWORD;
pMem:Pointer;
szTemp:Array[0..MAX_PATH-1] of CHAR;
p:PChar;
Str:String;
begin
Result:='';
FillChar(pszFilename,sizeof(pszFilename),0);
hFileMap := CreateFileMapping(hFile,
nil,
PAGE_READONLY,
0,
1,
nil);

if hFileMap<>INVALID_HANDLE_VALUE then
begin
pMem := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
if Assigned(pMem) then
begin
if GetMappedFileName (GetCurrentProcess,
pMem,
pszFilename,
MAX_PATH)<>0 then
begin
Str:=StrPas(pszFileName);
if GetLogicalDriveStrings(BUFSIZE-1, @pszFileName)<>0 then
begin
p:=@pszFileName;
Repeat
Inc(p,2);
p^:=#0;
Dec(p,2);
FillChar(szTemp,sizeof(szTemp),0);
if QueryDosDevice(p, @szTemp, MAX_PATH)<>0 then
begin
if AnsiPos(szTemp,Str)<>0 then
begin
Result := p+AnsiReplaceStr(Str,szTemp,'');
Exit;
end;
end;
Inc(p,4);
Until p^=#0;
end;
end;
UnmapViewOfFile(pMem);
end;
CloseHandle(hFileMap);
end;
end;
 
后退
顶部