跳到主要内容

原生代码与 Electron:C++ (Windows)

本教程基于原生代码与 Electron 的通用介绍构建,重点介绍如何使用 C++ 和 Win32 API 为 Windows 创建原生插件。 为了说明如何在 Electron 应用中嵌入原生 Win32 代码,我们将构建一个基本的 Windows 原生 GUI(使用 Windows 通用控件),该 GUI 将与 Electron 的 JavaScript 进行通信。

具体来说,我们将与两个常用的 Windows 原生库集成

  • comctl32.lib,包含通用控件和用户界面组件。 它提供了各种 UI 元素,例如按钮、滚动条、工具栏、状态栏、进度条和树视图。 就 Windows 上的 GUI 开发而言,这个库是非常底层和基础的 - WinUI 或 WPF 等更现代的框架是更高级的替代方案,但它们需要比本教程中所需的更多 C++ 和 Windows 版本考虑。 通过这种方式,我们可以避免为多个 Windows 版本构建原生界面的许多陷阱!
  • shcore.lib,一个提供高 DPI 感知功能以及围绕管理显示器和 UI 元素的其他 Shell 相关功能的库。

本教程对于已经熟悉 Windows 上的原生 C++ GUI 开发的人员最有用。 您应该有基本的窗口类和过程的经验,例如 WNDCLASSEXWWindowProc 函数。 您还应该熟悉 Windows 消息循环,它是任何原生应用程序的核心 - 我们的代码将使用 GetMessageTranslateMessageDispatchMessage 来处理消息。 最后,我们将使用(但不解释)标准的 Win32 控件,例如 WC_EDITWWC_BUTTONW

信息

如果您不熟悉 Windows 上的 C++ GUI 开发,我们推荐 Microsoft 出色的文档和指南,特别是针对初学者。 "Win32 和 C++ 入门" 是一个很好的介绍。

要求

正如我们的原生代码与 Electron 的通用介绍一样,本教程假定您已安装 Node.js 和 npm,以及编译原生代码所需的基本工具。 由于本教程讨论编写与 Windows 交互的原生代码,因此建议您在 Windows 上按照本教程进行操作,并安装 Visual Studio 和“使用 C++ 进行桌面开发”工作负载。 有关详细信息,请参阅Visual Studio 安装说明

1) 创建一个包

您可以重复使用我们在原生代码与 Electron 教程中创建的包。 本教程将不再重复描述的步骤。 首先让我们设置基本的插件文件夹结构

my-native-win32-addon/
├── binding.gyp
├── include/
│ └── cpp_code.h
├── js/
│ └── index.js
├── package.json
└── src/
├── cpp_addon.cc
└── cpp_code.cc

我们的 package.json 应该看起来像这样

package.json
{
"name": "cpp-win32",
"version": "1.0.0",
"description": "A demo module that exposes C++ code to Electron",
"main": "js/index.js",
"author": "Your Name",
"scripts": {
"clean": "rm -rf build_swift && rm -rf build",
"build-electron": "electron-rebuild",
"build": "node-gyp configure && node-gyp build"
},
"license": "MIT",
"dependencies": {
"bindings": "^1.5.0",
"node-addon-api": "^8.3.0"
}
}

2) 设置构建配置

对于特定于 Windows 的插件,我们需要修改 binding.gyp 文件以包含 Windows 库并设置适当的编译器标志。 简而言之,我们需要做以下三件事

  1. 我们需要确保我们的插件仅在 Windows 上编译,因为我们将编写平台特定的代码。
  2. 我们需要包含 Windows 特定的库。 在我们的教程中,我们将针对 comctl32.libshcore.lib
  3. 我们需要配置编译器并定义 C++ 宏。
binding.gyp
{
"targets": [
{
"target_name": "cpp_addon",
"conditions": [
['OS=="win"', {
"sources": [
"src/cpp_addon.cc",
"src/cpp_code.cc"
],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")",
"include"
],
"libraries": [
"comctl32.lib",
"shcore.lib"
],
"dependencies": [
"<!(node -p \"require('node-addon-api').gyp\")"
],
"msvs_settings": {
"VCCLCompilerTool": {
"ExceptionHandling": 1,
"DebugInformationFormat": "OldStyle",
"AdditionalOptions": [
"/FS"
]
},
"VCLinkerTool": {
"GenerateDebugInformation": "true"
}
},
"defines": [
"NODE_ADDON_API_CPP_EXCEPTIONS",
"WINVER=0x0A00",
"_WIN32_WINNT=0x0A00"
]
}]
]
}
]
}

如果您对这个配置的详细信息感到好奇,您可以继续阅读 - 否则,请随时复制它们并继续下一步,我们将在此处定义 C++ 接口。

Microsoft Visual Studio 构建配置

msvs_settings 提供 Visual Studio 特定的设置。

VCCLCompilerTool 设置

