JNA 应用小结

 

(1) char类型

    Java里的Char是双字节的,对应C的类型是wchar_t!要对应c的char类型可以用byte

 

(2) 字符串处理

    const char*类型官方建议直接对应String类型,实际应用时对中文的编码问题老是处理不好!所以我这边用byte[]进行对应传入字符串时手工加个'\0',如: 

    str=str+"\0" ;fun(str.getBytes("Gb2312"));

 

(3) 结构体处理

     结构体指针参数:

    typedef struct _Point {

  int x, y;
} Point;

Point* translate(Point* pt, int dx, int dy);

 

   // Equivalent JNA mapping

class Point extends Structure { public int x, y; }
Point translate(Point pt, int x, int y);
...
Point pt = new Point();
Point result = translate(pt, 100, 100);

   结构体参数或结构体返回值

   typedef struct _Point {

  int x, y;
} Point;

Point translate(Point pt, int dx, int dy);

   // Equivalent JNA mapping

class Point extends Structure {
    public static class ByValue extends Point implements Structure.ByValue { }
    public int x, y;
}
Point.ByValue translate(Point.ByValue pt, int x, int y);
...
Point.ByValue pt = new Point.ByValue();
Point result = translate(pt, 100, 100);

   结构体数组参数

   // Original C code

void get_devices(struct Device[], int size);

   // Equivalent JNA mapping

int size = ...
Device[] devices = new Device[size];
lib.get_devices(devices, devices.length);

    返回结构体数组(指针)

    // Original C code

struct Display* get_displays(int* pcount);
void free_displays(struct Display* displays);

   // Equivalent JNA mapping

Display get_displays(IntByReference pcount);
void free_displays(Display[] displays);
...
IntByReference pcount = new IntByReference();
Display d = lib.get_displays(pcount);
Display[] displays = (Display[])d.toArray(pcount.getValue());
...
lib.free_displays(displays);

 

 

JNA管方地址: http://jna.java.net/

文档地址:  http://jna.java.net/javadoc/overview-summary.html

你可能感兴趣的:(html,C++,c,.net,C#)