我只是想知道如何在我的 C++ 程序中获取导致 LoginUser 函数失败的错误并将其作为字符串返回?
JNIEXPORT jstring JNICALL Java_com_entrust_adminservices_urs_examples_authn_LdapAuthenticator2_takeInfo(JNIEnv *env, jobject obj, jstring domain, jstring id, jstring idca, jstring password)
{
const char *nt_domain;
const char *nt_id;
const char *nt_password;
nt_domain = env->GetStringUTFChars(domain, NULL);
nt_id = env->GetStringUTFChars(id, NULL);
nt_password = env->GetStringUTFChars(password, NULL);
HANDLE hToken = 0;
char *otherString;
bool aut = true;
aut = LogonUser(nt_id, nt_domain, nt_password, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &hToken );
if(aut)
{
otherString = "true";
}
else
{
otherString = //how would i get the last error here?
}
jstring newString = env->NewStringUTF((const char*)otherString);
return newString;
}
int main()
{
return 0;
}
谢谢 -皮特
编辑:
谢谢大家,用:
DWORD dwError = GetLastError();
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPTSTR) &lpMsgBuf, 0, NULL );
otherString = (char*)lpMsgBuf;
最佳答案
在 Windows 中,您可以使用 GetLastError检索错误,然后 FormatMessage将其转换为您可以使用的字符串。
关于c++ - 从 LogonUser 函数获取最后一个错误并将其作为字符串返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1075243/