首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在JNA中使用GetProcAddress?

如何在JNA中使用GetProcAddress?
EN

Stack Overflow用户
提问于 2018-05-31 17:18:11
回答 1查看 528关注 0票数 0

首先,我是一个JNA新手。我想通过java代码来控制我的主板的LED照明。华硕为此提供了一个用C (C++?)编写的软件开发工具包。

它们的头文件看起来很简单:

代码语言:javascript
复制
#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映射,结果如下所示:

代码语言:javascript
复制
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来获取上述方法的地址,这样它们就可以调用它们:

代码语言:javascript
复制
HMODULE hLib = nullptr;

hLib = LoadLibraryA("AURA_SDK.dll");

(FARPROC&)EnumerateMbController = GetProcAddress(hLib, "EnumerateMbController");

DWORD _count = EnumerateMbController(NULL, 0);

如何使用JNA执行相同的操作?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-31 18:14:51

我不知道为什么头文件看起来是这样的,但是基于您提供的C示例,驻留在库AURA_SDK中的函数EnumerateMbController是用两个参数调用的-第一个参数是MbLightControl数组(这些是不透明的指针),第二个参数表示“大小”。

我会从上下文中推断出:

  • MbLightControl数组将接收指针
  • 是您提供的数组的大小
  • 函数的返回值将是实际返回的MbLightControl条目数

带NULL的调用表明,它将返回它将返回的MbLightControl实例的数量。

我对正确绑定的建议是:

代码语言:javascript
复制
public interface Aura extends StdCallLibrary {

    Aura INSTANCE = Native.loadLibrary("AURA_SDK", Aura.class, W32APIOptions.DEFAULT_OPTIONS);

    public static class MbLightControl extends WinDef.PVOID {}

    public int EnumerateMbController(MbLightControl[] handles, int size);
    public int SetMbMode(MbLightControl handle, int mode);
    public int SetMbColor(MbLightControl handle, byte[] color, int size);
    public int GetMbColor(MbLightControl handle, byte[] color, int size);
    public int GetMbLedCount(MbLightControl handle);
}

这将加载名为AURA_SDK的动态链接库,并使五个函数可以访问。它还模仿MbLightControl的头文件中的类型定义。DWORD_s被绑定为java int以便于调用,因为java中的_int始终是32位的,而Windows上的DWORD值是相同的。

我不知道颜色代码的细节,但如果这些是RGB LED,那么我假设每个LED将占用字节数组中的3个字节。

它可能有助于了解,在幕后,JNA使用LoadLibraryEx (LoadLibrary的大哥)和GetProcAddress来加载函数。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50620781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档