简体   繁体   中英

Java Native Access a callback function with a float pointer

I have a C++ DLL that exposes the following function. this function calls the callback function ( GetProperty ) immediately. I can not change the C++ DLL

// c++
DllExport unsigned int RegisterCallbackGetPropertyReal( bool (*GetProperty)( UINT property_identifer, float * value ) ) ; 

I have a C# application that uses com.sun.jna to access this function of the DLL. I have gotten to the point where the callback function is being called correctly from the c++ DLL but I can't see to find a way to set the float * value

// Java 
public class main {
    public interface CBlargAPI extends Library {
        interface GetPropertyReal_t extends Callback {
            boolean invoke (int property_identifer, FloatByReference value);
        }
        int RegisterCallbackGetPropertyReal( GetPropertyReal_t getProperty ) ; 
    }

    public static void main(String[] args) throws Exception 
    {
        // Register call back functions         
        CBlargAPI.GetPropertyReal_t GetPropertyReal_fn = new CBACnetAPI.GetPropertyReal_t() {
            @Override
            public boolean invoke(int property_identifer, FloatByReference value) {
                System.out.println("GetPropertyReal_t: " )  ;
                value.setValue(97.5f); 
                return false; // [Edit] This is where the problem was. this should be `return true;` See my answer below. 
            }
        };
        CBlargAPI.INSTANCE.RegisterCallbackGetPropertyReal( GetPropertyReal_fn ) ;             
    }
}

What I would have expected is that the value should be set to 97.5f when it gets back to the c++ DLL. instead I get the default value of 0.000f

My question is:

  • How do I correctly set the a float pointer value in java with Jna?

This is a typo that no one else could have found. basicly if I return false on the GetPropertyReal_fn callback the default value of 0.000f would be used.

I have edited the question to show where the error was.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM