天天看点

JNI方法调用C++类库<一>

布局:

C++ ------------>lib,文件dms_lib.cpp

C++ ------------>JNI C++,文件dms_jni.cpp

JAVA ------------>JNI JAVA,文件dms.java

通过以下命令,可以生成java的jni头文件:javac dms.java && javah dms

/** 
 * file: dms.java
 * {@hide}
 */
public class dms
{
    // can't instantiate this class
    private dms()
    {
    }

    /**
     * start the upnp device.    
     */
    public static native int startDevice();
    /**
     * stop the upnp device.
     */
    public static native int stopDevice();
}
           

通过命令javac dms.java && javah dms生成的头文件:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class dms */

#ifndef _Included_dms
#define _Included_dms
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     dms
 * Method:    startDevice
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_dms_startDevice
  (JNIEnv *, jclass);

/*
 * Class:     dms
 * Method:    stopDevice
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_dms_stopDevice
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif
           

dms_lib.h:

#ifndef DMS_LIB_H
#define DMS_LIB_H
#include "Neptune.h"
#include "Platinum.h"

class dms
{
    private dms();
    
    private PLT_UPnP m_upnp;
    private PLT_DeviceHostReference m_device;
    
    public void start_dms();
    public void stop_dms();
}    

#endif //DMS_LIB_H
           

dms_lib.cpp:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Neptune.h"
#include "Platinum.h"
#include "dms_lib.h"

dms:dms
{
	/*
	 char *str = "test";
	 printf("start...%s",str); 
	 */
	// setup Neptune logging
	NPT_LogManager::GetDefault().Configure("plist:.level=INFO;.handlers=ConsoleHandler;.ConsoleHandler.colors=off;.ConsoleHandler.filter=42");

	m_device = new PLT_FileMediaServer("/data/local", 
								"Platinum UPnP Media Server");
	NPT_List<NPT_IpAddress> list;
	NPT_CHECK_SEVERE(PLT_UPnPMessageHelper::GetIPAddresses(list));
	NPT_String ip = list.GetFirstItem()->ToString();

	m_device->m_ModelDescription = "Platinum Media Server";
	m_device->m_ModelURL = "http://www.plutinosoft.com/";
	m_device->m_ModelNumber = "1.0";
	m_device->m_ModelName = "Platinum Media Server";
	m_device->m_Manufacturer = "Plutinosoft";
	m_device->m_ManufacturerURL = "http://www.plutinosoft.com/";

	m_upnp.AddDevice(m_device);
}

void dms::start_dms()
{
	NPT_CHECK_SEVERE(m_upnp.Start());	
	 
}

void dms::stop_dms()
{
	/*
	 char *str = "test";
	 printf("stop..."); 
	 */

	m_upnp.Stop();
}