views:

128

answers:

4

Hey Folks,

I am just wondering how i can get the error causing LoginUser function to fail in my C++ program and return it as a String?

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;
}

Thanks -Pete

Edit:

Thanks guys, did it with:

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;
+4  A: 

In Windows, you can use GetLastError to retrieve the error, and then FormatMessage to turn that into a string you can use.

Harper Shelby
Damn it, 8 seconds... Well, +1, your answer is perfect :)
avakar
+1  A: 

Use GetLastError to retrieve the error code. Use FormatMessage to get its textual description (there's an example at the bottom of the page).

avakar
+1  A: 

I use the FormatMessage function for that.

Otávio Décio
+1  A: 

Take a look at the function FormatMessage. You can pass FORMAT_MESSAGE_FROM_SYSTEM flag and the error code returned by GetLastError().

Naveen