Cancels SynchronousIo working with WNetAddConnection2?

I am trying and cannot cancel the call to WNetAddConnection2 with CancelSynchronousIo .

The CancelSynchronousIo call succeeds but nothing is canceled.

I am using a 32-bit console application running on Windows 7 x64.

Has anyone done this successfully? Am I doing something stupid? Here is an example console application (which needs to be associated with 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 (ie 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 (ie one successful cancellation and // one failed cancellation) // err is ERROR_NOT_FOUND (ie nothing to cancel) which makes // sense for the second call DWORD err = GetLastError(); // Wait for the thread to finish; this takes ages (ie the // WNetAddConnection2 call is not cancelled) WaitForSingleObject(hThread, INFINITE); return 0; } 
+2
source share
1 answer

According to Larry Osterman (I hope he doesnโ€™t mind me quoting him): โ€œThe question was answered in the comments: wnetaddconnection2 is not just an IOCTL call.โ€ So the answer is (unfortunately) no.

+2
source

Source: https://habr.com/ru/post/1396965/


All Articles