从LogonUser函数中获取最后一个错误并将其作为字符串返回? 我只是想知道我如何才能在C++程序中导致Login用户函数失败,并将它作为字符串返回? 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; }

从LogonUser函数中获取最后一个错误并将其作为字符串返回? 我只是想知道我如何才能在C++程序中导致Login用户函数失败,并将它作为字符串返回? 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; },c++,string,error-handling,C++,String,Error Handling,谢谢 -皮特 编辑: 谢谢大家,我用了: 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) &am

谢谢 -皮特

编辑:

谢谢大家,我用了:

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中,您可以使用来检索错误,然后将其转换为可以使用的字符串。

使用来检索错误代码。用于获取其文本描述(页面底部有一个示例)。

我使用FormatMessage函数来实现这一点。

看看这个函数。您可以从系统标志传递格式化消息以及GetLastError()返回的错误代码。

该死的,8秒。。。嗯,+1,你的答案很完美:)