001: /* Copyright (c) 2007 Timothy Wall, All Rights Reserved
002: *
003: * This library is free software; you can redistribute it and/or
004: * modify it under the terms of the GNU Lesser General Public
005: * License as published by the Free Software Foundation; either
006: * version 2.1 of the License, or (at your option) any later version.
007: * <p/>
008: * This library is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: */
013: package org.netbeans.core.nativeaccess.transparency.win32;
014:
015: import java.nio.Buffer;
016: import com.sun.jna.FromNativeContext;
017: import com.sun.jna.Native;
018: import com.sun.jna.NativeMapped;
019: import com.sun.jna.Pointer;
020: import com.sun.jna.PointerType;
021: import com.sun.jna.Structure;
022: import com.sun.jna.ptr.ByReference;
023: import com.sun.jna.ptr.IntByReference;
024: import com.sun.jna.ptr.PointerByReference;
025:
026: /** Definition (incomplete) of <code>kernel32.dll</code>. */
027: public interface Kernel32 extends W32API {
028:
029: Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32",
030: Kernel32.class, DEFAULT_OPTIONS);
031:
032: public static class SYSTEMTIME extends Structure {
033: public short wYear;
034: public short wMonth;
035: public short wDayOfWeek;
036: public short wDay;
037: public short wHour;
038: public short wMinute;
039: public short wSecond;
040: public short wMilliseconds;
041: }
042:
043: Pointer LocalFree(Pointer hLocal);
044:
045: Pointer GlobalFree(Pointer hGlobal);
046:
047: HMODULE GetModuleHandle(String name);
048:
049: void GetSystemTime(SYSTEMTIME result);
050:
051: int GetCurrentThreadId();
052:
053: HANDLE GetCurrentThread();
054:
055: int GetCurrentProcessId();
056:
057: HANDLE GetCurrentProcess();
058:
059: int GetProcessId(HANDLE process);
060:
061: int GetLastError();
062:
063: void SetLastError(int dwErrCode);
064:
065: int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x0100;
066: int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
067: int FORMAT_MESSAGE_IGNORE_INSERTS = 0x200;
068:
069: int FormatMessage(int dwFlags, Pointer lpSource, int dwMessageId,
070: int dwLanguageId, PointerByReference lpBuffer, int nSize,
071: Pointer va_list);
072:
073: int FormatMessage(int dwFlags, Pointer lpSource, int dwMessageId,
074: int dwLanguageId, Buffer lpBuffer, int nSize,
075: Pointer va_list);
076:
077: int FILE_LIST_DIRECTORY = 0x00000001;
078:
079: int FILE_SHARE_READ = 1;
080: int FILE_SHARE_WRITE = 2;
081: int FILE_SHARE_DELETE = 4;
082:
083: int CREATE_NEW = 1;
084: int CREATE_ALWAYS = 2;
085: int OPEN_EXISTING = 3;
086: int OPEN_ALWAYS = 4;
087: int TRUNCATE_EXISTING = 5;
088:
089: int FILE_FLAG_WRITE_THROUGH = 0x80000000;
090: int FILE_FLAG_OVERLAPPED = 0x40000000;
091: int FILE_FLAG_NO_BUFFERING = 0x20000000;
092: int FILE_FLAG_RANDOM_ACCESS = 0x10000000;
093: int FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000;
094: int FILE_FLAG_DELETE_ON_CLOSE = 0x04000000;
095: int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
096: int FILE_FLAG_POSIX_SEMANTICS = 0x01000000;
097: int FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000;
098: int FILE_FLAG_OPEN_NO_RECALL = 0x00100000;
099:
100: int FILE_ATTRIBUTE_READONLY = 0x00000001;
101: int FILE_ATTRIBUTE_HIDDEN = 0x00000002;
102: int FILE_ATTRIBUTE_SYSTEM = 0x00000004;
103: int FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
104: int FILE_ATTRIBUTE_ARCHIVE = 0x00000020;
105: int FILE_ATTRIBUTE_DEVICE = 0x00000040;
106: int FILE_ATTRIBUTE_NORMAL = 0x00000080;
107: int FILE_ATTRIBUTE_TEMPORARY = 0x00000100;
108: int FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200;
109: int FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400;
110: int FILE_ATTRIBUTE_COMPRESSED = 0x00000800;
111: int FILE_ATTRIBUTE_OFFLINE = 0x00001000;
112: int FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000;
113: int FILE_ATTRIBUTE_ENCRYPTED = 0x00004000;
114:
115: int GENERIC_WRITE = 0x40000000;
116:
117: public static class SECURITY_ATTRIBUTES extends Structure {
118: public int nLength = size();
119: public Pointer lpSecurityDescriptor;
120: public boolean bInheritHandle;
121: }
122:
123: HANDLE CreateFile(String lpFileName, int dwDesiredAccess,
124: int dwShareMode, SECURITY_ATTRIBUTES lpSecurityAttributes,
125: int dwCreationDisposition, int dwFlagsAndAttributes,
126: HANDLE hTemplateFile);
127:
128: boolean CreateDirectory();
129:
130: HANDLE CreateIoCompletionPort(HANDLE FileHandle,
131: HANDLE ExistingCompletionPort, Pointer CompletionKey,
132: int NumberOfConcurrentThreads);
133:
134: int INFINITE = 0xFFFFFFFF;
135:
136: boolean GetQueuedCompletionStatus(HANDLE CompletionPort,
137: IntByReference lpNumberOfBytes,
138: ByReference lpCompletionKey,
139: PointerByReference lpOverlapped, int dwMilliseconds);
140:
141: boolean PostQueuedCompletionStatus(HANDLE CompletionPort,
142: int dwNumberOfBytesTransferred, Pointer dwCompletionKey,
143: OVERLAPPED lpOverlapped);
144:
145: int WaitForSingleObject(HANDLE hHandle, int dwMilliseconds);
146:
147: boolean DuplicateHandle(HANDLE hSourceProcessHandle,
148: HANDLE hSourceHandle, HANDLE hTargetProcessHandle,
149: HANDLEByReference lpTargetHandle, int dwDesiredAccess,
150: boolean bInheritHandle, int dwOptions);
151:
152: boolean CloseHandle(HANDLE hObject);
153:
154: int FILE_ACTION_ADDED = 1;
155: int FILE_ACTION_REMOVED = 2;
156: int FILE_ACTION_MODIFIED = 3;
157: int FILE_ACTION_RENAMED_OLD_NAME = 4;
158: int FILE_ACTION_RENAMED_NEW_NAME = 5;
159:
160: int FILE_NOTIFY_CHANGE_FILE_NAME = 1;
161: int FILE_NOTIFY_CHANGE_DIR_NAME = 2;
162: int FILE_NOTIFY_CHANGE_NAME = 3;
163: int FILE_NOTIFY_CHANGE_ATTRIBUTES = 4;
164: int FILE_NOTIFY_CHANGE_SIZE = 8;
165: int FILE_NOTIFY_CHANGE_LAST_WRITE = 16;
166: int FILE_NOTIFY_CHANGE_LAST_ACCESS = 32;
167: int FILE_NOTIFY_CHANGE_CREATION = 64;
168: int FILE_NOTIFY_CHANGE_EA = 128;
169: int FILE_NOTIFY_CHANGE_SECURITY = 256;
170: int FILE_NOTIFY_CHANGE_STREAM_NAME = 512;
171: int FILE_NOTIFY_CHANGE_STREAM_SIZE = 1024;
172: int FILE_NOTIFY_CHANGE_STREAM_WRITE = 2048;
173:
174: /** This structure is non-trivial since it is a pattern stamped
175: * into a large block of result memory rather than something that stands
176: * alone or is used for input.
177: */
178: public static class FILE_NOTIFY_INFORMATION extends Structure {
179: public int NextEntryOffset;
180: public int Action;
181: public int FileNameLength;
182: // filename is not nul-terminated, so we can't use a String/WString
183: public char[] FileName = new char[1];
184:
185: private FILE_NOTIFY_INFORMATION() {
186: }
187:
188: public FILE_NOTIFY_INFORMATION(int size) {
189: if (size < size())
190: throw new IllegalArgumentException(
191: "Size must greater than " + size()
192: + ", requested " + size);
193: allocateMemory(size);
194: }
195:
196: /** WARNING: this filename may be either the short or long form
197: * of the filename.
198: */
199: public String getFilename() {
200: return new String(FileName, 0, FileNameLength / 2);
201: }
202:
203: public void read() {
204: // avoid reading filename until we know how long it is
205: FileName = new char[0];
206: super .read();
207: FileName = getPointer()
208: .getCharArray(12, FileNameLength / 2);
209: }
210:
211: public FILE_NOTIFY_INFORMATION next() {
212: if (NextEntryOffset == 0)
213: return null;
214: FILE_NOTIFY_INFORMATION next = new FILE_NOTIFY_INFORMATION();
215: next.useMemory(getPointer(), NextEntryOffset);
216: next.read();
217: return next;
218: }
219: }
220:
221: public static class OVERLAPPED extends Structure {
222: public int Internal;
223: public int InternalHigh;
224: public int Offset;
225: public int OffsetHigh;
226: public Pointer hEvent;
227: }
228:
229: // TODO: figure out how OVERLAPPED is used and apply an appropriate mapping
230: public static interface OVERLAPPED_COMPLETION_ROUTINE extends
231: StdCallCallback {
232: void callback(int errorCode, int nBytesTransferred,
233: OVERLAPPED overlapped);
234: }
235:
236: /** NOTE: only exists in unicode form (W suffix). Define this method
237: * explicitly with the W suffix to avoid inadvertent calls in ASCII mode.
238: */
239: boolean ReadDirectoryChangesW(HANDLE directory,
240: FILE_NOTIFY_INFORMATION info, int length,
241: boolean watchSubtree, int notifyFilter,
242: IntByReference bytesReturned, OVERLAPPED overlapped,
243: OVERLAPPED_COMPLETION_ROUTINE completionRoutine);
244:
245: }
|