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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:32:45+00:00 2026-06-04T01:32:45+00:00

I have a native function call that calls my c++ code and this code

  • 0

I have a native function call that calls my c++ code and this code creates an instance of my user defined class.

This function has the following signature:

public native Object loadEngine(int arg);

After this call has been made, I would like to call more native functions that would take the “Object” returned from the loadEngine() and modify it, or request data from it.

An example of signature:

public native String loadEngine(Object engine, int queryID);
  1. Is it even possible to do this ?
  2. If it is possible, how would i convert my GameEngine datatype to java Object or jni jobject.
  3. Java would automatically pass this as a reference so that it would never be copied and i would be able to modify it, right ?

The following scenario generates an error saying that it can’t convert GameEngine to jobject which is understandable but that’s my best shot having very very little experience with Java:

JNIEXPORT jobject JNICALL Java_package_loadEngine
  (JNIEnv *env, jobject obj, jint arg) {
     GameEngine engine(params);
     return (jobject)engine;
  }

JNIEXPORT jstring JNICALL Java_package_queryAction
  (JNIEnv *env, jobject obj, jobject engine, jint arg) {
     String ret = newString(Integer.toString((GameEngine)engine.unimportant()));
     return ret;
  }

Since I’m calling native java code via jni to c++, I unfortunatily can’t define the native function as:

 public native GameEngine loadEngine(int arg);

Understandable, yet I can’t seem to be able to figure out a workaround.

Thanks, Scarlet.

  • 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-04T01:32:46+00:00Added an answer on June 4, 2026 at 1:32 am

    You could create Java wrapper classes that contain, own, and manage your native code. For instance (off the top of my head, brain-compiled):

    class GameEngine {
        private long nativeGameEnginePointer;
    
        private native long loadEngine();
        public GameEngine() {
            super(...);
            nativeGameEnginePointer = loadEngine();
        }
    
        private native void destroyEngine(long nativePointer);
        protected void finalize() throws Throwable {
            try {
                destroyEngine(nativeGameEnginePointer);
            } finally {
                super.finalize();
            }
        }
    
        private native double nativePlayGameOrWhatever(long nativePointer);
        public double playGameOrWhatever() {
            return nativePlayGameOrWhatever(nativeGameEnginePointer);
        }
    }
    

    And in your JNI implementation:

    class CxxGameEngine;
    
    JNIEXPORT jlong JNICALL GameEngine_loadEngine(JNIEnv *env, jobject obj) {
         return (jlong)(new CxxGameEngine(params));
    }
    
    JNIEXPORT jvoid JNICALL GameEngine_destroyEngine(JNIEnv *env, jobject obj, jlong nativePointer) {
         delete (CxxGameEngine *)nativePointer;
    }
    
    JNIEXPORT jdouble JNICALL GameEngine_nativePlayGameOrWhatever(JNIEnv *env, jobject obj, jlong nativePointer) {
         return ((CxxGameEngine *)nativePointer)->playGameOrWhatever();
    }
    

    Note the use of long to represent a native pointer. This is how the Java runtime does so when needed (e.g. in the java.nio and java.util.zip packages.) long rather than int ensures that the type is wide enough to hold a pointer even on 64-bit systems.

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

Sidebar

Related Questions

If I have the following code: class User < ActiveRecord::Base has_many :problems attr_accessible :email,
The basic idea is that I have a native function I want to call
I have C# wrapper code that calls functions from a native (C++) dll. Currently,
The Setup I have a PDF API which has a native function that is
I have a native/unmanaged DLL and it has a CreateObject function which returns a
This is probably a multi-part question. Background: we have a native (c++) library that
I'm trying to call a native function from a managed assembly. I've done this
I have code that supports both native C++ types as well as Qt types.
I have a native C function (with JNI of course) that should callback a
The setup: I have a unmanaged/native Win32 application that I inject my code into.

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.