简体   繁体   English

如何在JNA中获取本机库的指针?

[英]How can I get the Pointer for a native library in JNA?

I want to reproduce this sample code in JNA : http://msdn.microsoft.com/en-us/library/windows/desktop/bb427356%28v=vs.85%29.aspx 我想在JNA中重现此示例代码: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/bb427356%28v=vs.85%29.aspx

In this code, I have to use the FormatMessage function (in Kernel32). 在这段代码中,我必须使用FormatMessage函数(在Kernel32中)。 I can have access to this function through JNA, but the second param is a pointer / handle on the return value of the call of LoadLibraryEx. 我可以通过JNA访问此函数,但是第二个参数是LoadLibraryEx调用的返回值上的指针/句柄。

Here is the question : how can I fill the ??????? 问题是:我该如何填充??????? :

Pointer p = ???????
String m = Kernel32.INSTANCE.FormatMessage(flag, p, .....)

Thanks for any help. 谢谢你的帮助。

You need to call LoadLibraryEx(). 您需要调用LoadLibraryEx()。 This is how I call a function returning a pointer (actually a String, but I work with it as a Pointer to be able to free the memory): 这就是我调用返回指针的函数的方法(实际上是一个String,但是我将其用作Pointer以能够释放内存):

public interface CLibrary1 extends Library
{
    public Pointer string_test(String str1, String str2); 
}

public static void main(String[] args)
{
    try {
        CLibrary1 clib1 = (CLibrary1)Native.loadLibrary("???.so", CLibrary1.class);
        Pointer p = clib1.string_test("string1", "string3");
        System.out.println(p.getString(0));
        Native.free(Pointer.nativeValue(p));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

This example calls the function char* string_test(char *, char *); 此示例调用函数char* string_test(char *, char *);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM