2014-02-25 54 views

回答

1

這是一個老問題,幾年前我有同樣的問題。作爲解決方法,您可以使用WaitForSingleObject。基本的想法是啓動一個線程,該線程將執行LogonUser並使主線程等待n毫秒,以便工作線程在完成時設置的句柄。

#include "stdafx.h" 
#include <windows.h> 
#include <stdio.h> 

HANDLE athEvent; 
DWORD WINAPI ThreadAuth(LPVOID); 
DWORD AUTHTIMEOUT = 5000; 

int main(void) 
{ 
    HANDLE hThread; 
    DWORD dwEvent, dwThreadID; 

    athEvent = CreateEvent(NULL, FALSE, FALSE, NULL); 

    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadAuth, NULL, 
     0, &dwThreadID); 

    // Wait for the thread to signal 
    dwEvent = WaitForSingleObject(athEvent, AUTHTIMEOUT); //5 seconds timeout 

    switch (dwEvent) 
    { 
     // athEvent was signaled 
     case WAIT_OBJECT_0: 
      printf("Authenticated"); 
      break; 
     case WAIT_TIMEOUT: 
      printf("Authenticate timeout\n"); 
      TerminateThread(hThread, 0); 
      break; 
     default: 
      printf("Wait error: %d\n", GetLastError()); 
      ExitProcess(0); 
    } 

    CloseHandle(athEvent); 
    CloseHandle(hThread); 

    return 0; 
} 

DWORD WINAPI ThreadAuth(LPVOID lpParam) 
{ 

    UNREFERENCED_PARAMETER(lpParam); 

    //## Insert your logon method here 
    //LogonUser("LocalUser", "InvalidDomain", "Password", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &token) 
    Sleep(5000); //if >= AUTHTIMEOUT then the waitforsingleobject will expire 
    //## 

    //Signal the event so WaitForSingleObject is released 
    SetEvent(athEvent); 

    return 0; 
}