binding.gyp
"VCCLCompilerTool": {
"ExceptionHandling": 1,
"DebugInformationFormat": "OldStyle",
"AdditionalOptions": [
"/FS"
]
}
  • ExceptionHandling: 1:这会使用 /EHsc 编译器标志启用 C++ 异常处理。 这很重要,因为它使编译器能够捕获 C++ 异常,确保异常发生时堆栈正确展开,并且 Node-API 需要它来正确处理 JavaScript 和 C++ 之间的异常。
  • DebugInformationFormat: "OldStyle":这指定调试信息的格式,使用较旧、更兼容的 PDB(程序数据库)格式。 这支持与各种调试工具的兼容性,并且更适用于增量构建。
  • AdditionalOptions: ["/FS"]:这会添加文件序列化标志,强制在编译期间对 PDB 文件进行序列化访问。 它防止在并行构建中多个编译器进程试图访问同一 PDB 文件时出现构建错误。

VCLinkerTool 设置

binding.gyp
"VCLinkerTool": {
"GenerateDebugInformation": "true"
}
  • GenerateDebugInformation: "true":这告诉链接器包含调试信息,这允许在使用符号的工具中进行源代码级调试。 最重要的是,如果插件崩溃,这将允许我们获得人类可读的堆栈跟踪。

预处理器宏 (defines):

  • NODE_ADDON_API_CPP_EXCEPTIONS:此宏在 Node Addon API 中启用 C++ 异常处理。 默认情况下,Node-API 使用返回值错误处理模式,但此定义允许 C++ 包装器抛出和捕获 C++ 异常,这使得代码更符合 C++ 惯例,并且更容易使用。
  • WINVER=0x0A00:这定义了代码目标最低的 Windows 版本。 值 0x0A00 对应于 Windows 10。 设置此项会告诉编译器代码可以使用 Windows 10 中可用的功能,并且不会尝试保持与早期 Windows 版本的向后兼容性。 确保将其设置为您打算使用 Electron 应用支持的最低 Windows 版本。
  • _WIN32_WINNT=0x0A00 - 类似于 WINVER,这定义了代码将运行的 Windows NT 内核的最低版本。 同样,0x0A00 对应于 Windows 10。 通常将其设置为与 WINVER 相同的值。

3) 定义 C++ 接口

我们在 include/cpp_code.h 中定义头文件

include/cpp_code.h
#pragma once
#include <string>
#include <functional>

namespace cpp_code {

std::string hello_world(const std::string& input);
void hello_gui();

// Callback function types
using TodoCallback = std::function<void(const std::string&)>;

// Callback setters
void setTodoAddedCallback(TodoCallback callback);

} // namespace cpp_code

此头文件

  • 包含通用教程中的基本 hello_world 函数
  • 添加 hello_gui 函数以创建 Win32 GUI
  • 定义待办事项操作(添加)的回调类型。 为了让本教程尽量简短,我们将仅实现一个回调。
  • 提供这些回调的设置函数

4) 实现 Win32 GUI 代码

现在,让我们在 src/cpp_code.cc 中实现我们的 Win32 GUI。 这是一个较大的文件,因此我们将分段查看。 首先,让我们包含必要的头文件并定义基本结构。

src/cpp_code.cc
#include <windows.h>
#include <windowsx.h>
#include <string>
#include <functional>
#include <chrono>
#include <vector>
#include <commctrl.h>
#include <shellscalingapi.h>
#include <thread>

#pragma comment(lib, "comctl32.lib")
#pragma comment(linker, "\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

using TodoCallback = std::function<void(const std::string &)>;

static TodoCallback g_todoAddedCallback;

struct TodoItem
{
GUID id;
std::wstring text;
int64_t date;

std::string toJson() const
{
OLECHAR *guidString;
StringFromCLSID(id, &guidString);
std::wstring widGuid(guidString);
CoTaskMemFree(guidString);

// Convert wide string to narrow for JSON
std::string guidStr(widGuid.begin(), widGuid.end());
std::string textStr(text.begin(), text.end());

return "{"
"\"id\":\"" + guidStr + "\","
"\"text\":\"" + textStr + "\","
"\"date\":" + std::to_string(date) +
"}";
}
};

namespace cpp_code
{
// More code to follow later...
}

在此部分

  • 我们包含必要的 Win32 头文件
  • 我们设置 pragma 注释以链接所需的库
  • 我们定义待办事项操作的回调变量
  • 我们创建一个带有转换为 JSON 方法的 TodoItem 结构体

接下来,让我们实现基本函数和辅助方法

src/cpp_code.cc
namespace cpp_code
{
std::string hello_world(const std::string &input)
{
return "Hello from C++! You said: " + input;
}

void setTodoAddedCallback(TodoCallback callback)
{
g_todoAddedCallback = callback;
}

// Window procedure function that handles window messages
// hwnd: Handle to the window
// uMsg: Message code
// wParam: Additional message-specific information
// lParam: Additional message-specific information
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

// Helper function to scale a value based on DPI
int Scale(int value, UINT dpi)
{
return MulDiv(value, dpi, 96); // 96 is the default DPI
}

// Helper function to convert SYSTEMTIME to milliseconds since epoch
int64_t SystemTimeToMillis(const SYSTEMTIME &st)
{
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
ULARGE_INTEGER uli;
uli.LowPart = ft.dwLowDateTime;
uli.HighPart = ft.dwHighDateTime;
return (uli.QuadPart - 116444736000000000ULL) / 10000;
}

// More code to follow later...
}

