File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Getting Computer Name using JNI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Getting Computer Name using JNI" Watch "Getting Computer Name using JNI" New topic
Author

Getting Computer Name using JNI

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi,

Can anyone help me with this? Obviously any developer who will launch his
application for windows may find this useful too.

Here is the VB code...



The above code says that the function is in "kernel32" dll of windows and method
name is apiGetComputerName.

Maybe using WSH (windows scripting host) is an easier solution?

For this we need to on runtime from our Java program


1. Create a wsh js (javascript) file say "computer.js"
2. Write this code in it
(One may try the code below in a .js file and click it to get computer name)
var WshNetwork = WScript.createObject("WScript.Network");
//WScript.Echo "Computer Name: " + WshNetwork.computerName;
WScript.echo("Computer Name: " + WshNetwork.computerName);

3. Execute it using runtime class
4. Read output
using...
Process..getInputStream();

5. delete file.

My main question is does a computer name is a valid indicator to check for
license validity of your application? I am putting a question on the
Java in General forum too to discuss this issue. I am using 2-3 indicators.

I am making a license checking component that any application can use by calling a method.

Thanks,

Maki Jav

[ January 13, 2008: Message edited by: Maki Jav ]
[ January 16, 2008: Message edited by: Maki Jav ]

Help gets you when you need it!
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
If a solution exists by executing a script and reading the output, that is likely to be easier than JNI.

However, starting an external process is a big operation. If you are going to need the computer name frequently and you can't cache it (which I'd guess you can...), then JNI will be much faster.

To use JNI, you need to be able to write and build C or C++. You write a C/C++ library, to the JNI specifications, and this calls the Windows kernel function.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi,

Thanks for replying me Peter. The application needs to check name on every run only and compare with previously stored file in the jar archive. Please see the above link to Java in general forum for my idea.

The code for checking name is here. Anyone may use this class by including it in their application classpath. I will appreciate it if users keep this class as it is or send me a copy when they add method to check computer name in Unix/Linux and other systems. They will add their names to the list of authors.


Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
hi,

Peter Chase and any other member of Javaranch, have you tried the above code on your system?


Thank you,


Maki Jav
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Sorry, I don't have time to try out that code.

One point I would make is that the %computername% environment variable is just an environment variable. It can be changed easily. So you wouldn't want to make a licensing scheme rely too heavily on it.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi,

You are right to an extent. This changing of enviromental variable is restricted to each individual command prompt only. Even if you change enviromental variable from Control Panel> System> Advance>Enviromental System Variables, You will not see the change in the command promt. Only in User Variables you will notice the change.

The user will not be aware of the method my system is using. It is using
1) computer name along with directory 2) path to check this.

Maybe this is somewhere machine number stored in the registry. It can be accessed using a Java api.

Maki Jav
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi,

I just got the computer name that is stored in the hotkey and it was not changed when I try to change name from command prompt or from ControlPanel as described in the above post.

The following has two names on XP System. I don't know what is the path on Windows 98 and other versions.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\
1)ActiveComputerName
2)ComputerName

Both are showing the same name.

Maki Jav
[ January 16, 2008: Message edited by: Maki Jav ]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Rather than go into all this detail about what can and can't change the name, I think you should just accept that the "computer name" is easily changed (and yes, even hand-editing the registry counts as easy compared to the sorts of things crackers can get up to). Computer name should not be used as an important part of any strong security scheme.

Network adapter MAC values are sometimes used to identify a machine, as they are supposed to be unique and are not trivially modifiable. They're not as safe as you'd like (they're often in flash, which can be changed), but much better than the computer name.

We still use hardware dongles here. People still crack us, though!

As I said in the other thread, if you are trying to achieve anything other than the most basic "deterrent" security, in a few days, starting from ordinary developer skills, you will fail.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi,

I have got solution on this thread for accessing MAC number


http://forum.java.sun.com/thread.jspa?threadID=245711

Thanks,

Maki Jav
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Getting Computer Name using JNI