?
Solved

NPGetUser problem

Posted on 2003-03-05
7
Medium Priority
?
440 Views
Last Modified: 2010-05-18
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
Comment
Question by:toyota
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8073305
"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
 
LVL 86

Expert Comment

by:jkr
ID: 8073325
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
 

Author Comment

by:toyota
ID: 8073363
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 8073407
>>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
 
LVL 86

Expert Comment

by:jkr
ID: 8102989
Anything new on this one?
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10058918
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question