在此部分中,我们添加了一个函数,该函数允许我们为添加的待办事项设置回调。 我们还添加了两个与 JavaScript 配合使用时所需的辅助函数:一个用于根据显示器的 DPI 缩放我们的 UI 元素 - 另一个用于将 Windows SYSTEMTIME 转换为自纪元以来的毫秒数,这是 JavaScript 跟踪时间的方式。

现在,让我们开始本教程中您可能最感兴趣的部分 - 创建 GUI 线程并在屏幕上绘制原生像素。 我们将通过向 cpp_code 命名空间添加一个 void hello_gui() 函数来完成此操作。 我们需要考虑几点

  • 我们需要为 GUI 创建一个新的线程,以避免阻塞 Node.js 事件循环。 处理 GUI 事件的 Windows 消息循环在一个无限循环中运行,如果它在主线程上运行,将阻止 Node.js 处理其他事件。 通过在单独的线程上运行 GUI,我们可以让原生 Windows 界面和 Node.js 保持响应。 这种分离还有助于防止 GUI 操作需要等待 JavaScript 回调时可能发生的死锁。 对于更简单的 Windows API 交互,您不需要这样做 - 但是由于您需要检查消息循环,因此您确实需要为 GUI 设置自己的线程。
  • 然后,在我们的线程中,我们需要运行一个消息循环来处理任何 Windows 消息。
  • 我们需要设置 DPI 感知以进行适当的显示缩放。
  • 我们需要注册一个窗口类,创建一个窗口,并添加各种 UI 控件。

在下面的代码中,我们尚未添加任何实际控件。 我们在这里故意这样做,以便以更小的部分查看我们添加的代码。

src/cpp_code.cc
void hello_gui() {
// Launch GUI in a separate thread
std::thread guiThread([]() {
// Enable Per-Monitor DPI awareness
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);

// Initialize Common Controls
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_STANDARD_CLASSES | ICC_WIN95_CLASSES;
InitCommonControlsEx(&icex);

// Register window class
WNDCLASSEXW wc = {};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = L"TodoApp";
RegisterClassExW(&wc);

// Get the DPI for the monitor
UINT dpi = GetDpiForSystem();

// Create window
HWND hwnd = CreateWindowExW(
0, L"TodoApp", L"Todo List",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
Scale(500, dpi), Scale(500, dpi),
nullptr, nullptr,
GetModuleHandle(nullptr), nullptr
);

if (hwnd == nullptr) {
return;
}

// Controls go here! The window is currently empty,
// we'll add controls in the next step.

ShowWindow(hwnd, SW_SHOW);

// Message loop
MSG msg = {};
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

// Clean up
DeleteObject(hFont);
});

// Detach the thread so it runs independently
guiThread.detach();
}

现在我们有了线程、窗口和消息循环,我们可以添加一些控件了。 我们在这里所做的没有任何特定于为 Electron 编写 Windows C++ 的内容 - 您只需将下面的代码复制粘贴到 hello_gui() 函数中的 Controls go here! 部分即可。

我们特别添加了按钮、日期选择器和列表。

src/cpp_code.cc
void hello_gui() {
// ...
// All the code above "Controls go here!"

// Create the modern font with DPI-aware size
HFONT hFont = CreateFontW(
-Scale(14, dpi), // Height (scaled)
0, // Width
0, // Escapement
0, // Orientation
FW_NORMAL, // Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // StrikeOut
DEFAULT_CHARSET, // CharSet
OUT_DEFAULT_PRECIS, // OutPrecision
CLIP_DEFAULT_PRECIS, // ClipPrecision
CLEARTYPE_QUALITY, // Quality
DEFAULT_PITCH | FF_DONTCARE, // Pitch and Family
L"Segoe UI" // Font face name
);

// Create input controls with scaled positions and sizes
HWND hEdit = CreateWindowExW(0, WC_EDITW, L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
Scale(10, dpi), Scale(10, dpi),
Scale(250, dpi), Scale(25, dpi),
hwnd, (HMENU)1, GetModuleHandle(nullptr), nullptr);
SendMessageW(hEdit, WM_SETFONT, (WPARAM)hFont, TRUE);

// Create date picker
HWND hDatePicker = CreateWindowExW(0, DATETIMEPICK_CLASSW, L"",
WS_CHILD | WS_VISIBLE | DTS_SHORTDATECENTURYFORMAT,
Scale(270, dpi), Scale(10, dpi),
Scale(100, dpi), Scale(25, dpi),
hwnd, (HMENU)4, GetModuleHandle(nullptr), nullptr);
SendMessageW(hDatePicker, WM_SETFONT, (WPARAM)hFont, TRUE);

HWND hButton = CreateWindowExW(0, WC_BUTTONW, L"Add",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
Scale(380, dpi), Scale(10, dpi),
Scale(50, dpi), Scale(25, dpi),
hwnd, (HMENU)2, GetModuleHandle(nullptr), nullptr);
SendMessageW(hButton, WM_SETFONT, (WPARAM)hFont, TRUE);

HWND hListBox = CreateWindowExW(0, WC_LISTBOXW, L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | LBS_NOTIFY,
Scale(10, dpi), Scale(45, dpi),
Scale(460, dpi), Scale(400, dpi),
hwnd, (HMENU)3, GetModuleHandle(nullptr), nullptr);
SendMessageW(hListBox, WM_SETFONT, (WPARAM)hFont, TRUE);

// Store menu handle in window's user data
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)hContextMenu);

