2012-11-15 64 views
0

我有一個用C++編寫的DLL,我無法更改。它具有以下暴露功能來自C++應用程序的Java com.sun.jna回調函數

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

我有一個Java應用程序需要調用此DLL函數。我目前使用的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. 
    } 
} 

目前這個正在與override function thingy (#A),但我想用static member function (#B)代替。我已經嘗試了一些事情沒有成功,如

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

我主要是C++程序員,很少觸及的Java

我的問題是:

  • 如何調用靜態成員函數SendUDPMessage()作爲回調而不是Override function thingy (#A)

回答

1

您必須撥打override function thingy (#A)才能撥打static member function(#B)。 Java沒有函數指針,所以你需要一個接口對象來達到這個目的。

爲什麼你需要做#B超過#A?

+0

我只是想讓班級設置得更乾淨。 –

相關問題