Using js-ctypes, how do I retrieve output from Windows ReadFile?

99封情书 提交于 2019-12-24 17:43:37

问题


I'm using (while not being quite sure about lpBuffer -> char.ptr)

  let readFile = kernel32.declare(
    "ReadFile",
    ctypes.winapi_abi,
    ctypes.int32_t,     //bool  // return type: 1 indicates success, 0 failure
    ctypes.voidptr_t,   // in: hObject
    ctypes.char.ptr,  // out: lpBuffer
    ctypes.uint32_t,    // in: lpNumberOfBytesRead
    ctypes.uint32_t.ptr,    // out: lpNumberOfBytesWritten
    ctypes.voidptr_t.ptr    // inout: lpOverlapped
  );

for

BOOL WINAPI ReadFile(
  _In_        HANDLE       hFile,
  _Out_       LPVOID       lpBuffer,
  _In_        DWORD        nNumberOfBytesToRead,
  _Out_opt_   LPDWORD      lpNumberOfBytesRead,
  _Inout_opt_ LPOVERLAPPED lpOverlapped
);

However, when I try

  let output = ctypes.char.array(1024);
  let isRead = readFile(hFile, output, 1024, bytesWritten.address())

I get

JPM [error] Message: TypeError: can't pass ctypes.char.array(1024) to argument 2 of int32_t ReadFile(void*, char*, uint32_t, void**)

More attempts.

  let output = ctypes.char.array(1024);
  let isRead = readFile(hFile, output.ptr, 1024, bytesWritten.address())

JPM [error] Message: TypeError: can't pass ctypes.char.array(1024).ptr to argument 2 of int32_t ReadFile(void*, char*, uint32_t, void**

  let output = ctypes.char.array(1024);
  let isRead = readFile(hFile, output.address(), 1024, bytesWritten.address())

JPM [error] Message: TypeError: output.address is not a function

  let output = ctypes.char.ptr;
  let isRead = readFile(hFile, output, 1024, bytesWritten.address())

JPM [error] Message: TypeError: can't pass ctypes.char.ptr to argument 2 of int32_t ReadFile(void*, char*, uint32_t, v oid**)


回答1:


This made it work

  let outputType = ctypes.char.array(1024);
  let output = outputType();
  let isRead = readFile(hFile, output, 1024, bytesWritten.address(), null);


来源:https://stackoverflow.com/questions/36031573/using-js-ctypes-how-do-i-retrieve-output-from-windows-readfile

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