1+ // src/addon.cpp
12#include < napi.h>
23
34#ifdef _WIN32
1011#include < exception>
1112#include < fstream>
1213#include < algorithm>
14+ #include < iostream>
1315
1416#pragma comment(lib, "Ole32.lib")
1517#pragma comment(lib, "Shlwapi.lib")
@@ -89,7 +91,9 @@ Napi::Value getImageBuffer(const Napi::CallbackInfo& info, bool useThumbnail) {
8991
9092 if (SUCCEEDED (hr) && hBitmap) {
9193 BITMAP bmp;
92- if (GetObject (hBitmap, sizeof (BITMAP), &bmp)) {
94+ if (GetObject (hBitmap, sizeof (BITMAP), &bmp) && bmp.bmWidth > 0 && bmp.bmHeight > 0 ) {
95+ std::wcerr << L" Bitmap: " << bmp.bmWidth << L" x" << bmp.bmHeight << std::endl;
96+
9397 BITMAPINFO bmi = {};
9498 bmi.bmiHeader .biSize = sizeof (BITMAPINFOHEADER);
9599 bmi.bmiHeader .biWidth = bmp.bmWidth ;
@@ -102,13 +106,26 @@ Napi::Value getImageBuffer(const Napi::CallbackInfo& info, bool useThumbnail) {
102106 std::unique_ptr<BYTE[]> pixels (new BYTE[bmpSize]);
103107
104108 HDC hMemDC = CreateCompatibleDC (nullptr );
105- if (hMemDC && GetDIBits (hMemDC, hBitmap, 0 , bmp.bmHeight , pixels.get (), &bmi, DIB_RGB_COLORS)) {
106- result = Napi::Buffer<BYTE>::Copy (env, pixels.get (), bmpSize);
109+ if (hMemDC) {
110+ int lines = GetDIBits (hMemDC, hBitmap, 0 , bmp.bmHeight , pixels.get (), &bmi, DIB_RGB_COLORS);
111+ if (lines > 0 ) {
112+ std::wcerr << L" GetDIBits returned " << lines << L" scanlines." << std::endl;
113+ result = Napi::Buffer<BYTE>::Copy (env, pixels.get (), bmpSize);
114+ } else {
115+ std::wcerr << L" GetDIBits failed or returned zero lines." << std::endl;
116+ Napi::Error::New (env, " GetDIBits failed" ).ThrowAsJavaScriptException ();
117+ }
118+ DeleteDC (hMemDC);
119+ } else {
120+ Napi::Error::New (env, " CreateCompatibleDC failed" ).ThrowAsJavaScriptException ();
107121 }
108- if (hMemDC) DeleteDC (hMemDC);
122+ } else {
123+ std::wcerr << L" Invalid bitmap dimensions." << std::endl;
124+ Napi::Error::New (env, " Invalid bitmap" ).ThrowAsJavaScriptException ();
109125 }
110126 DeleteObject (hBitmap);
111127 } else {
128+ std::wcerr << L" Failed to get icon/thumbnail. HRESULT=" << hr << std::endl;
112129 Napi::Error::New (env, " Failed to retrieve image" ).ThrowAsJavaScriptException ();
113130 }
114131 } catch (...) {
0 commit comments