gpt4 book ai didi

验证具有错误域的帐户时,C++ LogonUser() API 速度很慢

转载 作者:行者123 更新时间:2023-11-28 07:04:10 29 4
gpt4 key购买 nike

我正在使用以下代码:

HANDLE token;
if (!LogonUser("LocalUser", "InvalidDomain", "Password", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &token))
{
unsigned long error = GetLastError();
}

当机器在一个域上,并且输入了一个无效的域时,验证登录可能需要很长时间。这可以从 5 秒到 2/3 分钟不等。

有什么方法可以改进吗?

最佳答案

这是一个老问题,我几年前也遇到过同样的问题。作为解决方法,您可以使用 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;
}

关于验证具有错误域的帐户时,C++ LogonUser() API 速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22016699/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com