要把.h文件放到java $%JAVA_home%/include下面。否则会报 java.lang.UnsatisfiedLinkError 调用dll代码: public interface LicenseLibrary extends StdCallLibrary { LicenseLibrary INSTANCE = Native.load("D:\\IoTMapData\\license\\bin\\LicenseDll.dll", LicenseLibrary.class); /** * string: //生成的机器码(密文) * * @param strPubKeyPath//公钥路径 * @return 获取机器码(加密后) */ String getmcencoded(String strPubKeyPath); /** * 校验license * * @param strProduct //当前产品 * @param strPubKeyPath //公钥路径 * @param strLicenseEn //签名的机器码(密文) * @param nfo * @return int: 0 license验证成功 -1 license格式错误 -2 机器码校验失败 -3 产品未获授权 -4 授权级别参数非法 // -5 许可类型参数非法 -6 * 不在许可时间范围之内 -7 检测到当前时间早于上次使用时间 */ int verifylicense( String strProduct, String strPubKeyPath, String strLicenseEn, LicenseInfo nfo); }
h文件里的struct 对象:
public class LicenseInfo extends Structure { public char[] strMCEn = new char[256 * 4]; public int nLevel; public int nType; public long llEndDate; public char[] strUserName = new char[256]; public char[] strUserTel = new char[256]; public char[] strProduct = new char[256]; public char[] getStrMCEn() { return strMCEn; } public void setStrMCEn(char[] strMCEn) { this.strMCEn = strMCEn; } @Override public List getFieldOrder() { return Arrays.asList( "strMCEn", "strUserName", "strUserTel", "strProduct", "nLevel", "nType", "llEndDate"); } // public LicenseInfo (String strMCEn, String strUserName, String strUserTel, String // strProduct, int nLevel, int nType, long llEndDate) { // this.strMCEn =strMCEn.toCharArray(); // this.strUserName=strUserName.toCharArray(); // this.strUserTel=strUserTel.toCharArray(); // this.strProduct=strProduct.toCharArray(); // this.nLevel=nLevel; // this.nType=nType; // this.llEndDate=llEndDate; // } public int getnLevel() { return nLevel; } public void setnLevel(int nLevel) { this.nLevel = nLevel; } public int getnType() { return nType; } public void setnType(int nType) { this.nType = nType; } public long getLlEndDate() { return llEndDate; } public void setLlEndDate(long llEndDate) { this.llEndDate = llEndDate; } public void setLlEndDate(Long llEndDate) { this.llEndDate = llEndDate; } public char[] getStrUserName() { return strUserName; } public void setStrUserName(char[] strUserName) { this.strUserName = strUserName; } public char[] getStrUserTel() { return strUserTel; } public void setStrUserTel(char[] strUserTel) { this.strUserTel = strUserTel; } public char[] getStrProduct() { return strProduct; } public void setStrProduct(char[] strProduct) { this.strProduct = strProduct; } }
h头文件代码:
#pragma once
#define PROPERTY_MAX_LEN 256 // 属性字段最大长度
typedef struct _T_LICENSE_INFO
{
char strMCEn[PROPERTY_MAX_LEN*4];
char strUserName[PROPERTY_MAX_LEN];
char strUserTel[PROPERTY_MAX_LEN];
char strProduct[PROPERTY_MAX_LEN];
int nLevel;
int nType;
long long llEndDate;
};
#ifdef __cplusplus
extern "C" {
#endif
//*************************************************************************
// Returns: char*: //生成的机器码(密文)
// describe: 获取机器码(加密后)
// Parameter: const char* strPubKeyPath //公钥路径
//*************************************************************************
__declspec(dllexport) char* getmcencoded(const char* strPubKeyPath);
//*************************************************************************
// Returns: int: 0 license验证成功 -1 license格式错误 -2 机器码校验失败 -3 产品未获授权 -4 授权级别参数非法
// -5 许可类型参数非法 -6 不在许可时间范围之内 -7 检测到当前时间早于上次使用时间
// describe: 校验license
// Parameter: const char* strProduct //当前产品
// const char* strPubKeyPath //公钥路径
// const char* strMCSign //签名的机器码(密文)
// _T_LICENSE_INFO info //license信息
//*************************************************************************
__declspec(dllexport) int verifylicense(const char* strProduct, const char* strPubKeyPath, const char* strLicenseEn, _T_LICENSE_INFO &info);
#ifdef __cplusplus
}
#endif