forked from Srar/node-tap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevicecontrol.cpp
More file actions
52 lines (43 loc) · 1.38 KB
/
devicecontrol.cpp
File metadata and controls
52 lines (43 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <nan.h>
#include "./error.hpp"
using namespace std;
NAN_METHOD(N_DeviceControl)
{
if (info.Length() != 4)
{
Nan::ThrowError("Wrong number of arguments.");
return;
}
if (!info[0]->IsNumber())
{
Nan::ThrowError("Wrong type of handle.");
return;
}
long handle = (long)info[0]->NumberValue();
if (!info[1]->IsNumber())
{
Nan::ThrowError("Wrong type of CTLCode.");
return;
}
DWORD CTLCode = (DWORD)info[1]->NumberValue();
v8::Local<v8::Object> inputJsBuffer = v8::Local<v8::Object>::Cast(info[2]);
void *inputData = (void *)node::Buffer::Data(inputJsBuffer);
DWORD inputDataLength = node::Buffer::Length(inputJsBuffer);
if (!info[3]->IsNumber())
{
Nan::ThrowError("Wrong type of output length.");
return;
}
DWORD outputDataLength = (DWORD)info[3]->NumberValue();
void *outputData = malloc(outputDataLength);
DWORD outputDataReturned;
DeviceIoControl((HANDLE)handle, CTLCode, inputData, inputDataLength, outputData, outputDataLength, &outputDataReturned, NULL);
DWORD errorCode = GetLastError();
if (errorCode != ERROR_SUCCESS)
{
Nan::ThrowError("DeviceIoControl Failed: " + errorCode);
return;
}
info.GetReturnValue().Set(Nan::CopyBuffer((char *)outputData, outputDataReturned).ToLocalChecked());
free(outputData);
}