1. 程式人生 > >貼上JNA—JNI終結者、深入解析JNA—模擬C語言結構體兩篇文章的完整原始碼

貼上JNA—JNI終結者、深入解析JNA—模擬C語言結構體兩篇文章的完整原始碼

原文 http://blog.csdn.net/shendl/archive/2008/12/26/3599854.aspx

貼上 JNA—JNI 終結者 深入解析 JNA— 模擬 C 語言結構體 兩篇文章的完整原始碼

C 語言原始碼:

標頭檔案

#define MYLIBAPI extern "C" __declspec ( dllexport )

MYLIBAPI void say( wchar_t * pValue);

struct UserStruct{

long id;

wchar_t * name;

int age;

};

MYLIBAPI void sayUser(UserStruct* pUserStruct);

struct CompanyStruct{

long id;

wchar_t * name;

// UserStruct* users[100];

UserStruct users[100];

int count;

};

struct CompanyStruct2{

long id;

wchar_t * name;

UserStruct* users[100];

// UserStruct users[100];

int count;

};

MYLIBAPI void sayCompany(CompanyStruct* pCompanyStruct);

MYLIBAPI void sayCompany2(CompanyStruct2* pCompanyStruct);

原始檔

#include "stdafx.h"

#include <locale.h>

#include <iostream>

#include "Out.h"

void say( wchar_t * pValue){

std::wcout.imbue(std::locale(

"chs" ));

std::wcout<<L "上帝說:" <<pValue<<std::endl;

}

void sayUser(UserStruct* pUserStruct){

std::wcout.imbue(std::locale( "chs" ));

std::wcout<<L "ID:" <<pUserStruct->id<<std::endl;

std::wcout<<L "姓名:" <<pUserStruct->name<<std::endl;

std::wcout<<L "年齡:" <<pUserStruct->age<<std::endl;

}

void sayCompany(CompanyStruct* pCompanyStruct){

std::wcout.imbue(std::locale( "chs" ));

std::wcout<<L "ID:" <<pCompanyStruct->id<<std::endl;

std::wcout<<L "公司名稱:" <<pCompanyStruct->name<<std::endl;

std::wcout<<L "員工總數:" <<pCompanyStruct->count<<std::endl;

for ( int i=0;i<pCompanyStruct->count;i++){

sayUser(&pCompanyStruct->users[i]);

}

}

void sayCompany2(CompanyStruct2* pCompanyStruct){

std::wcout.imbue(std::locale( "chs" ));

std::wcout<<L "ID:" <<pCompanyStruct->id<<std::endl;

std::wcout<<L "公司名稱:" <<pCompanyStruct->name<<std::endl;

std::wcout<<L "員工總數:" <<pCompanyStruct->count<<std::endl;

for ( int i=0;i<pCompanyStruct->count;i++){

sayUser(pCompanyStruct->users[i]);

}

}

Java 原始檔:

請先把JNA.jar 匯入classpath

package com.google.p.shendl.jna.net;

import com.google.p.shendl.jna.net.TestDll1Service.TestDll1.CompanyStruct;

import com.google.p.shendl.jna.net.TestDll1Service.TestDll1.CompanyStruct2;

import com.google.p.shendl.jna.net.TestDll1Service.TestDll1.UserStruct;

import com.sun.jna.Library;

import com.sun.jna.Native;

import com.sun.jna.NativeLong;

import com.sun.jna.Structure;

import com.sun.jna.WString;

/**

* @author 沈東良 Edward Shen shendl_s@hotmail .com

* 2008 - 11 - 23 下午 05:07:14

*TestDll1.dll

*/

public class TestDll1Service {

public interface TestDll1 extends Library {

/**

* 當前路徑是在專案下,而不是 bin 輸出目錄下。

*/

TestDll1 INSTANCE = (TestDll1)Native.loadLibrary ( "TestDll1" , TestDll1. class );

public void say(WString value);

/*

* 定義一個類,模擬 C 語言的結構

* */

public static class UserStruct extends Structure{

public static class ByReference extends UserStruct implements Structure.ByReference { }

public static class ByValue extends UserStruct implements Structure.ByValue

{ }

public NativeLong id ;

public WString name ;

public int age ;

}

public void sayUser(UserStruct.ByReference struct);

public static class CompanyStruct extends Structure{

public static class ByReference extends CompanyStruct implements Structure.ByReference { }

public NativeLong id ;

public WString name ;

//public UserStruct[] users;

public UserStruct.ByValue[] users ;

//public UserStruct.ByValue[] users=new UserStruct.ByValue[100];

public int count ;

}

public void sayCompany(CompanyStruct.ByReference pCompanyStruct);

public static class CompanyStruct2 extends Structure{

public static class ByReference extends CompanyStruct2 implements Structure.ByReference { }

public NativeLong id ;

public WString name ;

public UserStruct.ByReference[] users = new UserStruct.ByReference[100];

public int count ;

}

public void sayCompany2(CompanyStruct2.ByReference pCompanyStruct);

}

/**

*

*/

public TestDll1Service() {

// TODO Auto-generated constructor stub

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

TestDll1. INSTANCE .say( new WString( "Hello World!" ));

System. out .println( "HHEEH 我我們無法萬惡 " );

UserStruct.ByReference userStruct= new UserStruct.ByReference();

userStruct. id = new NativeLong(100);

userStruct. age =30;

userStruct. name = new WString( " 沈東良 " );

TestDll1. INSTANCE .sayUser(userStruct);

System. out .println( "AAAAAAAAAAAAAAAAAAAAAAAAAAA" );

CompanyStruct.ByReference companyStruct= new CompanyStruct.ByReference();

companyStruct. id = new NativeLong(1);

companyStruct. name = new