xuejianhui

导航

Java对C语言Dll的加载 和 回调函数的使用

C语言HWPuSDK.h文件:

#ifndef _HW_PU_SDK_H_
#define _HW_PU_SDK_H_

#pragma pack(push,4)

#ifdef _WIN32

// for windows
#ifdef HWFEDSDK_EXPORTS
#define _HW_PU_API extern "C" __declspec(dllexport)
#else
#define _HW_PU_API extern "C" __declspec(dllimport)
#endif

#else
// for linux
#define _HW_PU_API
#define __stdcall
#define CALLBACK
#define WINAPI
#endif

typedef char                CHAR;
typedef unsigned char       UCHAR;
typedef short               SHORT;
typedef unsigned short      USHORT;
typedef int                 INT;
typedef unsigned int        UINT;
typedef int                 BOOL;
typedef long                LONG;
typedef long                DWORD;
typedef unsigned long       ULONG;
typedef unsigned long long  ULONGLONG;
typedef long long           LONGLONG;
typedef void *              HANDLE;
typedef void                VOID;
typedef int                 SOCKET;
typedef void *              HWND;

// 时间,时、分、秒,24小时制
typedef struct TIME
{
    CHAR szYear[TIME_YEAR_LEN];
    CHAR szMonth[TIME_OTHERS_LEN];
    CHAR szDay[TIME_OTHERS_LEN];
    CHAR szHour[TIME_OTHERS_LEN];
    CHAR szMinute[TIME_OTHERS_LEN];
    CHAR szSecond[TIME_OTHERS_LEN];
}PU_TIME_S, *LPPU_TIME_S;

// 告警上报信息结构
typedef struct ALARM_REPORT      
{                                        
    ULONG ulDeviceId;                                    //告警设备ID
    PU_ALARM_TYPE_E enAlarmType;                        //告警类型
    PU_TIME_S stTime;                                    //告警发生时间
    PU_ALARM_ACTION_E enAction;                         //告警发生/消除
    ULONG ulAlarmId;                                    //告警ID
    CHAR szReserve[RESERVE_LEN];
}PU_ALARM_REPORT_S, *LPPU_ALARM_REPORT_S;

// ★ 告警上报回调
typedef LONG(CALLBACK *pfGetAlarmInfoCallBack)(LPPU_ALARM_REPORT_S pstAlarmReport, VOID *pUsrData);

// ★ 告警上报接口
_HW_PU_API BOOL __stdcall IVS_PU_AlarmInfoStatesCallBack(ULONG ulIdentifyID, 
                                    pfGetAlarmInfoCallBack fAlarmResportCallBack, 
                                    VOID *pUsrData);

#pragma pack()

#endif

Java中Dll接口:

package com.huawei.ipc;

import com.sun.jna.*;

// Dll接口
public interface HwIpcSdk extends StdCallLibrary
{
    // Dll文件的加载
    HwIpcSdk INSTANCE = (HwIpcSdk)Native.loadLibrary("HWPuSDK", HwIpcSdk.class);

    //时间,时、分、秒,24小时制
    public static class LPPU_TIME_S extends Structure
    {
        public byte[] szYear = new byte[TIME_YEAR_LEN];
        public byte[] szMonth = new byte[TIME_OTHERS_LEN];
        public byte[] szDay = new byte[TIME_OTHERS_LEN];
        public byte[] szHour = new byte[TIME_OTHERS_LEN];
        public byte[] szMinute = new byte[TIME_OTHERS_LEN];
        public byte[] szSecond = new byte[TIME_OTHERS_LEN];
    }

    // 告警上报信息结构
    public static class PU_ALARM_REPORT_S extends Structure
    {
        public int ulDeviceId;      //告警设备ID
        public int enAlarmType;     //告警类型
        public LPPU_TIME_S stTime;  //告警发生时间
        public int enAction;        //告警发生/消除
        public int ulAlarmId;       //告警ID
        public byte[] szReserve = new byte[RESERVE_LEN]; 
    }

    // ★ 告警上报回调函数
    public static interface FGetAlarmInfoCallBack extends StdCallCallback
    {
        public int invoke(PU_ALARM_REPORT_S lpAlarmReport, String pszUserInfo);
    }

    // ★ 告警上报接口
    boolean IVS_PU_AlarmInfoStatesCallBack(int ulIdentifyID, 
                                    FGetAlarmInfoCallBack fAlarmResportCallBack, 
                                    String pszUserInfo);
}

 

调用类:

package com.huawei.ipc;

import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.util.Date;
import com.sun.jna.*;

// 主界面类
public class MainEntry extends javax.swing.JFrame
{
    //告警上报回调函数
    AlarmCallBack alarmCallBack = null;

    static HwIpcSdk hwIpcSdk = HwIpcSdk.INSTANCE;

    // 告警上报提供调用
    public void enableAlarm()
    {
        if(null == alarmCallBack)
        {
            alarmCallBack = new AlarmCallBack();
        } 
     
        if(false == hwIpcSdk.IVS_PU_AlarmInfoStatesCallBack(m_lUserID, alarmCallBack, null))
        {
            JOptionPane.showMessageDialog(this, "启动告警监测失败");
            System.out.println("启动告警监测失败!");
        }
        else
        {
            System.out.println("启动告警监测成功!");
        }
    }

    // 告警上报的回调函数类
    public class AlarmCallBack implements HwIpcSdk.FGetAlarmInfoCallBack
    {
        // ★ 回调的函数实现
        public int invoke(HwIpcSdk.PU_ALARM_REPORT_S lpAlarmReport, String pszUserInfo)
        {
            // 处理 lpAlarmReport 和 pszUserInfo 的内容
            return 0;
        }
    }
}

posted on 2012-10-24 09:40  xuejianhui  阅读(1254)  评论(0编辑  收藏  举报