Skip to content

Commit c55484e

Browse files
Copilotlijy91
andcommitted
Fix Windows compilation errors - string encoding, API calls, and syntax
Co-authored-by: lijy91 <3889523+lijy91@users.noreply.github.com>
1 parent a42bb50 commit c55484e

7 files changed

Lines changed: 32 additions & 25 deletions

src/keyboard_monitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class KeyboardMonitor {
6464

6565
private:
6666
class Impl;
67+
friend class Impl; // Allow Impl class to access private members
6768
std::unique_ptr<Impl> impl_;
6869
KeyboardEventHandler* event_handler_;
6970
};

src/platform/windows/accessibility_manager_windows.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#include <windows.h>
44
#include <oleacc.h>
55

6+
// SM_SCREENREADER is not defined in all Windows SDK versions
7+
#ifndef SM_SCREENREADER
8+
#define SM_SCREENREADER 40
9+
#endif
10+
611
namespace nativeapi {
712

813
void AccessibilityManager::Enable() {

src/platform/windows/display_manager_windows.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ UINT GetMonitorDpi(HMONITOR monitor) {
1313
typedef HRESULT(WINAPI* GetDpiForMonitorFunc)(HMONITOR, int, UINT*, UINT*);
1414

1515
// Try to load GetDpiForMonitor from Shcore.dll (Windows 8.1+)
16-
HMODULE shcore = LoadLibrary(L"Shcore.dll");
16+
HMODULE shcore = LoadLibraryW(L"Shcore.dll");
1717
if (shcore) {
1818
GetDpiForMonitorFunc getDpiForMonitor =
1919
(GetDpiForMonitorFunc)GetProcAddress(shcore, "GetDpiForMonitor");
@@ -54,7 +54,7 @@ Display CreateDisplayFromMonitor(HMONITOR monitor, bool isPrimary) {
5454
UINT dpi = GetMonitorDpi(monitor);
5555

5656
Display display;
57-
display.name = WideStringToString(std::wstring(info.szDevice));
57+
display.name = std::string(info.szDevice);
5858
display.scaleFactor = static_cast<double>(dpi) / kBaseDpi;
5959
display.isPrimary = isPrimary;
6060

@@ -74,7 +74,7 @@ Display CreateDisplayFromMonitor(HMONITOR monitor, bool isPrimary) {
7474
DISPLAY_DEVICE displayDevice;
7575
displayDevice.cb = sizeof(DISPLAY_DEVICE);
7676
if (EnumDisplayDevices(info.szDevice, 0, &displayDevice, 0)) {
77-
display.id = WideStringToString(std::wstring(displayDevice.DeviceID));
77+
display.id = std::string(displayDevice.DeviceID);
7878

7979
// Get display settings
8080
DEVMODE devMode;

src/platform/windows/menu_windows.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void MenuItem::SetIcon(std::string icon) {
8383
} else if (!icon.empty()) {
8484
// Load icon from file path
8585
std::wstring wicon(icon.begin(), icon.end());
86-
HICON hIcon = (HICON)LoadImage(nullptr, wicon.c_str(), IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
86+
HICON hIcon = (HICON)LoadImageW(nullptr, wicon.c_str(), IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
8787
if (hIcon) {
8888
// Convert HICON to HBITMAP
8989
HDC hdc = GetDC(nullptr);
@@ -151,8 +151,8 @@ void Menu::AddItem(MenuItem item) {
151151
// Store the item to keep it alive
152152
pimpl_->items_.push_back(item);
153153

154-
MENUITEMINFO mii = {};
155-
mii.cbSize = sizeof(MENUITEMINFO);
154+
MENUITEMINFOW mii = {};
155+
mii.cbSize = sizeof(MENUITEMINFOW);
156156
mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA;
157157
mii.wID = item.pimpl_->menu_id_;
158158

@@ -167,7 +167,7 @@ void Menu::AddItem(MenuItem item) {
167167
mii.hbmpItem = item.pimpl_->icon_bitmap_;
168168
}
169169

170-
InsertMenuItem(pimpl_->hmenu_, GetMenuItemCount(pimpl_->hmenu_), TRUE, &mii);
170+
InsertMenuItemW(pimpl_->hmenu_, GetMenuItemCount(pimpl_->hmenu_), TRUE, &mii);
171171
}
172172

173173
void Menu::RemoveItem(MenuItem item) {
@@ -188,12 +188,12 @@ void Menu::RemoveItem(MenuItem item) {
188188
void Menu::AddSeparator() {
189189
if (!pimpl_->hmenu_) return;
190190

191-
MENUITEMINFO mii = {};
192-
mii.cbSize = sizeof(MENUITEMINFO);
191+
MENUITEMINFOW mii = {};
192+
mii.cbSize = sizeof(MENUITEMINFOW);
193193
mii.fMask = MIIM_FTYPE;
194194
mii.fType = MFT_SEPARATOR;
195195

196-
InsertMenuItem(pimpl_->hmenu_, GetMenuItemCount(pimpl_->hmenu_), TRUE, &mii);
196+
InsertMenuItemW(pimpl_->hmenu_, GetMenuItemCount(pimpl_->hmenu_), TRUE, &mii);
197197
}
198198

199199
MenuItem Menu::CreateItem(std::string title) {

src/platform/windows/tray_windows.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ class Tray::Impl {
9999
// Register window class if not already registered
100100
static bool class_registered = false;
101101
if (!class_registered) {
102-
WNDCLASS wc = {};
102+
WNDCLASSW wc = {};
103103
wc.lpfnWndProc = TrayWindowProc;
104104
wc.hInstance = GetModuleHandle(nullptr);
105105
wc.lpszClassName = TRAY_WINDOW_CLASS;
106-
RegisterClass(&wc);
106+
RegisterClassW(&wc);
107107
class_registered = true;
108108
}
109109

110110
// Create hidden window for tray messages
111-
hwnd_ = CreateWindow(
111+
hwnd_ = CreateWindowW(
112112
TRAY_WINDOW_CLASS,
113113
L"TrayWindow",
114114
WS_OVERLAPPED,
@@ -122,13 +122,13 @@ class Tray::Impl {
122122
void InitializeTrayIcon() {
123123
if (!hwnd_) return;
124124

125-
nid_.cbSize = sizeof(NOTIFYICONDATA);
125+
nid_.cbSize = sizeof(NOTIFYICONDATAW);
126126
nid_.hWnd = hwnd_;
127127
nid_.uID = tray_uid_;
128128
nid_.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
129129
nid_.uCallbackMessage = g_tray_message;
130130
nid_.hIcon = LoadIcon(nullptr, IDI_APPLICATION); // Default icon
131-
wcscpy_s(nid_.szTip, L"Tray Icon");
131+
wcscpy_s(nid_.szTip, sizeof(nid_.szTip)/sizeof(wchar_t), L"Tray Icon");
132132

133133
// Store tray pointer in window
134134
SetWindowLongPtr(hwnd_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(tray_ptr_));
@@ -153,15 +153,14 @@ class Tray::Impl {
153153
if (!hwnd_) return;
154154

155155
std::wstring wtooltip = StringToWideString(tooltip);
156-
wcsncpy_s(nid_.szTip, wtooltip.c_str(), sizeof(nid_.szTip) / sizeof(wchar_t) - 1);
157-
nid_.szTip[sizeof(nid_.szTip) / sizeof(wchar_t) - 1] = L'\0';
156+
wcsncpy_s(nid_.szTip, sizeof(nid_.szTip) / sizeof(wchar_t), wtooltip.c_str(), _TRUNCATE);
158157

159158
Shell_NotifyIcon(NIM_MODIFY, &nid_);
160159
}
161160

162161
HWND hwnd_;
163162
UINT tray_uid_;
164-
NOTIFYICONDATA nid_;
163+
NOTIFYICONDATAW nid_;
165164
HICON icon_;
166165
std::string title_;
167166
std::string tooltip_;
@@ -200,7 +199,7 @@ void Tray::SetIcon(std::string icon) {
200199
} else if (!icon.empty()) {
201200
// Load icon from file path
202201
std::wstring wicon = StringToWideString(icon);
203-
pimpl_->icon_ = (HICON)LoadImage(
202+
pimpl_->icon_ = (HICON)LoadImageW(
204203
nullptr,
205204
wicon.c_str(),
206205
IMAGE_ICON,
@@ -246,9 +245,9 @@ Rectangle Tray::GetBounds() {
246245

247246
if (pimpl_->hwnd_) {
248247
// Get the position of the notification area
249-
HWND hTray = FindWindow(L"Shell_TrayWnd", nullptr);
248+
HWND hTray = FindWindowW(L"Shell_TrayWnd", nullptr);
250249
if (hTray) {
251-
HWND hNotify = FindWindowEx(hTray, nullptr, L"TrayNotifyWnd", nullptr);
250+
HWND hNotify = FindWindowExW(hTray, nullptr, L"TrayNotifyWnd", nullptr);
252251
if (hNotify) {
253252
RECT rect;
254253
GetWindowRect(hNotify, &rect);

src/platform/windows/window_manager_windows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
4242
if (!(exStyle & WS_EX_TOOLWINDOW)) {
4343
// Check if window has a title
4444
wchar_t title[256];
45-
if (GetWindowText(hwnd, title, sizeof(title) / sizeof(wchar_t)) > 0) {
45+
if (GetWindowTextW(hwnd, title, sizeof(title) / sizeof(wchar_t)) > 0) {
4646
windows->push_back(hwnd);
4747
}
4848
}

src/platform/windows/window_windows.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ Point Window::GetPosition() {
442442
void Window::SetTitle(std::string title) {
443443
if (pimpl_->hwnd_) {
444444
std::wstring wtitle = StringToWideString(title);
445-
SetWindowText(pimpl_->hwnd_, wtitle.c_str());
445+
SetWindowTextW(pimpl_->hwnd_, wtitle.c_str());
446446
}
447447
}
448448

@@ -451,7 +451,7 @@ std::string Window::GetTitle() {
451451
int length = GetWindowTextLength(pimpl_->hwnd_);
452452
if (length > 0) {
453453
std::wstring wtitle(length + 1, L'\0');
454-
GetWindowText(pimpl_->hwnd_, &wtitle[0], length + 1);
454+
GetWindowTextW(pimpl_->hwnd_, &wtitle[0], length + 1);
455455
wtitle.resize(length);
456456
return WideStringToString(wtitle);
457457
}
@@ -571,4 +571,6 @@ void Window::StartResizing() {
571571
void* Window::GetNSWindow() const {
572572
// This method name suggests macOS compatibility - return HWND instead
573573
return pimpl_->hwnd_;
574-
}
574+
}
575+
576+
} // namespace nativeapi

0 commit comments

Comments
 (0)