windows - CancelSynchronousIo 是否可以与 WNetAddConnection2 一起使用?

标签 windows winapi networking

我正在尝试取消对 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/

相关文章:

windows - 有没有办法使用 PowerShell 检查本地服务器上任意安全主体的管理权限?

winapi - 渲染音频流(WASAPI/WINAPI)

delphi - GetAsyncKeyState "strange"行为

c# - 向没有窗口的进程发送 WM_CLOSE 消息

macos - 在系统级别的 Mac OS X Lion 上修改传出的 HTTP header

java - Java 中的线程最大数量?

c++ - 如何获取外部应用程序 ListView 的 HWND?在 Windows Api 中使用 c++

使用 ETW 跟踪 Windows 用户模式进程系统调用

c++ - timeGetTime() 开始变量大于结束变量

networking - 上传文件后ftp4j读取超时