I have a DLL written in C++ that I can not change. 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. I am currently using a java library 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. 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
My question is:
- How do I call the static member function
SendUDPMessage()as the callback instead of theOverride function thingy (#A)?
You have to make the
override function thingy (#A)call thestatic member function(#B). Java does not have function pointers, so you need an interface object to serve that purpose.Why do you need to do #B over #A?