c++ - 从 LogonUser 函数获取最后一个错误并将其作为字符串返回?

标签 c++ string error-handling

我只是想知道如何在我的 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/

相关文章:

c - 在 C 中实现可变字符串的最有效方法是什么?

Python - 处理值错误

php - Apache不为不存在的URL返回404错误

javascript - 使用javascript单例记录错误

c++ - 在泛型编程中使用 placement new

c++ - CMake:如何检查是否已定义某些标志

javascript - 使用 javascript/jquery 删除字符串的开头

string - 你如何比较 Ant 中的字符串?

c++ - 如何在 R 中将 C++ ODE 求解器与 Rcpp 一起使用?

c++ - C++ 中的 Android 用户界面