node-native-win-utils 1.3.1 → 1.3.2-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,173 +1,173 @@
1
- #include <napi.h>
2
- #include <iostream>
3
- #include <Windows.h>
4
- #include <dxgi.h>
5
- #include <inspectable.h>
6
- #include <dxgi1_2.h>
7
- #include <d3d11.h>
8
- #include <winrt/Windows.Foundation.h>
9
- #include <winrt/Windows.System.h>
10
- #include <winrt/Windows.Graphics.Capture.h>
11
- #include <windows.graphics.capture.interop.h>
12
- #include <windows.graphics.directx.direct3d11.interop.h>
13
- #include <roerrorapi.h>
14
- #include <shlobj_core.h>
15
- #include <dwmapi.h>
16
- #include <opencv2/core.hpp>
17
- #include <opencv2/imgcodecs.hpp>
18
-
19
- #pragma comment(lib, "Dwmapi.lib")
20
- #pragma comment(lib, "windowsapp.lib")
21
-
22
- Napi::Value CaptureWindow(const Napi::CallbackInfo &info)
23
- {
24
- Napi::Env env = info.Env();
25
-
26
- if (info.Length() < 1 || !info[0].IsString())
27
- {
28
- Napi::TypeError::New(env, "Window name must be provided as a string").ThrowAsJavaScriptException();
29
- return env.Null();
30
- }
31
-
32
- std::string windowName = info[0].As<Napi::String>().Utf8Value();
33
- HWND hwndTarget = FindWindowA(NULL, windowName.c_str());
34
- if (!hwndTarget)
35
- {
36
- Napi::TypeError::New(env, "Window not found").ThrowAsJavaScriptException();
37
- return env.Null();
38
- }
39
-
40
- // Init COM
41
- winrt::init_apartment(winrt::apartment_type::single_threaded);
42
-
43
- // Create Direct 3D Device
44
- winrt::com_ptr<ID3D11Device> d3dDevice;
45
- winrt::check_hresult(D3D11CreateDevice(
46
- nullptr,
47
- D3D_DRIVER_TYPE_HARDWARE,
48
- nullptr,
49
- D3D11_CREATE_DEVICE_BGRA_SUPPORT,
50
- nullptr,
51
- 0,
52
- D3D11_SDK_VERSION,
53
- d3dDevice.put(),
54
- nullptr,
55
- nullptr));
56
-
57
- winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice device;
58
- const auto dxgiDevice = d3dDevice.as<IDXGIDevice>();
59
- {
60
- winrt::com_ptr<::IInspectable> inspectable;
61
- winrt::check_hresult(CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice.get(), inspectable.put()));
62
- device = inspectable.as<winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice>();
63
- }
64
-
65
- auto idxgiDevice2 = dxgiDevice.as<IDXGIDevice2>();
66
- winrt::com_ptr<IDXGIAdapter> adapter;
67
- winrt::check_hresult(idxgiDevice2->GetParent(winrt::guid_of<IDXGIAdapter>(), adapter.put_void()));
68
- winrt::com_ptr<IDXGIFactory2> factory;
69
- winrt::check_hresult(adapter->GetParent(winrt::guid_of<IDXGIFactory2>(), factory.put_void()));
70
-
71
- ID3D11DeviceContext *d3dContext = nullptr;
72
- d3dDevice->GetImmediateContext(&d3dContext);
73
-
74
- RECT rect{};
75
- DwmGetWindowAttribute(hwndTarget, DWMWA_EXTENDED_FRAME_BOUNDS, &rect, sizeof(RECT));
76
- const auto size = winrt::Windows::Graphics::SizeInt32{rect.right - rect.left, rect.bottom - rect.top};
77
-
78
- winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool m_framePool =
79
- winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::Create(
80
- device,
81
- winrt::Windows::Graphics::DirectX::DirectXPixelFormat::B8G8R8A8UIntNormalized,
82
- 2,
83
- size);
84
-
85
- const auto activationFactory = winrt::get_activation_factory<
86
- winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
87
- auto interopFactory = activationFactory.as<IGraphicsCaptureItemInterop>();
88
- winrt::Windows::Graphics::Capture::GraphicsCaptureItem captureItem = {nullptr};
89
- interopFactory->CreateForWindow(hwndTarget, winrt::guid_of<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>(),
90
- reinterpret_cast<void **>(winrt::put_abi(captureItem)));
91
-
92
- auto isFrameArrived = false;
93
- winrt::com_ptr<ID3D11Texture2D> texture;
94
- const auto session = m_framePool.CreateCaptureSession(captureItem);
95
- m_framePool.FrameArrived([&](auto &framePool, auto &)
96
- {
97
- if (isFrameArrived) return;
98
- auto frame = framePool.TryGetNextFrame();
99
-
100
- struct __declspec(uuid("A9B3D012-3DF2-4EE3-B8D1-8695F457D3C1"))
101
- IDirect3DDxgiInterfaceAccess : ::IUnknown
102
- {
103
- virtual HRESULT __stdcall GetInterface(GUID const& id, void** object) = 0;
104
- };
105
-
106
- auto access = frame.Surface().as<IDirect3DDxgiInterfaceAccess>();
107
- access->GetInterface(winrt::guid_of<ID3D11Texture2D>(), texture.put_void());
108
- isFrameArrived = true; });
109
-
110
- session.IsCursorCaptureEnabled(false);
111
- session.StartCapture();
112
-
113
- // Message pump
114
- MSG msg;
115
- clock_t timer = clock();
116
- while (!isFrameArrived)
117
- {
118
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
119
- DispatchMessage(&msg);
120
-
121
- if (clock() - timer > 20000)
122
- {
123
- // TODO: try to make here a better error handling
124
- return env.Null();
125
- }
126
- }
127
-
128
- session.Close();
129
-
130
- D3D11_TEXTURE2D_DESC capturedTextureDesc;
131
- texture->GetDesc(&capturedTextureDesc);
132
-
133
- capturedTextureDesc.Usage = D3D11_USAGE_STAGING;
134
- capturedTextureDesc.BindFlags = 0;
135
- capturedTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
136
- capturedTextureDesc.MiscFlags = 0;
137
-
138
- winrt::com_ptr<ID3D11Texture2D> userTexture = nullptr;
139
- winrt::check_hresult(d3dDevice->CreateTexture2D(&capturedTextureDesc, NULL, userTexture.put()));
140
-
141
- d3dContext->CopyResource(userTexture.get(), texture.get());
142
-
143
- D3D11_MAPPED_SUBRESOURCE resource;
144
- winrt::check_hresult(d3dContext->Map(userTexture.get(), NULL, D3D11_MAP_READ, 0, &resource));
145
-
146
- BITMAPINFO lBmpInfo;
147
-
148
- // BMP 32 bpp
149
- ZeroMemory(&lBmpInfo, sizeof(BITMAPINFO));
150
- lBmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
151
- lBmpInfo.bmiHeader.biBitCount = 32;
152
- lBmpInfo.bmiHeader.biCompression = BI_RGB;
153
- lBmpInfo.bmiHeader.biWidth = capturedTextureDesc.Width;
154
- lBmpInfo.bmiHeader.biHeight = capturedTextureDesc.Height;
155
- lBmpInfo.bmiHeader.biPlanes = 1;
156
- lBmpInfo.bmiHeader.biSizeImage = capturedTextureDesc.Width * capturedTextureDesc.Height * 4;
157
-
158
- auto imageBuffer = cv::Mat(capturedTextureDesc.Height, capturedTextureDesc.Width, CV_8UC4, resource.pData, resource.RowPitch);
159
- std::vector<uchar> encodedImage;
160
- cv::imencode(".png", imageBuffer, encodedImage);
161
-
162
- if (encodedImage.empty())
163
- {
164
- return env.Null(); // Error handling for encoding failure
165
- }
166
-
167
- auto data = encodedImage.data();
168
-
169
- d3dContext->Unmap(userTexture.get(), 0);
170
- Napi::Buffer<uchar> resultBuffer = Napi::Buffer<uchar>::Copy(env, data, encodedImage.size());
171
-
172
- return resultBuffer;
173
- }
1
+ #include <napi.h>
2
+ #include <iostream>
3
+ #include <Windows.h>
4
+ #include <dxgi.h>
5
+ #include <inspectable.h>
6
+ #include <dxgi1_2.h>
7
+ #include <d3d11.h>
8
+ #include <winrt/Windows.Foundation.h>
9
+ #include <winrt/Windows.System.h>
10
+ #include <winrt/Windows.Graphics.Capture.h>
11
+ #include <windows.graphics.capture.interop.h>
12
+ #include <windows.graphics.directx.direct3d11.interop.h>
13
+ #include <roerrorapi.h>
14
+ #include <shlobj_core.h>
15
+ #include <dwmapi.h>
16
+ #include <opencv2/core.hpp>
17
+ #include <opencv2/imgcodecs.hpp>
18
+
19
+ #pragma comment(lib, "Dwmapi.lib")
20
+ #pragma comment(lib, "windowsapp.lib")
21
+
22
+ Napi::Value CaptureWindow(const Napi::CallbackInfo &info)
23
+ {
24
+ Napi::Env env = info.Env();
25
+
26
+ if (info.Length() < 1 || !info[0].IsString())
27
+ {
28
+ Napi::TypeError::New(env, "Window name must be provided as a string").ThrowAsJavaScriptException();
29
+ return env.Null();
30
+ }
31
+
32
+ std::string windowName = info[0].As<Napi::String>().Utf8Value();
33
+ HWND hwndTarget = FindWindowA(NULL, windowName.c_str());
34
+ if (!hwndTarget)
35
+ {
36
+ Napi::TypeError::New(env, "Window not found").ThrowAsJavaScriptException();
37
+ return env.Null();
38
+ }
39
+
40
+ // Init COM
41
+ winrt::init_apartment(winrt::apartment_type::single_threaded);
42
+
43
+ // Create Direct 3D Device
44
+ winrt::com_ptr<ID3D11Device> d3dDevice;
45
+ winrt::check_hresult(D3D11CreateDevice(
46
+ nullptr,
47
+ D3D_DRIVER_TYPE_HARDWARE,
48
+ nullptr,
49
+ D3D11_CREATE_DEVICE_BGRA_SUPPORT,
50
+ nullptr,
51
+ 0,
52
+ D3D11_SDK_VERSION,
53
+ d3dDevice.put(),
54
+ nullptr,
55
+ nullptr));
56
+
57
+ winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice device;
58
+ const auto dxgiDevice = d3dDevice.as<IDXGIDevice>();
59
+ {
60
+ winrt::com_ptr<::IInspectable> inspectable;
61
+ winrt::check_hresult(CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice.get(), inspectable.put()));
62
+ device = inspectable.as<winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice>();
63
+ }
64
+
65
+ auto idxgiDevice2 = dxgiDevice.as<IDXGIDevice2>();
66
+ winrt::com_ptr<IDXGIAdapter> adapter;
67
+ winrt::check_hresult(idxgiDevice2->GetParent(winrt::guid_of<IDXGIAdapter>(), adapter.put_void()));
68
+ winrt::com_ptr<IDXGIFactory2> factory;
69
+ winrt::check_hresult(adapter->GetParent(winrt::guid_of<IDXGIFactory2>(), factory.put_void()));
70
+
71
+ ID3D11DeviceContext *d3dContext = nullptr;
72
+ d3dDevice->GetImmediateContext(&d3dContext);
73
+
74
+ RECT rect{};
75
+ DwmGetWindowAttribute(hwndTarget, DWMWA_EXTENDED_FRAME_BOUNDS, &rect, sizeof(RECT));
76
+ const auto size = winrt::Windows::Graphics::SizeInt32{rect.right - rect.left, rect.bottom - rect.top};
77
+
78
+ winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool m_framePool =
79
+ winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::Create(
80
+ device,
81
+ winrt::Windows::Graphics::DirectX::DirectXPixelFormat::B8G8R8A8UIntNormalized,
82
+ 2,
83
+ size);
84
+
85
+ const auto activationFactory = winrt::get_activation_factory<
86
+ winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
87
+ auto interopFactory = activationFactory.as<IGraphicsCaptureItemInterop>();
88
+ winrt::Windows::Graphics::Capture::GraphicsCaptureItem captureItem = {nullptr};
89
+ interopFactory->CreateForWindow(hwndTarget, winrt::guid_of<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>(),
90
+ reinterpret_cast<void **>(winrt::put_abi(captureItem)));
91
+
92
+ auto isFrameArrived = false;
93
+ winrt::com_ptr<ID3D11Texture2D> texture;
94
+ const auto session = m_framePool.CreateCaptureSession(captureItem);
95
+ m_framePool.FrameArrived([&](auto &framePool, auto &)
96
+ {
97
+ if (isFrameArrived) return;
98
+ auto frame = framePool.TryGetNextFrame();
99
+
100
+ struct __declspec(uuid("A9B3D012-3DF2-4EE3-B8D1-8695F457D3C1"))
101
+ IDirect3DDxgiInterfaceAccess : ::IUnknown
102
+ {
103
+ virtual HRESULT __stdcall GetInterface(GUID const& id, void** object) = 0;
104
+ };
105
+
106
+ auto access = frame.Surface().as<IDirect3DDxgiInterfaceAccess>();
107
+ access->GetInterface(winrt::guid_of<ID3D11Texture2D>(), texture.put_void());
108
+ isFrameArrived = true; });
109
+
110
+ session.IsCursorCaptureEnabled(false);
111
+ session.StartCapture();
112
+
113
+ // Message pump
114
+ MSG msg;
115
+ clock_t timer = clock();
116
+ while (!isFrameArrived)
117
+ {
118
+ if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
119
+ DispatchMessage(&msg);
120
+
121
+ if (clock() - timer > 20000)
122
+ {
123
+ // TODO: try to make here a better error handling
124
+ return env.Null();
125
+ }
126
+ }
127
+
128
+ session.Close();
129
+
130
+ D3D11_TEXTURE2D_DESC capturedTextureDesc;
131
+ texture->GetDesc(&capturedTextureDesc);
132
+
133
+ capturedTextureDesc.Usage = D3D11_USAGE_STAGING;
134
+ capturedTextureDesc.BindFlags = 0;
135
+ capturedTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
136
+ capturedTextureDesc.MiscFlags = 0;
137
+
138
+ winrt::com_ptr<ID3D11Texture2D> userTexture = nullptr;
139
+ winrt::check_hresult(d3dDevice->CreateTexture2D(&capturedTextureDesc, NULL, userTexture.put()));
140
+
141
+ d3dContext->CopyResource(userTexture.get(), texture.get());
142
+
143
+ D3D11_MAPPED_SUBRESOURCE resource;
144
+ winrt::check_hresult(d3dContext->Map(userTexture.get(), NULL, D3D11_MAP_READ, 0, &resource));
145
+
146
+ BITMAPINFO lBmpInfo;
147
+
148
+ // BMP 32 bpp
149
+ ZeroMemory(&lBmpInfo, sizeof(BITMAPINFO));
150
+ lBmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
151
+ lBmpInfo.bmiHeader.biBitCount = 32;
152
+ lBmpInfo.bmiHeader.biCompression = BI_RGB;
153
+ lBmpInfo.bmiHeader.biWidth = capturedTextureDesc.Width;
154
+ lBmpInfo.bmiHeader.biHeight = capturedTextureDesc.Height;
155
+ lBmpInfo.bmiHeader.biPlanes = 1;
156
+ lBmpInfo.bmiHeader.biSizeImage = capturedTextureDesc.Width * capturedTextureDesc.Height * 4;
157
+
158
+ auto imageBuffer = cv::Mat(capturedTextureDesc.Height, capturedTextureDesc.Width, CV_8UC4, resource.pData, resource.RowPitch);
159
+ std::vector<uchar> encodedImage;
160
+ cv::imencode(".png", imageBuffer, encodedImage);
161
+
162
+ if (encodedImage.empty())
163
+ {
164
+ return env.Null(); // Error handling for encoding failure
165
+ }
166
+
167
+ auto data = encodedImage.data();
168
+
169
+ d3dContext->Unmap(userTexture.get(), 0);
170
+ Napi::Buffer<uchar> resultBuffer = Napi::Buffer<uchar>::Copy(env, data, encodedImage.size());
171
+
172
+ return resultBuffer;
173
+ }
@@ -1,46 +1,46 @@
1
- #include <napi.h>
2
- #include <Windows.h>
3
- #include <iostream>
4
- #include <dwmapi.h>
5
- #include <helpers.h>
6
-
7
- Napi::Value GetWindowData(const Napi::CallbackInfo &info)
8
- {
9
- Napi::Env env = info.Env();
10
-
11
- if (info.Length() < 1 || !info[0].IsString())
12
- {
13
- Napi::TypeError::New(env, "Window name must be provided as a string").ThrowAsJavaScriptException();
14
- return env.Null();
15
- }
16
-
17
- std::string windowName = info[0].As<Napi::String>().Utf8Value();
18
-
19
- HWND windowHandle = GetWindowByName(windowName.c_str());
20
-
21
- if (windowHandle == NULL)
22
- {
23
- Napi::Error::New(env, "Window not found").ThrowAsJavaScriptException();
24
- return env.Null();
25
- }
26
-
27
- RECT windowRect;
28
- DwmGetWindowAttribute(windowHandle, DWMWA_EXTENDED_FRAME_BOUNDS, &windowRect, sizeof(windowRect));
29
- HDC hdc = GetDC(windowHandle);
30
- int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
31
- int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
32
- ReleaseDC(windowHandle, hdc);
33
-
34
- int width = windowRect.right - windowRect.left;
35
- int height = windowRect.bottom - windowRect.top;
36
- int x = windowRect.left;
37
- int y = windowRect.top;
38
-
39
- Napi::Object result = Napi::Object::New(env);
40
- result.Set("width", Napi::Number::New(env, width));
41
- result.Set("height", Napi::Number::New(env, height));
42
- result.Set("x", Napi::Number::New(env, x));
43
- result.Set("y", Napi::Number::New(env, y));
44
-
45
- return result;
1
+ #include <napi.h>
2
+ #include <Windows.h>
3
+ #include <iostream>
4
+ #include <dwmapi.h>
5
+ #include <helpers.h>
6
+
7
+ Napi::Value GetWindowData(const Napi::CallbackInfo &info)
8
+ {
9
+ Napi::Env env = info.Env();
10
+
11
+ if (info.Length() < 1 || !info[0].IsString())
12
+ {
13
+ Napi::TypeError::New(env, "Window name must be provided as a string").ThrowAsJavaScriptException();
14
+ return env.Null();
15
+ }
16
+
17
+ std::string windowName = info[0].As<Napi::String>().Utf8Value();
18
+
19
+ HWND windowHandle = GetWindowByName(windowName.c_str());
20
+
21
+ if (windowHandle == NULL)
22
+ {
23
+ Napi::Error::New(env, "Window not found").ThrowAsJavaScriptException();
24
+ return env.Null();
25
+ }
26
+
27
+ RECT windowRect;
28
+ DwmGetWindowAttribute(windowHandle, DWMWA_EXTENDED_FRAME_BOUNDS, &windowRect, sizeof(windowRect));
29
+ HDC hdc = GetDC(windowHandle);
30
+ int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
31
+ int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
32
+ ReleaseDC(windowHandle, hdc);
33
+
34
+ int width = windowRect.right - windowRect.left;
35
+ int height = windowRect.bottom - windowRect.top;
36
+ int x = windowRect.left;
37
+ int y = windowRect.top;
38
+
39
+ Napi::Object result = Napi::Object::New(env);
40
+ result.Set("width", Napi::Number::New(env, width));
41
+ result.Set("height", Napi::Number::New(env, height));
42
+ result.Set("x", Napi::Number::New(env, x));
43
+ result.Set("y", Napi::Number::New(env, y));
44
+
45
+ return result;
46
46
  }
@@ -1,15 +1,15 @@
1
-
2
- #include "helpers.h"
3
-
4
- std::wstring ToWChar(const std::string &str)
5
- {
6
- int bufferSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
7
- std::wstring wstr(bufferSize, 0);
8
- MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], bufferSize);
9
- return wstr;
10
- }
11
-
12
- HWND GetWindowByName(const char *windowName)
13
- {
14
- return FindWindowA(NULL, windowName);
1
+
2
+ #include "helpers.h"
3
+
4
+ std::wstring ToWChar(const std::string &str)
5
+ {
6
+ int bufferSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
7
+ std::wstring wstr(bufferSize, 0);
8
+ MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], bufferSize);
9
+ return wstr;
10
+ }
11
+
12
+ HWND GetWindowByName(const char *windowName)
13
+ {
14
+ return FindWindowA(NULL, windowName);
15
15
  }
package/src/cpp/helpers.h CHANGED
@@ -1,8 +1,8 @@
1
- #include <Windows.h>
2
- #include <iostream>
3
- #include <codecvt>
4
- #include <Windows.h>
5
- #include <napi.h>
6
-
7
- std::wstring ToWChar(const std::string &str);
8
- HWND GetWindowByName(const char *windowName);
1
+ #include <Windows.h>
2
+ #include <iostream>
3
+ #include <codecvt>
4
+ #include <Windows.h>
5
+ #include <napi.h>
6
+
7
+ std::wstring ToWChar(const std::string &str);
8
+ HWND GetWindowByName(const char *windowName);