• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

NPGetUser problem

I am trying to use NPGetUser to get username (via Java Native Methods) for the person logged on to the domain. I keep getting compile errors with 'unresolved external symbol _NPGetUser@12'.

Am I doing anything wrong in this code? Why does it not find _NPGetUser@12, although I have npapi.h?

#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#include <npapi.h>

JNIEXPORT jstring JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{

CHAR sz1[1024];
CHAR szName[1024];
DWORD dwSize= 1024;

szName[0]= 0;
sz1[0]=0;

if( NPGetUser( (LPTSTR)sz1, szName, &dwSize ) != 0 )
{
printf( "User is %s", szName );
return (*env)->NewStringUTF(env,szName);
}
else
{
printf( "Error!" );
}
}


The error message is:
----------------------------------------------------------

"HelloWorldImp.c
HelloWorldImp.c(19) : warning C4133: 'function' : incompatible types - from 'char *' to 'unsigned sh
ort *'
HelloWorldImp.c(19) : warning C4133: 'function' : incompatible types - from 'char [1024]' to 'unsign
ed short *'
HelloWorldImp.c(33) : warning C4715: 'Java_HelloWorld_displayHelloWorld' : not all control paths ret
urn a value
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/dll
/implib:hello.lib
/out:hello.dll
advapi32.lib
HelloWorldImp.obj
 Creating library hello.lib and object hello.exp
HelloWorldImp.obj : error LNK2001: unresolved external symbol _NPGetUser@12
hello.dll : fatal error LNK1120: 1 unresolved externals
": error LNK2001: unresolved external symbol _NPGetUser@12"


0
toyota
Asked:
toyota
  • 4
1 Solution
 
jkrCommented:
"NPGetUser()" is a function from the Win9x DDK - you do not want to use this one. "GetUserName()" is the API of choice, e.g.


#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#include <npapi.h>

JNIEXPORT jstring JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{

CHAR sz1[1024];
CHAR szName[1024];
DWORD dwSize= 1024;

szName[0]= 0;
sz1[0]=0;

if( GetUserName( (szName, &dwSize ) != 0 )
{
printf( "User is %s", szName );
return (*env)->NewStringUTF(env,szName);
}
else
{
printf( "Error!" );
}
}

0
 
jkrCommented:
BTW: As a reference why you should not use it, see the docs at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/w98ddk/hh/w98ddk/network_0581.asp

Using "GetUserName()" will get rid of your first two compiler warnings also.

The last one

HelloWorldImp.c(33) : warning C4715: 'Java_HelloWorld_displayHelloWorld' : not all control paths return a value

is more serious and occurs because you are not returning anything when the 'if' condition fais. Change it to

JNIEXPORT jstring JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{

CHAR sz1[1024];
CHAR szName[1024];
DWORD dwSize= 1024;

szName[0]= 0;
sz1[0]=0;

if( GetUserName( (szName, &dwSize ) != 0 )
{
printf( "User is %s", szName );
return (*env)->NewStringUTF(env,szName);
}
else
{
printf( "Error!" );
}

return (*env)->NewStringUTF(env,"No User Name");
}


0
 
toyotaAuthor Commented:
The GetUserName works fine if called from the command line. The correct username is displayed.

However, when this function is called through JSP, the value 'SYSTEM' is returned. This seems to be the name used by the thread on the server or something.

So, how do I get the actual user logged on to the domain?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jkrCommented:
>>However, when this function is called through JSP, the value 'SYSTEM' is returned

That is because the JSP environment runs as a system service.

>>So, how do I get the actual user logged on to the domain?

That is more difficult - there might be one user logged on or more. Maybe there is no user logged on at all. For a sample on how to find out who is logged on, see the source code of PsLoggedOn at http://www.sysinternals.com/files/PsLoggedonSrc.zip
0
 
jkrCommented:
Anything new on this one?
0
 
jmcgOwnerCommented:
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by jkr.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now