// All the code below "Controls go here!"
// ...
}

既然我们有了允许用户添加待办事项的用户界面,我们需要存储它们 - 并添加一个可能调用我们的 JavaScript 回调的辅助函数。 紧接着 void hello_gui() { ... } 函数下方,我们将添加以下内容

src/cpp_code.cc
  // Global vector to store todos
static std::vector<TodoItem> g_todos;

void NotifyCallback(const TodoCallback &callback, const std::string &json)
{
if (callback)
{
callback(json);
// Process pending messages
MSG msg;
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}

我们还需要一个函数,可以将待办事项转换为可显示的内容。 我们不需要任何花哨的东西 - 给定待办事项的名称和 SYSTEMTIME 时间戳,我们将返回一个简单的字符串。 将其添加到上面函数下方

src/cpp_code.cc
  std::wstring FormatTodoDisplay(const std::wstring &text, const SYSTEMTIME &st)
{
wchar_t dateStr[64];
GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, dateStr, 64);
return text + L" - " + dateStr;
}

当用户添加待办事项时,我们希望将控件重置回空状态。 为此,在刚刚添加的代码下方添加一个辅助函数

src/cpp_code.cc
  void ResetControls(HWND hwnd)
{
HWND hEdit = GetDlgItem(hwnd, 1);
HWND hDatePicker = GetDlgItem(hwnd, 4);
HWND hAddButton = GetDlgItem(hwnd, 2);

// Clear text
SetWindowTextW(hEdit, L"");

// Reset date to current
SYSTEMTIME currentTime;
GetLocalTime(&currentTime);
DateTime_SetSystemtime(hDatePicker, GDT_VALID, &currentTime);
}

然后,我们需要实现窗口过程来处理 Windows 消息。 就像我们这里的许多代码一样,这些代码中很少有特定于 Electron 的内容 - 因此作为 Win32 C++ 开发人员,您会认出这个函数。 唯一独特之处在于我们希望可能将添加的待办事项通知 JavaScript 回调。 我们之前已经实现了 NotifyCallback() 函数,我们将在此处使用它。 将此代码添加到上面函数下方

src/cpp_code.cc
  LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
{
HWND hListBox = GetDlgItem(hwnd, 3);
int cmd = LOWORD(wParam);

switch (cmd)
{
case 2: // Add button
{
wchar_t buffer[256];
GetDlgItemTextW(hwnd, 1, buffer, 256);

if (wcslen(buffer) > 0)
{
SYSTEMTIME st;
HWND hDatePicker = GetDlgItem(hwnd, 4);
DateTime_GetSystemtime(hDatePicker, &st);

TodoItem todo;
CoCreateGuid(&todo.id);
todo.text = buffer;
todo.date = SystemTimeToMillis(st);

g_todos.push_back(todo);

std::wstring displayText = FormatTodoDisplay(buffer, st);
SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)displayText.c_str());

ResetControls(hwnd);
NotifyCallback(g_todoAddedCallback, todo.toJson());
}
break;
}
}
break;
}

case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}

return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}

我们现在已经成功实现了 Win32 C++ 代码。 其中大部分应该看起来和您编写的无论有没有 Electron 的代码一样。 在下一步中,我们将构建 C++ 和 JavaScript 之间的桥接。 以下是完整的实现

src/cpp_code.cc
#include <windows.h>
#include <windowsx.h>
#include <string>
#include <functional>
#include <chrono>
#include <vector>
#include <commctrl.h>
#include <shellscalingapi.h>
#include <thread>

#pragma comment(lib, "comctl32.lib")
#pragma comment(linker, "\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

using TodoCallback = std::function<void(const std::string &)>;

static TodoCallback g_todoAddedCallback;
static TodoCallback g_todoUpdatedCallback;
static TodoCallback g_todoDeletedCallback;

struct TodoItem
{
GUID id;
std::wstring text;
int64_t date;

std::string toJson() const
{
OLECHAR *guidString;
StringFromCLSID(id, &guidString);
std::wstring widGuid(guidString);
CoTaskMemFree(guidString);

// Convert wide string to narrow for JSON
std::string guidStr(widGuid.begin(), widGuid.end());
std::string textStr(text.begin(), text.end());

return "{"
"\"id\":\"" + guidStr + "\","
"\"text\":\"" + textStr + "\","
"\"date\":" + std::to_string(date) +
"}";
}
};

