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 5961901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:01:51+00:00 2026-05-22T19:01:51+00:00

I have .so (shared library) written in C++, lets call it functionality.so in which

  • 0

I have .so (shared library) written in C++, lets call it functionality.so in which I implement different functions, here is list of some functions:

1. unsigned long Initialize(void* userData);
2. unsigned long Uninitialize(void);
3. unsigned long DeviceOpen( unsigned long id, unsigned long* device);
4. unsigned long DeviceClose( unsigned long device );

and so on …

I want to use this library’s (functionality.so) functionality in my java application for android. For that I create jni folder in my android application project folder and place there files:

  1. Android.mk

    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE           := Test_library
    LOCAL_SRC_FILES        := Test_library.c
    ## Linking functionality library
    LOCAL_LDLIBS := -lfunctionality
    include $(BUILD_SHARED_LIBRARY)
    
  2. Test_library.c

    #include <string.h>
    #include <jni.h>
    #include "Test_library.h"
    
    jint Java_com_Dsm_Test_DsmLibraryTest_vtUninitialize(JNIEnv* env, jobject thiz) {
    return Uninitialize( );
    }
    
    jint Java_com_Dsm_Test_DsmLibraryTest_vtDeviceClose(JNIEnv* env, jobject thiz, jint hDevice) {
    return DeviceClose( hDevice );
    }
    
  3. Test_library.h

    A header file where Initialize, Uninitialize, DeviceOpen, DeviceClose functions are declared.

After this I run ndk-build and create a Test_library.so library and load it in my java application and use them like this:

// Some code

public native int Uninitialize( );

public native int DeviceClose( int hDevice );

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

Everything Runs fine. After I want to add other two functions

1. unsigned long Initialize(void* userData);
2. unsigned long DeviceOpen( unsigned long id, unsigned long* device);

`

Now Questions:

  1. How I can write this two functions as native in java ? As there are no void* or unsigned long* types in java
  2. How I can write same functions in Test_library.c as in jni.h there are no void** or unsigned long* types

Thanks for help.

  • 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-05-22T19:01:52+00:00Added an answer on May 22, 2026 at 7:01 pm

    You can use jlong to pass a pointer (or a pointer to pointer, or whatever) back to Java. Java code won’t be able to use it for anything, other than passing it as an argument to one of your other methods; but often that’s all you really want. If, on the other hand, you want Initialize() to be called with data set up in Java, then void * isn’t appropriate; you’ll need to use a Java class, and use reflection in JNI to get the information you need out of it.

    Crudely, you could wrap malloc() and free():

    jlong Java_c_utils_malloc(JNIEnv* env, jclass clazz, jint size) {
        return (jlong) malloc(size);
    }
    
    void Java_c_utils_free(JNIEnv* env, jclass clazz, jlong ptr) {
       free((void *) ptr);
    }
    

    and then use them (to no effect!) in Java:

    long ptr = utils.malloc(100);
    // Store ptr for a while
    utils.free(ptr);
    

    Now, if we wrapped some other functions that needed a block of memory as an argument, we could wrap them too, and let them accept a jlong argument, the same way free() does. The fact that the Java variable ptr represents a memory address is completely opaque in Java, but it’s useful nonetheless.

    Window system implementations for Java (i,e., AWT, SWT) use this same sort of thing to associate the native widget handle with the Java component.

    Now, if you want your Initialize() to be able to take useful arguments from Java, then a void * isn’t going to cut it. You’d need to write your method to accept a Java object as an argument; that’s the only way to allow you to manipulate the object in Java.

    I don’t want to duplicate all the code here, but Sun’s JNI tutorial is here. This is the section on calling arbitrary methods of a Java object (either the this object, or one passed to your method as an argument) and this is an analogous section on accessing the fields of an object.

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

Sidebar

Related Questions

No related questions found

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.