Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8033063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:34:50+00:00 2026-06-05T01:34:50+00:00

The following java program calls a native method in C that should print a

  • 0

The following java program calls a native method in C that should print a message you pressed a key ! if the user presses a key. But i can’t see the message as the i press the key.I also check if the function SetWindowsHookEx returns null but no,it doesn’t return null.

Java Code :

package keylogger;

public class TestKeys {

private native void setWinHook();

public static void main(String args[]) {
    TestKeys o = new TestKeys();
    try {
        o.setWinHook();
        Thread.sleep(10000);
    } catch(Exception exc) {
        exc.printStackTrace();
    }
}

static {
    System.loadLibrary("MyHook");
}

}
C Code :

#include <stdio.h>
#include <windows.h>
#include <w32api.h>
#include "keylogger_TestKeys.h"
static HHOOK handleKeyboardHook = NULL;
HINSTANCE hInst = NULL;

static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
  printf("You pressed a key !\n");
  return CallNextHookEx(handleKeyboardHook, nCode, wParam, lParam);
}

void Java_keylogger_TestKeys_setWinHook
 (JNIEnv *env, jobject obj) {
hInst = GetModuleHandle(NULL); // include or exclude,i don't see the result
handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc,NULL, 0);
if(handleKeyboardHook==NULL) {
    printf("Is Null");
} else {
    printf("Is not Null");
}
printf("Inside fucntion setWinHook !");
}

/*int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
 printf("Hello World !");
 handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0);
 if(handleKeyboardHook==NULL) {
  printf("Is Null");
 } else {
    printf("Is not Null");
   }
 MSG msg;
 while(GetMessage(&msg, NULL, 0, 0))
 {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
 }
 return msg.wParam;
}*/

The only output that I see is Is not NullInside fucntion setWinHook !

Where is the problem ?

What should i do so that this program returns me the message when i press the key.
The only output that I see is : Inside function setWinHook !

Note :

If the above program runs on someone’s machine,please mention that.

Output Pic :

enter image description here

I don’t see any message on key tapping.Program just exits after 10 seconds without displaying a message.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:34:52+00:00Added an answer on June 5, 2026 at 1:34 am

    There is for sure a better way to implement this. Here DllMain is called multiple times once a thread has been created,that doesn’t seem right to me.I am not sure if this is legal ! The C Code starts a new thread to implement the keycatcher.

    C Code :

    #include <stdio.h>
    #include <windows.h>
    #include <w32api.h>
    #include "keylogger_TestKeys.h"
    
    static HHOOK handleKeyboardHook = NULL;
    HINSTANCE hInst = NULL;
    static DWORD hookThreadId = 0;
    static HANDLE hookThreadHandle = NULL;
    BOOL WINAPI installHook(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved);
    static int i = 0;
    
    static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
    
      printf("You pressed the key !");
      return CallNextHookEx(handleKeyboardHook, nCode, wParam, lParam);
    }
    
    
    BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
     if(hookThreadHandle==NULL) {
        printf("hookThreadHandle is NULL\n");
        LPTHREAD_START_ROUTINE lpStartAddress = &installHook;
        hookThreadHandle = CreateThread(NULL, 0, lpStartAddress, NULL, 0, &hookThreadId);
     }
    }
    
    BOOL WINAPI installHook(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved) {
    // printf("From installHook : %u",fwdReason);
    printf("count : %d\n",i++);
    handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hinstDLL, 0);
    MSG msg;
    
    while(GetMessage(&msg, NULL, 0, 0))
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
    return msg.wParam;
    }
    
    void Java_keylogger_TestKeys_unregisterWinHook
     (JNIEnv *env, jobject obj) {
     // should have stopped the thread before unhooking
     if(handleKeyboardHook != NULL) {
        UnhookWindowsHookEx(handleKeyboardHook);
     }
    }
    
    void Java_keylogger_TestKeys_stopNativeThread // stop the native thread
      (JNIEnv *env, jobject obj) {
        PostThreadMessage(hookThreadId, WM_QUIT, (WPARAM) NULL, (LPARAM) NULL);
        WaitForSingleObject(hookThreadHandle, 5000);
    }
    

    Java Code :

    package keylogger;
    
    public class TestKeys {
    private static int i = 0;
    private native void setWinHook();
    private native void unregisterWinHook();
    private native void createWinThread();
    private native void stopNativeThread();
    
    public static void main(String args[]) {
        TestKeys o = new TestKeys();
        try {
            Thread.sleep(5000);
        }catch(Exception exc) {
            exc.printStackTrace();
        }
       o.stopNativeThread();
       o.unregisterWinHook();
       System.out.println("Native thread stopped and Hook unregistered !");
    
       try {
            Thread.sleep(3000); // Now you won't see the message : you pressed the key 
        }catch(Exception exc) {
            exc.printStackTrace();
        }
    }
    
    static {
        System.loadLibrary("MyHook");
    }
    }
    

    I start the java program and DLLMain is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a stand alone Java program (THAT WORKS) it calls a native library
I have a simple Java program that allows a user to draw rectangles on
Possible Duplicate: Is Java pass-by-reference? Java pass by reference For the following Java program,
I get the following error when I run a java program: Exception in thread
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
Why gives the following Windows 7 .cmd command script: set SUN_JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_17 if
I've got the following variable set in my cygwin $HOME/.bashrc PATH=/bin:/usr/sbin:/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin:$PATH Problem is
The folowing program was orignally in java. But i still get 1 error with
I have written a Hello, world program with JNI. Java calls c program with
I have a java program that generates an HTML file. The Java program takes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.