namespace cpp_code
{

std::string hello_world(const std::string &input)
{
return "Hello from C++! You said: " + input;
}

void setTodoAddedCallback(TodoCallback callback)
{
g_todoAddedCallback = callback;
}

void setTodoUpdatedCallback(TodoCallback callback)
{
g_todoUpdatedCallback = callback;
}

void setTodoDeletedCallback(TodoCallback callback)
{
g_todoDeletedCallback = callback;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

// Helper function to scale a value based on DPI
int Scale(int value, UINT dpi)
{
return MulDiv(value, dpi, 96); // 96 is the default DPI
}

// Helper function to convert SYSTEMTIME to milliseconds since epoch
int64_t SystemTimeToMillis(const SYSTEMTIME &st)
{
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
ULARGE_INTEGER uli;
uli.LowPart = ft.dwLowDateTime;
uli.HighPart = ft.dwHighDateTime;
return (uli.QuadPart - 116444736000000000ULL) / 10000;
}

void ResetControls(HWND hwnd)
{
HWND hEdit = GetDlgItem(hwnd, 1);
HWND hDatePicker = GetDlgItem(hwnd, 4);
HWND hAddButton = GetDlgItem(hwnd, 2);

// Clear text
SetWindowTextW(hEdit, L"");

// Reset date to current
SYSTEMTIME currentTime;
GetLocalTime(&currentTime);
DateTime_SetSystemtime(hDatePicker, GDT_VALID, &currentTime);
}

void hello_gui() {
// Launch GUI in a separate thread
std::thread guiThread([]() {
// Enable Per-Monitor DPI awareness
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);

// Initialize Common Controls
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_STANDARD_CLASSES | ICC_WIN95_CLASSES;
InitCommonControlsEx(&icex);

// Register window class
WNDCLASSEXW wc = {};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = L"TodoApp";
RegisterClassExW(&wc);

// Get the DPI for the monitor
UINT dpi = GetDpiForSystem();

// Create window
HWND hwnd = CreateWindowExW(
0, L"TodoApp", L"Todo List",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
Scale(500, dpi), Scale(500, dpi),
nullptr, nullptr,
GetModuleHandle(nullptr), nullptr
);

if (hwnd == nullptr) {
return;
}

// Create the modern font with DPI-aware size
HFONT hFont = CreateFontW(
-Scale(14, dpi), // Height (scaled)
0, // Width
0, // Escapement
0, // Orientation
FW_NORMAL, // Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // StrikeOut
DEFAULT_CHARSET, // CharSet
OUT_DEFAULT_PRECIS, // OutPrecision
CLIP_DEFAULT_PRECIS, // ClipPrecision
CLEARTYPE_QUALITY, // Quality
DEFAULT_PITCH | FF_DONTCARE, // Pitch and Family
L"Segoe UI" // Font face name
);

// Create input controls with scaled positions and sizes
HWND hEdit = CreateWindowExW(0, WC_EDITW, L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
Scale(10, dpi), Scale(10, dpi),
Scale(250, dpi), Scale(25, dpi),
hwnd, (HMENU)1, GetModuleHandle(nullptr), nullptr);
SendMessageW(hEdit, WM_SETFONT, (WPARAM)hFont, TRUE);

// Create date picker
HWND hDatePicker = CreateWindowExW(0, DATETIMEPICK_CLASSW, L"",
WS_CHILD | WS_VISIBLE | DTS_SHORTDATECENTURYFORMAT,
Scale(270, dpi), Scale(10, dpi),
Scale(100, dpi), Scale(25, dpi),
hwnd, (HMENU)4, GetModuleHandle(nullptr), nullptr);
SendMessageW(hDatePicker, WM_SETFONT, (WPARAM)hFont, TRUE);

HWND hButton = CreateWindowExW(0, WC_BUTTONW, L"Add",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
Scale(380, dpi), Scale(10, dpi),
Scale(50, dpi), Scale(25, dpi),
hwnd, (HMENU)2, GetModuleHandle(nullptr), nullptr);
SendMessageW(hButton, WM_SETFONT, (WPARAM)hFont, TRUE);

HWND hListBox = CreateWindowExW(0, WC_LISTBOXW, L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | LBS_NOTIFY,
Scale(10, dpi), Scale(45, dpi),
Scale(460, dpi), Scale(400, dpi),
hwnd, (HMENU)3, GetModuleHandle(nullptr), nullptr);
SendMessageW(hListBox, WM_SETFONT, (WPARAM)hFont, TRUE);

ShowWindow(hwnd, SW_SHOW);

// Message loop
MSG msg = {};
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

// Clean up
DeleteObject(hFont);
});

// Detach the thread so it runs independently
guiThread.detach();
}

// Global vector to store todos
static std::vector<TodoItem> g_todos;

void NotifyCallback(const TodoCallback &callback, const std::string &json)
{
if (callback)
{
callback(json);
// Process pending messages
MSG msg;
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}

std::wstring FormatTodoDisplay(const std::wstring &text, const SYSTEMTIME &st)
{
wchar_t dateStr[64];
GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, dateStr, 64);
return text + L" - " + dateStr;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
{
HWND hListBox = GetDlgItem(hwnd, 3);
int cmd = LOWORD(wParam);

switch (cmd)
{
case 2: // Add button
{
wchar_t buffer[256];
GetDlgItemTextW(hwnd, 1, buffer, 256);

if (wcslen(buffer) > 0)
{
SYSTEMTIME st;
HWND hDatePicker = GetDlgItem(hwnd, 4);
DateTime_GetSystemtime(hDatePicker, &st);

TodoItem todo;
CoCreateGuid(&todo.id);
todo.text = buffer;
todo.date = SystemTimeToMillis(st);

g_todos.push_back(todo);

std::wstring displayText = FormatTodoDisplay(buffer, st);
SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)displayText.c_str());

ResetControls(hwnd);
NotifyCallback(g_todoAddedCallback, todo.toJson());
}
break;
}
}
break;
}

