首先,我是JNA新手。我想通过 Java 代码控制主板的 LED 灯。华硕为此提供了一个用 C(C++?)编写的 SDK。
他们的头文件看起来很简单:
#pragma once
#include <Windows.h>
typedef void* MbLightControl;
typedef void* GPULightControl;
typedef void* ClaymoreKeyboardLightControl;
typedef void* RogMouseLightControl;
typedef DWORD(WINAPI* EnumerateMbControllerFunc)(MbLightControl handles[], DWORD size);
typedef DWORD(WINAPI* SetMbModeFunc) (MbLightControl handle, DWORD mode);
typedef DWORD(WINAPI* SetMbColorFunc) (MbLightControl handle, BYTE* color, DWORD size);
typedef DWORD(WINAPI* GetMbColorFunc) (MbLightControl handle, BYTE* color, DWORD size);
typedef DWORD(WINAPI* GetMbLedCountFunc)(MbLightControl handle);
typedef DWORD(WINAPI* EnumerateGPUFunc)(GPULightControl handles[], DWORD size);
typedef DWORD(WINAPI* SetGPUModeFunc) (GPULightControl handle, DWORD mode);
typedef DWORD(WINAPI* SetGPUColorFunc) (GPULightControl handle, BYTE* color, DWORD size);
typedef DWORD(WINAPI* GetGPULedCountFunc)(GPULightControl handle);
typedef DWORD(WINAPI* CreateClaymoreKeyboardFunc)(ClaymoreKeyboardLightControl* handle);
typedef DWORD(WINAPI* SetClaymoreKeyboardModeFunc) (ClaymoreKeyboardLightControl handle, DWORD mode);
typedef DWORD(WINAPI* SetClaymoreKeyboardColorFunc) (ClaymoreKeyboardLightControl handle, BYTE* color, DWORD size);
typedef DWORD(WINAPI* GetClaymoreKeyboardLedCountFunc)(ClaymoreKeyboardLightControl handle);
typedef DWORD(WINAPI* CreateRogMouseFunc)(RogMouseLightControl* handle);
typedef DWORD(WINAPI* SetRogMouseModeFunc) (RogMouseLightControl handle, DWORD mode);
typedef DWORD(WINAPI* SetRogMouseColorFunc) (RogMouseLightControl handle, BYTE* color, DWORD size);
typedef DWORD(WINAPI* RogMouseLedCountFunc)(RogMouseLightControl handle);
我使用 JNAerator 来获取 JNA 映射,这是我得到的结果:
import com.ochafik.lang.jnaerator.runtime.LibraryExtractor;
import com.ochafik.lang.jnaerator.runtime.MangledFunctionMapper;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
public interface Aura extends Library {
String JNA_LIBRARY_NAME = LibraryExtractor.getLibraryPath("AURA_SDK", true, Aura.class);
NativeLibrary JNA_NATIVE_LIB = NativeLibrary.getInstance(Aura.JNA_LIBRARY_NAME, MangledFunctionMapper.DEFAULT_OPTIONS);
Aura INSTANCE = (Aura) Native.loadLibrary(Aura.JNA_LIBRARY_NAME, Aura.class, MangledFunctionMapper.DEFAULT_OPTIONS);
interface EnumerateMbControllerFunc extends StdCallCallback {
int apply(PointerByReference handles, int size);
}
interface SetMbModeFunc extends StdCallCallback {
int apply(Pointer handle, int mode);
}
interface SetMbColorFunc extends StdCallCallback {
int apply(Pointer handle, Pointer color, int size);
}
interface GetMbColorFunc extends StdCallCallback {
int apply(Pointer handle, Pointer color, int size);
}
interface GetMbLedCountFunc extends StdCallCallback {
int apply(Pointer handle);
}
interface EnumerateGPUFunc extends StdCallCallback {
int apply(PointerByReference handles, int size);
}
interface SetGPUModeFunc extends StdCallCallback {
int apply(Pointer handle, int mode);
}
interface SetGPUColorFunc extends StdCallCallback {
int apply(Pointer handle, Pointer color, int size);
}
interface GetGPULedCountFunc extends StdCallCallback {
int apply(Pointer handle);
}
interface CreateClaymoreKeyboardFunc extends StdCallCallback {
int apply(PointerByReference handle);
}
interface SetClaymoreKeyboardModeFunc extends StdCallCallback {
int apply(Pointer handle, int mode);
}
interface SetClaymoreKeyboardColorFunc extends StdCallCallback {
int apply(Pointer handle, Pointer color, int size);
}
interface GetClaymoreKeyboardLedCountFunc extends StdCallCallback {
int apply(Pointer handle);
}
interface CreateRogMouseFunc extends StdCallCallback {
int apply(PointerByReference handle);
}
interface SetRogMouseModeFunc extends StdCallCallback {
int apply(Pointer handle, int mode);
}
interface SetRogMouseColorFunc extends StdCallCallback {
int apply(Pointer handle, Pointer color, int size);
}
interface RogMouseLedCountFunc extends StdCallCallback {
int apply(Pointer handle);
}
}
在示例 C++ 代码中,他们使用 GetProcAddress 来获取上述方法的地址,这样他们就可以调用它们:
HMODULE hLib = nullptr;
hLib = LoadLibraryA("AURA_SDK.dll");
(FARPROC&)EnumerateMbController = GetProcAddress(hLib, "EnumerateMbController");
DWORD _count = EnumerateMbController(NULL, 0);
我如何对 JNA 做同样的事情?
提前致谢。