Can we call Native Windows API from Delphi?

China☆狼群 提交于 2019-12-10 13:16:31

问题


Is it possible to call the kernel Native APIs from within a Delphi application? Like nt and zw syscalls.


回答1:


You can indeed call the native API from Delphi.

Delphi does not ship with header translations for the native API. So you need to provide your own, or use a pre-existing translation. For example. the JEDI translation of the NT API.




回答2:


As David Heffernan says it's perfectly possible to use the Native API from usermode and thus Delphi. You will need the JwaNative unit from the Jedi Apilib.

Here is small example to enumerate processes using the Native API: (TProcessList is a descendant from TObjectList but the relevant part is the call to NtQuerySystemInformation)

function EnumProcesses: TProcessList;
var
  Current: PSystemProcesses;
  SystemProcesses : PSystemProcesses;
  dwSize: DWORD;
  nts: NTSTATUS;
begin
  Result := TProcessList.Create;

  dwSize := 200000;
  SystemProcesses := AllocMem(dwSize);

  nts := NtQuerySystemInformation(SystemProcessesAndThreadsInformation,
      SystemProcesses, dwSize, @dwSize);

  while nts = STATUS_INFO_LENGTH_MISMATCH do
  begin
    ReAllocMem(SystemProcesses, dwSize);
    nts := NtQuerySystemInformation(SystemProcessesAndThreadsInformation,
      SystemProcesses, dwSize, @dwSize);
  end;

  if nts = STATUS_SUCCESS then
  begin
    Current := SystemProcesses;
    while True do
    begin
      Result.Add(TProcess.Create(Current^));
      if Current^.NextEntryDelta = 0 then
        Break;

      Current := PSYSTEM_PROCESSES(DWORD_PTR(Current) + Current^.NextEntryDelta);
    end;
  end;

  FreeMem(SystemProcesses);
end;


来源:https://stackoverflow.com/questions/14920950/can-we-call-native-windows-api-from-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!