case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}

return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}

} // namespace cpp_code

5) 创建 Node.js 插件桥接

现在,让我们在 src/cpp_addon.cc 中实现 C++ 代码和 Node.js 之间的桥接。 我们首先创建一个插件的基本骨架

src/cpp_addon.cc
#include <napi.h>
#include <string>
#include "cpp_code.h"

Napi::Object Init(Napi::Env env, Napi::Object exports) {
// We'll add code here later
return exports;
}

NODE_API_MODULE(cpp_addon, Init)

这是使用 node-addon-api 创建 Node.js 插件所需的最少结构。 Init 函数在加载插件时调用,NODE_API_MODULE 宏注册我们的初始化程序。

创建一个类来封装我们的 C++ 代码

让我们创建一个类来封装我们的 C++ 代码并将其暴露给 JavaScript

src/cpp_addon.cc
#include <napi.h>
#include <string>
#include "cpp_code.h"

class CppAddon : public Napi::ObjectWrap<CppAddon> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "CppWin32Addon", {
// We'll add methods here later
});

Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);

exports.Set("CppWin32Addon", func);
return exports;
}

CppAddon(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<CppAddon>(info) {
// Constructor logic will go here
}

private:
// Will add private members and methods later
};

Napi::Object Init(Napi::Env env, Napi::Object exports) {
return CppAddon::Init(env, exports);
}

NODE_API_MODULE(cpp_addon, Init)

这创建了一个继承自 Napi::ObjectWrap 的类,它允许我们封装 C++ 对象以在 JavaScript 中使用。 Init 函数设置类并将其导出到 JavaScript。

实现基本功能 - HelloWorld

现在,让我们添加我们的第一个方法,即 HelloWorld 函数

src/cpp_addon.cc
// ... previous code

class CppAddon : public Napi::ObjectWrap<CppAddon> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "CppWin32Addon", {
InstanceMethod("helloWorld", &CppAddon::HelloWorld),
});

// ... rest of Init function
}

CppAddon(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<CppAddon>(info) {
// Constructor logic will go here
}

private:
Napi::Value HelloWorld(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 1 || !info[0].IsString()) {
Napi::TypeError::New(env, "Expected string argument").ThrowAsJavaScriptException();
return env.Null();
}

std::string input = info[0].As<Napi::String>();
std::string result = cpp_code::hello_world(input);

return Napi::String::New(env, result);
}
};

// ... rest of the file

这会将 HelloWorld 方法添加到我们的类中,并使用 DefineClass 注册它。 该方法验证输入,调用我们的 C++ 函数,并将结果返回给 JavaScript。

src/cpp_addon.cc
// ... previous code

class CppAddon : public Napi::ObjectWrap<CppAddon> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "CppWin32Addon", {
InstanceMethod("helloWorld", &CppAddon::HelloWorld),
InstanceMethod("helloGui", &CppAddon::HelloGui),
});

// ... rest of Init function
}

// ... constructor

private:
// ... HelloWorld method

void HelloGui(const Napi::CallbackInfo& info) {
cpp_code::hello_gui();
}
};

// ... rest of the file

这个简单的方法调用了 C++ 代码中的 hello_gui 函数,该函数会在一个单独的线程中启动 Win32 GUI 窗口。

设置事件系统

现在到了复杂的部分 - 设置事件系统,以便我们的 C++ 代码可以回调 JavaScript。 我们需要

  1. 添加私有成员以存储回调
  2. 创建线程安全函数以进行跨线程通信
  3. 添加 On 方法以注册 JavaScript 回调
  4. 设置将触发 JavaScript 回调的 C++ 回调
src/cpp_addon.cc
// ... previous code

