我正在尝试取消对 WNetAddConnection2 的调用,但失败了与 CancelSynchronousIo .
对 CancelSynchronousIo 的调用成功,但实际上没有取消任何内容。
我使用的是在 Windows 7 x64 上运行的 32 位控制台应用。
有谁成功完成过这个操作吗?我是不是在做一些蠢事?这是一个示例控制台应用程序(需要与 mpr.lib 链接):
DWORD WINAPI ConnectThread(LPVOID param)
{
NETRESOURCE nr;
memset(&nr, 0, sizeof(nr));
nr.dwType = RESOURCETYPE_ANY;
nr.lpRemoteName = L"\\\\8.8.8.8\\bog";
// result is ERROR_BAD_NETPATH (i.e. the call isn't cancelled)
DWORD result = WNetAddConnection2(&nr, L"pass", L"user", CONNECT_TEMPORARY);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
// Create a new thread to run WNetAddConnection2
HANDLE hThread = CreateThread(0, 0, ConnectThread, 0, 0, 0);
if (!hThread)
return 1;
// Retry the cancel until it fails; keep track of how often
int count = 0;
BOOL ok;
do
{
// Sleep to give the thread a chance to start
Sleep(1000);
ok = CancelSynchronousIo(hThread);
++count;
}
while (ok);
// count will equal two here (i.e. one successful cancellation and
// one failed cancellation)
// err is ERROR_NOT_FOUND (i.e. nothing to cancel) which makes
// sense for the second call
DWORD err = GetLastError();
// Wait for the thread to finish; this takes ages (i.e. the
// WNetAddConnection2 call is not cancelled)
WaitForSingleObject(hThread, INFINITE);
return 0;
}
最佳答案
根据 Larry Osterman 的说法(我希望他不介意我引用他的话):“这个问题在评论中得到了回答:wnetaddconnection2 不是一个简单的 IOCTL 调用。”所以答案(不幸的是)是否定的。
关于windows - CancelSynchronousIo 是否可以与 WNetAddConnection2 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9475581/