下面列出了怎么用com.sun.jna.platform.win32.WinDef.DWORD的API类实例代码及写法,或者点击链接到github查看源代码。
public static List<ProcessInfo> allProcesses() {
List<ProcessInfo> processList = new ArrayList<ProcessInfo>();
HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(
Tlhelp32.TH32CS_SNAPPROCESS, new DWORD(0));
try {
Tlhelp32.PROCESSENTRY32.ByReference pe
= new Tlhelp32.PROCESSENTRY32.ByReference();
for (boolean more = Kernel32.INSTANCE.Process32First(snapshot, pe);
more;
more = Kernel32.INSTANCE.Process32Next(snapshot, pe)) {
int pid = pe.th32ProcessID.intValue();
String name = getProcessImageName(pe.th32ProcessID.intValue());
if (null == name) {
continue;
}
processList.add(new ProcessInfo(pid, name));
}
return processList;
} finally {
Kernel32.INSTANCE.CloseHandle(snapshot);
}
}
/**
* Sends an action (flags) at the given x,y coordinates.
*
* @param x
* @param y
* @param flags
*/
public static void mouseAction(int x, int y, int flags) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_MOUSE);
input.input.setType("mi");
if (x != -1) {
input.input.mi.dx = new LONG(x);
}
if (y != -1) {
input.input.mi.dy = new LONG(y);
}
input.input.mi.time = new DWORD(0);
input.input.mi.dwExtraInfo = new ULONG_PTR(0);
input.input.mi.dwFlags = new DWORD(flags);
User32.INSTANCE.SendInput(new DWORD(1), new INPUT[] { input }, input.size());
}
private List<WinProcess> getChildren() throws IOException {
ArrayList<WinProcess> result = new ArrayList<WinProcess>();
WinNT.HANDLE hSnap = this.kernel32lib.CreateToolhelp32Snapshot(Kernel32Lib.TH32CS_SNAPPROCESS, new DWORD(0));
Kernel32Lib.PROCESSENTRY32.ByReference ent = new Kernel32Lib.PROCESSENTRY32.ByReference();
if (!this.kernel32lib.Process32First(hSnap, ent)) {
return result;
}
do {
if (ent.th32ParentProcessID.intValue() == this.pid) {
try {
result.add(new WinProcess(ent.th32ProcessID.intValue()));
}
catch (IOException e) {
System.err.println("WinProcess::getChildren, IOException " + e);
}
}
}
while (this.kernel32lib.Process32Next(hSnap, ent));
Kernel32.INSTANCE.CloseHandle(hSnap);
return result;
}
static long findProcessID( String processName )
{
Tlhelp32.PROCESSENTRY32.ByReference processInfo = new Tlhelp32.PROCESSENTRY32.ByReference();
WinNT.HANDLE processSnapshotHandle =
kernel32.CreateToolhelp32Snapshot( Tlhelp32.TH32CS_SNAPPROCESS, new DWORD( 0L ) );
try
{
kernel32.Process32First( processSnapshotHandle, processInfo );
if ( processName.equals( Native.toString( processInfo.szExeFile ) ) )
{
return processInfo.th32ProcessID.longValue();
}
while ( kernel32.Process32Next( processSnapshotHandle, processInfo ) )
{
if ( processName.equals( Native.toString( processInfo.szExeFile ) ) )
{
return processInfo.th32ProcessID.longValue();
}
}
return 0L;
}
finally
{
kernel32.CloseHandle( processSnapshotHandle );
}
}
/**
* Sends a key-down input followed by a key-up input for the given character
* value c.
*
* @param c
*/
public static void pressKey(int c) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_KEYBOARD);
input.input.setType("ki");
input.input.ki.wScan = new WORD(0);
input.input.ki.time = new DWORD(0);
input.input.ki.dwExtraInfo = new ULONG_PTR(0);
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYDOWN);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYUP);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
}
/**
* Sends a key-down input for the given character value c.
*
* @param c
*/
public static void sendKeyDown(int c) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_KEYBOARD);
input.input.setType("ki");
input.input.ki.wScan = new WORD(0);
input.input.ki.time = new DWORD(0);
input.input.ki.dwExtraInfo = new ULONG_PTR(0);
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYDOWN);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
}
/**
* Sends a key-up input for the given character value c.
*
* @param c
*/
public static void sendKeyUp(int c) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_KEYBOARD);
input.input.setType("ki");
input.input.ki.wScan = new WORD(0);
input.input.ki.time = new DWORD(0);
input.input.ki.dwExtraInfo = new ULONG_PTR(0);
input.input.ki.wVk = new WORD(c);
input.input.ki.dwFlags = new DWORD(KEYEVENTF_KEYUP);
User32.INSTANCE.SendInput(new DWORD(1), (INPUT[]) input.toArray(1), input.size());
}
public Win32Process(String filePath, String fileName, BufferedImage icon, DWORD th32ProcessID) {
this.filePath = filePath;
this.fileName = fileName;
this.icon = icon;
this.th32ProcessID = th32ProcessID;
}
public PROCESSENTRY32() {
dwSize = new WinDef.DWORD(size());
}
/**
* Takes a snapshot of the specified processes, as well as the heaps, modules, and threads used by these processes.
*
* @param dwFlags The portions of the system to be included in the snapshot.
* @param th32ProcessID The process identifier of the process to be included in the snapshot. This parameter can be zero to indicate
* the current process. This parameter is used when the TH32CS_SNAPHEAPLIST, TH32CS_SNAPMODULE,
* TH32CS_SNAPMODULE32, or TH32CS_SNAPALL value is specified. Otherwise, it is ignored and all processes are
* included in the snapshot.
* <p/>
* If the specified process is the Idle process or one of the CSRSS processes, this function fails and the last
* error code is ERROR_ACCESS_DENIED because their access restrictions prevent user-level code from opening them.
* <p/>
* If the specified process is a 64-bit process and the caller is a 32-bit process, this function fails and the
* last error code is ERROR_PARTIAL_COPY (299).
* @return If the function succeeds, it returns an open handle to the specified snapshot.
* <p/>
* If the function fails, it returns INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
* Possible error codes include ERROR_BAD_LENGTH.
*/
public WinNT.HANDLE CreateToolhelp32Snapshot(WinDef.DWORD dwFlags, WinDef.DWORD th32ProcessID);
/**
* Controls whether the system will handle the specified types of serious errors or whether the process will handle them.
* See: http://msdn.microsoft.com/en-us/library/ms680621%28VS.85%29.aspx
*
* @param uMode The process error mode. This parameter can be one or more of the following values.
*/
public int SetErrorMode(DWORD uMode);
DWORD GetPixel(HDC hdc, int nXPos, int nYPos);
HANDLE CreateSolidBrush(DWORD crColor);