原文 http://blog.csdn.net/shendl/archive/2008/12/26/3599854.aspx
#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
#include
#include "Out.h"
void say( wchar_t * pValue){
std::wcout.imbue(std::locale( "chs" ));
std::wcout<
}
void sayUser(UserStruct* pUserStruct){
std::wcout.imbue(std::locale( "chs" ));
std::wcout<
std::wcout<
std::wcout<
}
void sayCompany(CompanyStruct* pCompanyStruct){
std::wcout.imbue(std::locale( "chs" ));
std::wcout<
std::wcout<
std::wcout<
for ( int i=0;i
sayUser(&pCompanyStruct->users[i]);
}
}
void sayCompany2(CompanyStruct2* pCompanyStruct){
std::wcout.imbue(std::locale( "chs" ));
std::wcout<
std::wcout<
std::wcout<
for ( int i=0;i
sayUser(pCompanyStruct->users[i]);
}
}
请先把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