class CppAddon : public Napi::ObjectWrap<CppAddon> {
public:
// ... previous public methods

private:
Napi::Env env_;
Napi::ObjectReference emitter;
Napi::ObjectReference callbacks;
napi_threadsafe_function tsfn_;

// ... existing private methods
};

// ... rest of the file

现在,让我们增强构造函数以初始化这些成员

src/cpp_addon.cc
// ... previous code

class CppAddon : public Napi::ObjectWrap<CppAddon> {
public:
// CallbackData struct to pass data between threads
struct CallbackData {
std::string eventType;
std::string payload;
CppAddon* addon;
};

CppAddon(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<CppAddon>(info)
, env_(info.Env())
, emitter(Napi::Persistent(Napi::Object::New(info.Env())))
, callbacks(Napi::Persistent(Napi::Object::New(info.Env())))
, tsfn_(nullptr) {

// We'll add threadsafe function setup here in the next step
}

// Add destructor to clean up
~CppAddon() {
if (tsfn_ != nullptr) {
napi_release_threadsafe_function(tsfn_, napi_tsfn_release);
tsfn_ = nullptr;
}
}

// ... rest of the class
};

// ... rest of the file

现在让我们在构造函数中添加线程安全函数设置

src/cpp_addon.cc
// ... existing constructor code
CppAddon(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<CppAddon>(info)
, env_(info.Env())
, emitter(Napi::Persistent(Napi::Object::New(info.Env())))
, callbacks(Napi::Persistent(Napi::Object::New(info.Env())))
, tsfn_(nullptr) {

napi_status status = napi_create_threadsafe_function(
env_,
nullptr,
nullptr,
Napi::String::New(env_, "CppCallback"),
0,
1,
nullptr,
nullptr,
this,
[](napi_env env, napi_value js_callback, void* context, void* data) {
auto* callbackData = static_cast<CallbackData*>(data);
if (!callbackData) return;

Napi::Env napi_env(env);
Napi::HandleScope scope(napi_env);

auto addon = static_cast<CppAddon*>(context);
if (!addon) {
delete callbackData;
return;
}

try {
auto callback = addon->callbacks.Value().Get(callbackData->eventType).As<Napi::Function>();
if (callback.IsFunction()) {
callback.Call(addon->emitter.Value(), {Napi::String::New(napi_env, callbackData->payload)});
}
} catch (...) {}

delete callbackData;
},
&tsfn_
);

if (status != napi_ok) {
Napi::Error::New(env_, "Failed to create threadsafe function").ThrowAsJavaScriptException();
return;
}

// We'll add callback setup in the next step
}

这创建了一个线程安全函数,允许 C++ 代码从任何线程调用 JavaScript。 调用时,它会检索适当的 JavaScript 回调,并使用提供的负载调用它。

现在让我们添加回调设置

src/cpp_addon.cc
// ... existing constructor code after threadsafe function setup

// Set up the callbacks here
auto makeCallback = [this](const std::string& eventType) {
return [this, eventType](const std::string& payload) {
if (tsfn_ != nullptr) {
auto* data = new CallbackData{
eventType,
payload,
this
};
napi_call_threadsafe_function(tsfn_, data, napi_tsfn_blocking);
}
};
};

cpp_code::setTodoAddedCallback(makeCallback("todoAdded"));

这创建了一个为每种事件类型生成回调的函数。 回调捕获事件类型,并在调用时创建一个 CallbackData 对象并将其传递给我们的线程安全函数。

最后,让我们添加 On 方法以允许 JavaScript 注册特定事件类型的回调函数

src/cpp_addon.cc
// ... in the class definition, add On to DefineClass
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "CppWin32Addon", {
InstanceMethod("helloWorld", &CppAddon::HelloWorld),
InstanceMethod("helloGui", &CppAddon::HelloGui),
InstanceMethod("on", &CppAddon::On)
});

// ... rest of Init function
}

// ... and add the implementation in the private section
Napi::Value On(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 2 || !info[0].IsString() || !info[1].IsFunction()) {
Napi::TypeError::New(env, "Expected (string, function) arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

callbacks.Value().Set(info[0].As<Napi::String>(), info[1].As<Napi::Function>());
return env.Undefined();
}

这允许 JavaScript 为特定事件类型注册回调。

整合桥接代码

现在我们已经具备了所有部分。

以下是完整的实现

src/cpp_addon.cc
#include <napi.h>
#include <string>
#include "cpp_code.h"

class CppAddon : public Napi::ObjectWrap<CppAddon> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "CppWin32Addon", {
InstanceMethod("helloWorld", &CppAddon::HelloWorld),
InstanceMethod("helloGui", &CppAddon::HelloGui),
InstanceMethod("on", &CppAddon::On)
});

Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);

exports.Set("CppWin32Addon", func);
return exports;
}

struct CallbackData {
std::string eventType;
std::string payload;
CppAddon* addon;
};

CppAddon(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<CppAddon>(info)
, env_(info.Env())
, emitter(Napi::Persistent(Napi::Object::New(info.Env())))
, callbacks(Napi::Persistent(Napi::Object::New(info.Env())))
, tsfn_(nullptr) {

napi_status status = napi_create_threadsafe_function(
env_,
nullptr,
nullptr,
Napi::String::New(env_, "CppCallback"),
0,
1,
nullptr,
nullptr,
this,
[](napi_env env, napi_value js_callback, void* context, void* data) {
auto* callbackData = static_cast<CallbackData*>(data);
if (!callbackData) return;

Napi::Env napi_env(env);
Napi::HandleScope scope(napi_env);

auto addon = static_cast<CppAddon*>(context);
if (!addon) {
delete callbackData;
return;
}

try {
auto callback = addon->callbacks.Value().Get(callbackData->eventType).As<Napi::Function>();
if (callback.IsFunction()) {
callback.Call(addon->emitter.Value(), {Napi::String::New(napi_env, callbackData->payload)});
}
} catch (...) {}

delete callbackData;
},
&tsfn_
);

if (status != napi_ok) {
Napi::Error::New(env_, "Failed to create threadsafe function").ThrowAsJavaScriptException();
return;
}

// Set up the callbacks here
auto makeCallback = [this](const std::string& eventType) {
return [this, eventType](const std::string& payload) {
if (tsfn_ != nullptr) {
auto* data = new CallbackData{
eventType,
payload,
this
};
napi_call_threadsafe_function(tsfn_, data, napi_tsfn_blocking);
}
};
};

cpp_code::setTodoAddedCallback(makeCallback("todoAdded"));
}

~CppAddon() {
if (tsfn_ != nullptr) {
napi_release_threadsafe_function(tsfn_, napi_tsfn_release);
tsfn_ = nullptr;
}
}

private:
Napi::Env env_;
Napi::ObjectReference emitter;
Napi::ObjectReference callbacks;
napi_threadsafe_function tsfn_;

Napi::Value HelloWorld(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 1 || !info[0].IsString()) {
Napi::TypeError::New(env, "Expected string argument").ThrowAsJavaScriptException();
return env.Null();
}

std::string input = info[0].As<Napi::String>();
std::string result = cpp_code::hello_world(input);

return Napi::String::New(env, result);
}

void HelloGui(const Napi::CallbackInfo& info) {
cpp_code::hello_gui();
}

Napi::Value On(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 2 || !info[0].IsString() || !info[1].IsFunction()) {
Napi::TypeError::New(env, "Expected (string, function) arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

callbacks.Value().Set(info[0].As<Napi::String>(), info[1].As<Napi::Function>());
return env.Undefined();
}
};

Napi::Object Init(Napi::Env env, Napi::Object exports) {
return CppAddon::Init(env, exports);
}

NODE_API_MODULE(cpp_addon, Init)

6) 创建 JavaScript 封装器

最后,我们在 js/index.js 中添加一个 JavaScript 封装器。 正如我们都看到的,C++ 需要大量的样板代码,这些代码可能更容易或更快地在 JavaScript 中编写 - 您会发现许多生产应用程序在调用原生代码之前会在 JavaScript 中转换数据或请求。 例如,我们将时间戳转换为一个适当的 JavaScript 日期。

js/index.js
const EventEmitter = require('events')

class CppWin32Addon extends EventEmitter {
constructor() {
super()

if (process.platform !== 'win32') {
throw new Error('This module is only available on Windows')
}

const native = require('bindings')('cpp_addon')
this.addon = new native.CppWin32Addon();

this.addon.on('todoAdded', (payload) => {
this.emit('todoAdded', this.#parse(payload))
});

this.addon.on('todoUpdated', (payload) => {
this.emit('todoUpdated', this.#parse(payload))
});

this.addon.on('todoDeleted', (payload) => {
this.emit('todoDeleted', this.#parse(payload))
});
}

helloWorld(input = "") {
return this.addon.helloWorld(input)
}

helloGui() {
this.addon.helloGui()
}

#parse(payload) {
const parsed = JSON.parse(payload)

return { ...parsed, date: new Date(parsed.date) }
}
}

if (process.platform === 'win32') {
module.exports = new CppWin32Addon()
} else {
module.exports = {}
}

7) 构建和测试插件

所有文件到位后,您可以构建插件

npm run build

结论

您现在已经使用 C++ 和 Win32 API 为 Windows 构建了一个完整的 Node.js 原生插件。 我们在这里做的一些事情包括

  1. 从 C++ 创建原生 Windows GUI
  2. 实现具有添加、编辑和删除功能的待办事项列表应用程序
  3. C++ 和 JavaScript 之间的双向通信
  4. 使用 Win32 控件和 Windows 特定功能
  5. 从 C++ 线程安全地回调到 JavaScript

这为您在 Electron 应用中构建更复杂的 Windows 特定功能提供了基础,让您两全其美:web 技术的便捷性与原生代码的强大功能。

有关使用 Win32 API 的更多信息,请参阅Microsoft C++、C 和汇编程序文档Windows API 参考