什么情况下GetClipboardData(CF_TEXT)会返回NULL?
我有一个断断续续的、不一致的问题,长期以来一直让我发疯:在我的一个程序中,GetClipboardData(CF_TEXT) 在 90%(左右)的时间内成功,但每隔一次一会儿就返回NULL。
尽管事实上 OpenClipboard() 在调用 GetClipboardData(CF_TEXT) 之前总是成功(并检查返回值)。
请注意,90% 的成功率是针对同一页面! (即我知道那里有CF_TEXT内容)
注意:当失败时,我立即调用GetLastError(),但它返回的只是:“操作成功完成”。
有问题的代码很简单:
if (::OpenClipboard(hwndW))
{
HANDLE handleClip = ::GetClipboardData(CF_TEXT);
if (handleClip == NULL)
dw = GetLastError()
}
什么可能会向其中注入错误的 GetLastError() 代码?
知道什么会导致这种不一致的行为吗?
是否有其他进程正在锁定剪贴板?如果是这样,我该如何收回?
我该如何排除或调试类似的问题?
I have this intermittent and incosistent problem which has been driving me crazy for a long time: In a program of mine, GetClipboardData(CF_TEXT) succeeds 90% (or so) of the time, but every once in a while it returns NULL.
This is despite the fact that OpenClipboard() always succeeds (and return value checked) before calling GetClipboardData(CF_TEXT).
Please note that the 90% success ratio is for the same exact page! (i.e. I know there is a CF_TEXT content there)
Note: When it fails, I immediately call GetLastError() but all it returns is: "The operation completed successfully".
The code in question is as simple as:
if (::OpenClipboard(hwndW))
{
HANDLE handleClip = ::GetClipboardData(CF_TEXT);
if (handleClip == NULL)
dw = GetLastError()
}
What could possibly inject the wrong GetLastError() code into this?
Any idea what could prompt such inconsistent behavior?
Is it possible that some other process is locking the clipboard? If so, how do I take it back?
How do I troubleshoot or debug something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用 WebRoot SecureAnywhere 吗?如果未经允许的应用程序(基本上是未经预先批准的任何应用程序)尝试从受保护的应用程序(包括许多浏览器和电子邮件客户端)放置到剪贴板的剪贴板检索文本,它的身份盾功能会自动清空剪贴板。默认)。发生这种情况时,即使之前对
IsClipboardFormatAvailable(CF_TEXT)
的调用返回 true,GetClipboardData(CF_TEXT)
也将返回 NULL。Are you using WebRoot SecureAnywhere? It's Identity Shield feature automatically empties the clipboard if a non-allowed application (basically anything that wasn't pre-approved) attempts to retrieve text from the clipboard that was placed onto the clipboard by a protected application (includes many browsers and email clients by default). When this happens,
GetClipboardData(CF_TEXT)
will return NULL even if a previous call toIsClipboardFormatAvailable(CF_TEXT)
returned true.我进行了 Google 搜索,发现 其他人类似的问题(向下滚动以查找特定响应),结果是由于重入造成的。您是否在任何地方调用 EmptyClipboard() 然后对更改做出反应?也许您遇到了重入问题。
提供代码片段后更新
在您发布的代码中,调用
GetLastError
之前的条件是错误的。您仅在获得非 NULL 结果时调用它,而不是在获得NULL
结果时调用它。如果您解决了这个问题,您应该从GetLastError
获得更好的答案。 这篇 MSDN 文章 应该有助于破译GetLastError
的结果实际上意味着什么。更正代码片段后更新
我的猜测是,您正面临与某些其他应用程序访问剪贴板的竞争条件。我建议您检查是否有其他正在运行的工具可以执行此操作。
I did a Google search and found someone else with a similar problem (scroll down to find the particular response) that turned out to be due to re-entrancy. Do you call EmptyClipboard() anywhere and then react to changes? Perhaps you have a re-entrancy problem.
Update after code snippet provided
In the code you posted, the condition is wrong before calling
GetLastError
. You're only calling it when you get a non-NULL result, rather than when you get aNULL
result. If you fix that, you should get a better answer fromGetLastError
. This MSDN article should help in deciphering what the result ofGetLastError
actually means.Update after corrected code snippet
My guess is that you're facing a race condition with some other application accessing the clipboard. I would recommend checking to see if you have any other tools running that might do this.