Last active
August 29, 2015 13:57
-
-
Save joskuijpers/9540937 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <node.h> | |
#include <v8.h> | |
#include <windows.h> | |
#include <stdio.h> | |
#include <conio.h> | |
#include <tchar.h> | |
//#include <iostream> | |
using namespace v8; | |
Local<String> LpStrToV8(LPSTR str) { | |
HandleScope scope; | |
Local<String> v8str; | |
v8str = String::New(static_cast<char*>(str)); | |
return scope.Close(v8str); | |
} | |
Handle<Value> Method(const Arguments& args){ | |
// Isolate* isolate = Isolate::GetCurrent(); | |
HandleScope scope; | |
DWORD dwSizeNeeded; | |
DWORD dwNumItems; | |
LPPRINTER_INFO_2 lpInfo = NULL; | |
EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &dwSizeNeeded, &dwNumItems); | |
lpInfo = (LPPRINTER_INFO_2) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSizeNeeded); | |
if(lpInfo == NULL) | |
{ | |
return scope.Close(String::New("{\"error\": \"Not enough memory to enumerate printers.\"}")); | |
} | |
if(EnumPrintersW(PRINTER_ENUM_LOCAL, // what to enumerate | |
NULL, // printer name (NULL for all) | |
2, // level | |
(LPBYTE)lpInfo, // buffer | |
dwSizeNeeded, // size of buffer | |
&dwSizeNeeded, // returns size | |
&dwNumItems // return num. items | |
) == 0) | |
{ | |
return scope.Close(String::New("{\"error\": \"EnumPrinters() failed.\"}")); | |
} | |
Handle<Array> printerList = Array::New(dwNumItems); | |
for(unsigned int i = 0; i < dwNumItems; i++) | |
{ | |
Handle<Object> printer = Object::New(); | |
printer->Set(String::New("printer"), LpStrToV8(lpInfo[i].pPrinterName)); | |
printer->Set(String::New("port"), LpStrToV8(lpInfo[i].pPortName)); | |
printer->Set(String::New("driver"), LpStrToV8(lpInfo[i].pDriverName)); | |
printer->Set(String::New("comments"), LpStrToV8(lpInfo[i].pComment)); | |
printer->Set(String::New("location"), LpStrToV8(lpInfo[i].pLocation)); | |
printerList->Set(i, printer); | |
} | |
// free memory | |
HeapFree(GetProcessHeap(), 0, lpInfo); | |
return scope.Close(printerList); | |
} | |
void init(Handle<Object> target){ | |
target->Set(String::NewSymbol("getPrinters"), | |
FunctionTemplate::New(Method)->GetFunction() | |
); | |
} | |
NODE_MODULE(printers, init) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment