Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 1.24 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Does CancelSynchronousIo work with WNetAddConnection2?
  2. DWORD WINAPI ConnectThread(LPVOID param)
  3. {
  4.     NETRESOURCE nr;
  5.     memset(&nr, 0, sizeof(nr));
  6.     nr.dwType = RESOURCETYPE_ANY;
  7.     nr.lpRemoteName = L"\\8.8.8.8\bog";
  8.  
  9.     // result is ERROR_BAD_NETPATH (i.e. the call isn't cancelled)
  10.     DWORD result = WNetAddConnection2(&nr, L"pass", L"user", CONNECT_TEMPORARY);
  11.  
  12.     return 0;
  13. }
  14.  
  15. int _tmain(int argc, _TCHAR* argv[])
  16. {
  17.     // Create a new thread to run WNetAddConnection2
  18.     HANDLE hThread = CreateThread(0, 0, ConnectThread, 0, 0, 0);
  19.     if (!hThread)
  20.         return 1;
  21.  
  22.     // Retry the cancel until it fails; keep track of how often
  23.     int count = 0;
  24.     BOOL ok;
  25.     do
  26.     {
  27.         // Sleep to give the thread a chance to start
  28.         Sleep(1000);
  29.         ok = CancelSynchronousIo(hThread);
  30.         ++count;
  31.     }
  32.     while (ok);
  33.  
  34.     // count will equal two here (i.e. one successful cancellation and
  35.     // one failed cancellation)
  36.  
  37.     // err is ERROR_NOT_FOUND (i.e. nothing to cancel) which makes
  38.     // sense for the second call
  39.     DWORD err = GetLastError();
  40.  
  41.     // Wait for the thread to finish; this takes ages (i.e. the
  42.     // WNetAddConnection2 call is not cancelled)
  43.     WaitForSingleObject(hThread, INFINITE);
  44.  
  45.     return 0;
  46. }