我正在将二进制组件转换为 js-ctypes 并且 StartPagePrinter 给了我 ERROR_INVALID_HANDLE。我正在使用标准的 Windows 技术进行打印。(搜索“MSDN WritePrinter”以查看基本步骤。)我想我要么使用了错误的类型,要么没有进行某种类型的转换。
我没有将 ctypes.uintptr_t 用于 jobHandle,而是尝试了 ctypes.intptr_t、ctypes.voidptr_t 和 ctypes.int32_t。它们都因相同的错误而失败。
我必须承认这是我 30 年职业生涯中完成的第一个 Win32 编程,所以请多多包涵。
这是代码片段:
Components.utils.import('resource://gre/modules/ctypes.jsm');
var winspoolLib = ctypes.open('winspool.drv');
var spoolssLib = ctypes.open('spoolss');
const docNameLength = 32;
const printer = "My Printer"; // A valid, configured printer
var jobHandle = new ctypes.uintptr_t(0);
var openPrinter = winspoolLib.declare(
"OpenPrinterW", ctypes.winapi_abi, ctypes.bool, ctypes.jschar.ptr,
ctypes.uintptr_t.ptr, ctypes.voidptr_t
);
if (openPrinter(printer, jobHandle.address(), null)) {
const docInfo1 = new ctypes.StructType(
"docInfo1",
[
{pDocName: ctypes.jschar.ptr},
{pOutputFile: ctypes.voidptr_t},
{pDataType: ctypes.voidptr_t}
]
);
var startDocPrinter = winspoolLib.declare(
"StartDocPrinterW", ctypes.winapi_abi, ctypes.int32_t,
ctypes.uintptr_t, ctypes.int32_t, docInfo1.ptr
);
var docInfo = new docInfo1(
ctypes.jschar.array(docNameLength)('MYJOB'), null, null
);
var job = startDocPrinter(jobHandle, 1, docInfo.address());
if (job != 0) {
var startPagePrinter = spoolssLib.declare(
"StartPagePrinter", ctypes.winapi_abi, ctypes.bool,
ctypes.uintptr_t
);
if (! startPagePrinter(jobHandle))
// Always fails with ERROR_INVALID_HANDLE (6)
alert('There was an error: ' + ctypes.winLastError);
}
}