gpt4 book ai didi

c - FindWindow() 偶尔失败(尝试 IPC)

转载 作者:可可西里 更新时间:2023-11-01 11:46:17 25 4
gpt4 key购买 nike

我在 stackoverflow 上的第一篇文章。

我不是一个编码员,我有时会为了好玩而涉足编码,因此我不会花太多时间来理解基础知识,而是找到任何可行的解决方案,即使它有点“丑陋” .

这让我想到了我的问题:我用 C 编写了一个简单的 winapi 程序,带有一个对话框和一个 DlgProc。它接受文件并对它们做一些事情,比方说,为了简化,它所做的只是创建一个扩展名为 *.BAK 的文件副本。

我已经在注册表 (HKEY_CLASSES_ROOT*\shell\BKUP\command) 中添加了一个键,这样我就可以在 Windows 资源管理器中选择几个文件,并可以选择“创建备份”来将它们的所有名称发送到我的程序,但是分别为每个文件调用我的程序。所以我用谷歌搜索,做了一些阅读,结果我需要一个叫做 IPC(进程间通信)的东西,阅读了一些选项,WM_COPYDATA 消息看起来像是最简单和最简单的解决方案,所以我使用了它并且它像一个魅力一样工作,但是,有时它只是不......首先我会解释我做了什么:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
COPYDATASTRUCT dsIPC;
hwnd=FindWindow("#32770","Backup program");
if(hwnd)
{
// send "__argv[1]" via SendMessage(hwnd,WM_COPYDATA... etc.
return(0);
}

return DialogBox(hInstance, MAKEINTRESOURCE(ID_DIALOG), NULL, DlgProc);
}

我使用 FindWindow() 检查是否有正在运行的程序实例,如果没有,它会正常运行,如果是,我将文件名发送到 FindWindow() 找到的窗口,然后完全退出该程序实例。

在对话过程中,我有代码用文件名填充一个数组,并使用 SetTimer() 为每个设置一个短计时器,当收到所有文件名时,计时器关闭,我开始复制文件。

同样,所有这一切都很好,但有时会打开程序的两个甚至 3 个实例,并且文件在它们之间拆分,这意味着 FindWindow() 有时无法找到第一个窗口。示例:

我在 Windows 资源管理器中选择 10 个文件,右键单击它们并选择“创建备份”。我的程序打开了 2 个窗口。

第一个窗口输出:

"File 00.DAT" Backed up successfully.
"File 01.DAT" Backed up successfully.

第二个窗口输出:

"File 02.DAT" Backed up successfully.
"File 03.DAT" Backed up successfully.
"File 04.DAT" Backed up successfully.
"File 05.DAT" Backed up successfully.
"File 06.DAT" Backed up successfully.
"File 07.DAT" Backed up successfully.
"File 08.DAT" Backed up successfully.
"File 09.DAT" Backed up successfully.

然后,我关闭 2 个窗口并再次选择相同的 10 个文件并再次选择“创建备份”,但这次和接下来的几次尝试我只得到一个窗口:

第一个窗口输出:

"File 00.DAT" Backed up successfully.
"File 01.DAT" Backed up successfully.
"File 02.DAT" Backed up successfully.
"File 03.DAT" Backed up successfully.
"File 04.DAT" Backed up successfully.
"File 05.DAT" Backed up successfully.
"File 06.DAT" Backed up successfully.
"File 07.DAT" Backed up successfully.
"File 08.DAT" Backed up successfully.
"File 09.DAT" Backed up successfully.

谁能解释为什么会这样?

附言如果重要的话,我正在 Windows 7 x64 上进行测试。

