Wednesday, March 3, 2010

Hide windows Task bar java JNI

See my previous post on javaJNI sample imlpementation for basic info.

Now how to hide the user menu in windows system, user functionalities are in user32.dll file of windows.

Hide task bar

1. write a java class for native method or dll loading

public class WindowLock {
    private native void HideTaskbarClick(boolean flag);
    public static void main(String[] args) {
        new WindowLock().HideTaskbarClick(true);

2.  now compile this you will get a WindowLock .class file and generate .h(header file) for that class by using javah command

/* DO NOT EDIT THIS FILE - it is machine generated */
/* Header for class WindowLock */

#ifndef _Included_WindowLock
#define _Included_WindowLock
#ifdef __cplusplus
extern "C" {
 * Class:     WindowLock
 * Method:    HideTaskbarClick
 * Signature: (Z)V
JNIEXPORT void JNICALL Java_WindowLock_HideTaskbarClick
  (JNIEnv *, jobject, jboolean);

#ifdef __cplusplus

3. Now you have a .h file use this to create a WindowLock.c file which is in form of c
it will look like this

 #define     WIN32_LEAN_AND_MEAN
 #define     _WIN32_WINNT 0x0400

 #include "WindowLock.h"

 #define     TASKBAR         "Shell_TrayWnd"        // Taskbar class name

  (JNIEnv *env, jobject obj, jboolean flag)
    printf("Inside Task bar Lock ! \n");
    HWND    hWnd;

    hWnd = FindWindow(TASKBAR, NULL);

    ShowWindow(hWnd, flag ? SW_SHOW : SW_HIDE);



Now the important step come in you need to download mingw software so that you can run gcc command

once you have this in place

you go to command prompt and follow these steps(depends upon where you install mingw)
C:\>Cd mingw\bin

now you need to use following command to generate .o file(i show you how)

gcc -c -I"C:\program files\Java\jdk1.5.0\include" -I"C:\Program Files\Java\jdk1.5.0\include\win32" -o "C:\windowLock\WindowLock.o" "C:\windowLock\WindowLock.c"

you have to run this command on


this will create WindowLock.o file

-I"C:\program files\Java\jdk1.5.0\include" in this you need to mention your PATH of jdk in my case this is (C:\program files\Java\jdk1.5.0\include)

and in

-o "C:\windowLock\WindowLock.o"

you need to specify loc where you want to have this WindowLock.o file(You should include all the files in one directory in my case it is windowLock)


and this is the path of WindowLock.c file

After this you now have a .o file

now you have to write WindowLock.def file like this


where WindowLock is the name of the class and _HideTaskbarClick is the native method name
save it in the same directory WindowLock in my case

now you have to create a new dll WindowLock.dll that will provide communication between java and other language

Use this command to generate a new dll

gcc -shared -o"C:\WindowLock\WindowLock.dll" "C:\WindowLock\WindowLock.o" "C:\WindowLock\WindowLock.def"

it will greate the new dll WindowLock.dll overwrite the previous one

6.  now run class with parameter true/false to hide/show task bar ...

Window Lock Source Code 


Post a Comment