简体   繁体   English

来自C ++应用程序的Java com.sun.jna回调函数

[英]Java com.sun.jna callback function from a c++ application

I have a DLL written in C++ that I can not change. 我有一个用C ++编写的无法更改的DLL。 It has the following exposed function 它具有以下公开功能

// c++ 
DllExport unsigned int ProcessMessage( char * in_message, USHORT in_message_length, char * connectionString, bool ( SendMessage)( char * connectionString, BYTE * payload, USHORT iPayloadSize )  );

I have a Java application that needs to call this DLL function. 我有一个Java应用程序,需要调用此DLL函数。 I am currently using a java library com.sun.jna 我目前正在使用Java库com.sun.jna

// Java 
public class main {
    public interface CBlargAPI extends Library {
        interface sendMessage_t extends Callback {
            boolean invoke(String connectionString, Pointer payload, short iPayloadSize );
        }
        int ProcessMessage( byte[] in_message, short in_message_length, String connectionString, sendMessage_t SendMessage ) ; 
    }
    public static void main(String[] args) throws Exception 
    {
        // Override function thingy (#A) 
        CBlarg.sendMessage_t sendMessage_fn  = new CBlarg.sendMessage_t() {
            @Override
            public boolean invoke(String connectionString, Pointer payload, short iPayloadSize) {
                System.out.println("sendMessage_t: " )  ;
                return false; 
                }
            };         
        }
        CBlargAPI.INSTANCE.ProcessMessage( receivePacket.getData(), (short) receivePacket.getLength(), connectionString, sendMessage_fn ); 
    }

    // static member function (#B) 
    public static boolean SendUDPMessage( String connectionString, Pointer payload, short length )  {    
        // ToDo: I want to use this one. 
    }
}

Currently this is working with the override function thingy (#A) but I want to use the static member function (#B) instead. 当前,这正在使用override function thingy (#A)但我想使用static member function (#B)代替。 I have tried a few things without success such as 我尝试了一些没有成功的事情,例如

// Errors with "cannot find symbol, symbol: class SendUDPMessage, location: class main" 
CBACnetAPI.sendMessage_t sendMessage_fn  = new main.SendUDPMessage();

I am primary a c++ programmer and rarely touch java 我主要是C ++程序员,很少接触Java

My question is: 我的问题是:

  • How do I call the static member function SendUDPMessage() as the callback instead of the Override function thingy (#A) ? 如何调用静态成员函数SendUDPMessage()作为回调而不是Override function thingy (#A)

You have to make the override function thingy (#A) call the static member function(#B) . 您必须让override function thingy (#A)调用static member function(#B) Java does not have function pointers, so you need an interface object to serve that purpose. Java没有函数指针,因此您需要一个接口对象来实现此目的。

Why do you need to do #B over #A? 为什么您需要#B胜过#A?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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