[编辑 - 2017 年 5 月 16 日]这是 zett42 代码的简化版本,它非常适合测试,但在我的待办事项列表中,我写下了阅读和理解 zett42 代码的其余部分,因为我的代码可能在某些方面存在缺陷。

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
COPYDATASTRUCT dsIPC;
int i;
DWORD err;
HANDLE hMutex;
hMutex = CreateMutex(NULL, TRUE, "56f0e348-2c1a-4e01-a98e-3e6c8198f9aa");
err = GetLastError();
if(!hMutex)
{
MessageBox(NULL, "Cannot create mutex object. Click OK to exit.", "Error:", MB_OK | MB_ICONSTOP);
return (1);
}
if(err == ERROR_ALREADY_EXISTS)
{
for(i=0 ; i<1000 ; i++)
{
hwnd=FindWindow("#32770","Backup program");
if(hwnd) break;
Sleep(30);
}
if(i==1000) return (1);
dsIPC.dwData=666;
dsIPC.cbData=lstrlen(__argv[1])+1;
dsIPC.lpData=__argv[1];
SendMessage(hwnd, WM_COPYDATA, (WPARAM)hInstance, (LPARAM)&dsIPC);
return(0);
}

return DialogBox(hInstance, MAKEINTRESOURCE(ID_DIALOG), NULL, DlgProc);
}

最佳答案

正如评论者 treintje 所建议的:

There could be a race condition happening where two of your program instances are started at the same time and neither of them had the chance to create a dialog window yet, causing FindWindow to return NULL in both cases. You could prevent this by using a mutex object to check if another instance is already running.

这是一个代码示例,展示了 mutex 是如何实现的。可用于避免竞争条件:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
// Try to create a mutex. Replace the string by something globally unique,
// for instance a GUID created by using the GuidGen utility, that comes with
// Visual Studio (look in the "Extras" menu).
HANDLE hMutex = CreateMutexW(nullptr, TRUE, L"REPLACE-WITH-YOUR-GUID");

// Make sure to put no other code in between the CreateMutex() and the
// GetLastError() calls to prevent the last error value from being messed up.
DWORD err = GetLastError();

if(!hMutex)
{
// TODO: error handling
return 1;
}

if(err == ERROR_ALREADY_EXISTS)
{
// An instance of this process is already running, but it might not
// have created the window yet, so FindWindow() could still fail.
// You could call FindWindow() in a loop but that would waste resources.
// So I'm using an event object to wait until the window has been created.
// This event object must be set to "signaled" state in WM_INITDIALOG
// handler of the dialog.

HANDLE hWindowCreatedEvent = CreateEventW(nullptr, TRUE, FALSE,
L"PUT-ANOTHER-GUID-HERE");
if(hWindowCreatedEvent)
{
// Wait with timeout of 30s because the 1st process might have failed
// to create the window for some reason.
DWORD waitRes = WaitForSingleObject(hWindowCreatedEvent, 30 * 1000);
if(waitRes == WAIT_OBJECT_0)
{
// The event is signaled so now we know for sure that the window
// has been created.
HWND hwnd;
COPYDATASTRUCT dsIPC;
hwnd=FindWindow("#32770","Backup program");
if(hwnd)
{
// send "__argv[1]" via SendMessage(hwnd,WM_COPYDATA... etc.
}
}
else
{
// TODO: error handling
}

CloseHandle(hWindowCreatedEvent);
}
else
{
// TODO: error handling
}
}
else
{
// This is the first instance of this process.

return DialogBox(hInstance, MAKEINTRESOURCE(ID_DIALOG), NULL, DlgProc);
}

CloseHandle(hMutex);
}

编辑您的 DialogProc设置事件对象,向进程的其他实例发出窗口已创建的信号:

INT_PTR CALLBACK DialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch(uMsg)
{
case WM_INITDIALOG:
{
// Use the same event name as in WinMain()!
HANDLE hWindowCreatedEvent = CreateEventW(nullptr, TRUE, FALSE,
L"PUT-GUID-FROM-WINMAIN-HERE");
if(hWindowCreatedEvent)
{
SetEvent(hWindowCreatedEvent);
CloseHandle(hWindowCreatedEvent);
}
// other initialization code...
return TRUE;
}
}
return FALSE;
}

另一个建议:不要搜索窗口标题,因为“备份程序”是通用的方式,可以被其他应用程序使用。然后,您会将 WM_COPYDATA 发送到错误的进程。

相反,register a window class具有全局唯一名称并仅搜索类名(调用 FindWindow() 并将 NULL 作为 lpWindowName 的参数)。您还必须在对话框模板中指定类名。

关于c - FindWindow() 偶尔失败(尝试 IPC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43959259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com