本文最后更新于 2026年5月11日。
最后代码
// main.cpp - 完全调试通过的Everything搜索工具
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#include <string>
#include <map>
#include "Everything.h"
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "Everything64.lib") // 根据平台选择
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
// 全局变量
HWND hMainWnd, hSearchBox, hResultsList;
HFONT hFont;
const UINT IPC_MSG_ID = WM_APP + 1;
// 函数声明
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void PerformSearch(const wchar_t* searchTerm);
void HandleSearchResults(DWORD errorCode = 0);
void OpenSelectedFile();
bool InitializeEverythingIPC(HWND hWnd);
void DebugPrint(const wchar_t* format, ...);
// 程序入口
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) {
// 初始化公共控件
INITCOMMONCONTROLSEX icc = { sizeof(icc), ICC_STANDARD_CLASSES };
InitCommonControlsEx(&icc);
// 注册窗口类
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = L"QuickSearchClass";
if (!RegisterClassEx(&wcex)) return 1;
// 创建主窗口
hMainWnd = CreateWindowEx(
WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
L"QuickSearchClass",
L"Quick Search",
WS_POPUP | WS_CAPTION | WS_SYSMENU,
100, 100, 500, 600,
nullptr, nullptr, hInstance, nullptr);
if (!hMainWnd) return FALSE;
// 创建控件
hSearchBox = CreateWindowEx(
WS_EX_CLIENTEDGE, L"EDIT", L"",
WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
10, 10, 460, 30,
hMainWnd, nullptr, hInstance, nullptr);
hResultsList = CreateWindowEx(
WS_EX_CLIENTEDGE, L"LISTBOX", L"",
WS_CHILD | WS_VISIBLE | LBS_NOTIFY | WS_VSCROLL | WS_BORDER | LBS_HASSTRINGS,
10, 50, 460, 520,
hMainWnd, nullptr, hInstance, nullptr);
// 设置字体
hFont = CreateFont(18, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, L"Segoe UI");
SendMessage(hSearchBox, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hResultsList, WM_SETFONT, (WPARAM)hFont, TRUE);
// 初始化Everything连接
if (!InitializeEverythingIPC(hMainWnd)) {
MessageBox(hMainWnd,
L"无法连接Everything服务\n"
L"1. 请确保Everything正在运行\n"
L"2. 右键Everything快捷方式→以管理员身份运行",
L"初始化错误", MB_ICONERROR);
return 1;
}
// 注册全局快捷键(Alt+Space)
if (!RegisterHotKey(hMainWnd, 1, MOD_ALT, VK_SPACE)) {
DebugPrint(L"注册热键失败: %d", GetLastError());
}
// 显示窗口
ShowWindow(hMainWnd, nCmdShow);
UpdateWindow(hMainWnd);
// 消息循环
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// 清理资源
Everything_CleanUp();
DeleteObject(hFont);
return (int)msg.wParam;
}
// 调试输出
void DebugPrint(const wchar_t* format, ...) {
wchar_t buffer[512];
va_list args;
va_start(args, format);
vswprintf_s(buffer, format, args);
va_end(args);
OutputDebugStringW(buffer);
}
// 初始化IPC连接
bool InitializeEverythingIPC(HWND hWnd) {
// 重置连接
Everything_CleanUp();
Sleep(50);
// 设置IPC参数
Everything_SetReplyWindow(hWnd);
Everything_SetReplyID(IPC_MSG_ID);
// 验证连接状态
if (!Everything_IsAdmin()) {
DebugPrint(L"Everything未以管理员权限运行");
return false;
}
//if (!Everything_IsDBLoaded()) {
// DebugPrint(L"数据库未就绪 (进度: %d%%)",
// Everything_GetDBProgress() * 100 / 0x10000);
// return false;
//}
// 测试查询
Everything_SetSearchW(L"test");
if (!Everything_QueryW(FALSE)) {
DWORD err = Everything_GetLastError();
DebugPrint(L"连接测试失败 (错误: 0x%X)", err);
return false;
}
DebugPrint(L"IPC初始化成功 (版本: %d.%d)",
Everything_GetMajorVersion(),
Everything_GetMinorVersion());
return true;
}
// 执行搜索
void PerformSearch(const wchar_t* searchTerm) {
// 空搜索处理
if (wcslen(searchTerm) == 0) {
SendMessage(hResultsList, LB_RESETCONTENT, 0, 0);
return;
}
// 检查连接状态
if (!Everything_IsAdmin() || !Everything_IsDBLoaded()) {
if (!InitializeEverythingIPC(hMainWnd)) {
HandleSearchResults(0x80004005); // E_ACCESSDENIED
return;
}
}
// 执行搜索(最多重试2次)
for (int i = 0; i < 2; ++i) {
Everything_SetSearchW(searchTerm);
if (Everything_QueryW(TRUE)) break;
DWORD err = Everything_GetLastError();
if (i == 1) {
HandleSearchResults(err);
return;
}
DebugPrint(L"搜索失败,尝试重新连接 (错误: 0x%X)", err);
Everything_CleanUp();
Sleep(100);
}
}
// 处理搜索结果
void HandleSearchResults(DWORD errorCode) {
SendMessage(hResultsList, LB_RESETCONTENT, 0, 0);
if (errorCode != 0) {
static const std::map<DWORD, const wchar_t*> errorMessages = {
{0x80004005, L"错误: 拒绝访问 (请用管理员权限运行Everything)"},
{0x80070005, L"错误: 访问被拒绝 (检查防火墙设置)"},
{0x80070002, L"错误: 服务未运行 (请启动Everything)"}
};
auto it = errorMessages.find(errorCode);
SendMessageW(hResultsList, LB_ADDSTRING, 0,
(LPARAM)(it != errorMessages.end() ? it->second : L"未知错误"));
return;
}
// 显示结果
DWORD numResults = Everything_GetNumResults();
DWORD totResults = Everything_GetTotResults();
if (numResults == 0) {
SendMessageW(hResultsList, LB_ADDSTRING, 0,
(LPARAM)(totResults > 0 ? L"结果太多,请缩小范围" : L"无匹配结果"));
return;
}
// 限制显示数量
numResults = min(numResults, 100);
wchar_t path[MAX_PATH];
for (DWORD i = 0; i < numResults; ++i) {
if (Everything_GetResultFullPathNameW(i, path, MAX_PATH)) {
SendMessageW(hResultsList, LB_ADDSTRING, 0, (LPARAM)path);
}
}
// 显示统计信息
wchar_t summary[128];
swprintf_s(summary, L"显示 %d/%d 条结果", numResults, totResults);
SendMessageW(hResultsList, LB_ADDSTRING, 0, (LPARAM)summary);
}
// 打开选中的文件
void OpenSelectedFile() {
int selIndex = (int)SendMessage(hResultsList, LB_GETCURSEL, 0, 0);
if (selIndex != LB_ERR) {
wchar_t path[MAX_PATH];
if (SendMessageW(hResultsList, LB_GETTEXT, selIndex, (LPARAM)path) != LB_ERR) {
ShellExecuteW(nullptr, L"open", path, nullptr, nullptr, SW_SHOW);
}
}
}
// 窗口消息处理
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_COMMAND: {
if (HIWORD(wParam) == EN_CHANGE && (HWND)lParam == hSearchBox) {
// 防抖搜索 (300ms延迟)
static UINT_PTR timerId = 0;
if (timerId) KillTimer(hWnd, timerId);
timerId = SetTimer(hWnd, 1, 300, nullptr);
}
else if (HIWORD(wParam) == LBN_DBLCLK) {
OpenSelectedFile();
}
break;
}
case WM_TIMER: {
KillTimer(hWnd, wParam);
wchar_t text[256];
GetWindowTextW(hSearchBox, text, 256);
PerformSearch(text);
break;
}
case WM_KEYDOWN: {
if (wParam == VK_ESCAPE) {
ShowWindow(hWnd, SW_HIDE);
}
else if (wParam == VK_RETURN && GetFocus() == hSearchBox) {
// 回车打开第一个结果
SendMessage(hResultsList, LB_SETCURSEL, 0, 0);
OpenSelectedFile();
}
break;
}
case WM_HOTKEY: {
if (wParam == 1) { // Alt+Space
BOOL visible = IsWindowVisible(hWnd);
ShowWindow(hWnd, visible ? SW_HIDE : SW_SHOW);
if (!visible) {
SetFocus(hSearchBox);
}
}
break;
}
case WM_APP + 1: { // IPC消息
HandleSearchResults((DWORD)lParam);
break;
}
case WM_DESTROY: {
UnregisterHotKey(hWnd, 1);
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
能运行不报错但没有显示搜索结果
(2)配置项目属性
-
右键项目 → 属性
-
C/C++→常规→附加包含目录- 添加:
$(ProjectDir)Everything-SDK(存放Everything.h的目录)
- 添加:
-
链接器→常规→附加库目录- 添加:
$(ProjectDir)Everything-SDK(存放.lib的目录)
- 添加:
-
链接器→输入→附加依赖项- 添加:
Everything64.lib(64位)或Everything32.lib(32位)
- 添加:
- 确保
Everything64.lib和Everything.h正确配置在Everything-SDK目录,而.dll文件必须放在.exe同级目录。
$(CoreLibraryDependencies);%(AdditionalDependencies);Everything64.lib
Everything-SDK/include
Everything-SDK/lib
$(ProjectDir)Everything-SDK