jna 使用笔记

最近用到jna想实现获得当前用户选中文本的功能,结果发现这是个不好做的功能,后来想通过发送WM_COPY来通过粘贴板获得也是不行。只好放弃。

jna我没仔细看,只是初略了解了下。

 

扩展:

user32这样的类,没有完全暴露user32的接口,因此,如果有你想用的api但是jna又没封装的话,只要自己定义接口就行了,例如

public interface MyUser32 extends User32
{
	MyUser32 INSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class);

	WinDef.HWND GetForegroundWindow();

	long GetLastError();

	long SendMessageW(HWND hWnd, int msg, int num1, int num2);

	boolean IsClipboardFormatAvailable(int cfText);

	boolean OpenClipboard(HWND hWnd);

	void CloseClipboard();

	Pointer GetClipboardData(int format);

	boolean AttachThreadInput(int idAttach, int idAttachTo, boolean fAttach);

	int GetCurrentThreadId();

	boolean BringWindowToTop(HWND hwnd);

	int WM_KEYUP = 0x0101;

	int VK_CONTROL = 0x11;
	
	int VK_C = 0x43;

	int WM_COPY = 0x0301;
	int CF_TEXT = 1;
}


这各类中我暴露了GetForegroundWindow这样的多个user32 api。主要注意类型匹配,这个是个关键,怎样匹配可从官网文档上看:http://jna.java.net/javadoc/overview-summary.html

C TypeNative RepresentationJava Type
char8-bit integerbyte
wchar_tplatform-dependentchar
short16-bit integershort
int32-bit integerint
intboolean flagboolean
enumenumeration typeint (usually)
long long, __int6464-bit integerlong
float32-bit floating pointfloat
double64-bit floating pointdouble
pointer (e.g. void*)platform-dependent (32- or 64-bit pointer to memory)Buffer

Pointer

pointer (e.g. void*),

array
32- or 64-bit pointer to memory (argument/return)

contiguous memory (struct member)
<P>[] (array of primitive type)
In addition to the above types, which are supported at the native layer, the JNA Java library automatically handles the following types. All but NativeMapped and NativeLong are converted to Pointer before being passed to the native layer.
longplatform-dependent (32- or 64-bit integer)NativeLong
const char*NUL-terminated array (native encoding or jna.encoding)String
const wchar_t*NUL-terminated array (unicode)WString
char**NULL-terminated array of C stringsString[]
wchar_t**NULL-terminated array of wide C stringsWString[]
void**NULL-terminated array of pointersPointer[]
struct*

struct
pointer to struct (argument or return) (or explicitly)

struct by value (member of struct) (or explicitly)

Structure
unionsame as StructureUnion
struct[]array of structs, contiguous in memoryStructure[]
void (*FP)()function pointer (Java or native)Callback
pointer (<T> *)same as PointerPointerType
otherinteger typeIntegerType
othercustom mapping, depends on definitionNativeMapped

具体情况那还得自己钻研,我还不是很熟。

例如一个hwnd型既可以用jna的HWND也可以用pointer,到底用哪个取决于你的需要,同时申明两个函数对应同一个api也是可行的。

boolean OpenClipboard(HWND hWnd);

boolean OpenClipboard(Pointer hWnd);

都是对应OpenClipboard,只是参数类型不一样,最终会转到windows api认同的类型。

 

继承

Myuser32 extends User32 这样的写法不能继承User32 声明的接口,如果要用User32 的接口,还是应该使用user32.instance来调用。

所以Myuser32 extends StdCallLibrary才比较合适。

java的char是unicode所以对于字符串缓存,如果java 的byte[]对应api的char,java的char[]对应api的wchar

而且应该使用Native.toString来转换到java的String。

 

int的指针对应IntByReference,其他类推。


Total views.

© 2013 - 2024. All rights reserved.

Powered by Hydejack v6.6.1