use of com.sun.jna.platform.win32.WinDef.DWORD in project jna by java-native-access.
the class Kernel32Test method testGetPrivateProfileString.
public final void testGetPrivateProfileString() throws IOException {
final File tmp = File.createTempFile("testGetPrivateProfileString", ".ini");
tmp.deleteOnExit();
final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
writer.println("[Section]");
writer.println("existingKey = ABC");
writer.close();
final char[] buffer = new char[8];
DWORD len = Kernel32.INSTANCE.GetPrivateProfileString("Section", "existingKey", "DEF", buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
assertEquals(3, len.intValue());
assertEquals("ABC", Native.toString(buffer));
len = Kernel32.INSTANCE.GetPrivateProfileString("Section", "missingKey", "DEF", buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
assertEquals(3, len.intValue());
assertEquals("DEF", Native.toString(buffer));
}
use of com.sun.jna.platform.win32.WinDef.DWORD in project jna by java-native-access.
the class Kernel32Test method testModule32NextW.
public void testModule32NextW() {
HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, new DWORD(Kernel32.INSTANCE.GetCurrentProcessId()));
if (snapshot == null) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
Win32Exception we = null;
Tlhelp32.MODULEENTRY32W first = new Tlhelp32.MODULEENTRY32W();
try {
if (!Kernel32.INSTANCE.Module32NextW(snapshot, first)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
// not sure if this will be run against java.exe or javaw.exe but this
// check tests both
assertTrue("The first module in the current process should be java.exe or javaw.exe", first.szModule().startsWith("java"));
assertEquals("The process ID of the module ID should be our process ID", Kernel32.INSTANCE.GetCurrentProcessId(), first.th32ProcessID.intValue());
} catch (Win32Exception e) {
we = e;
// re-throw so finally block is executed
throw we;
} finally {
try {
Kernel32Util.closeHandle(snapshot);
} catch (Win32Exception e) {
if (we == null) {
we = e;
} else {
we.addSuppressed(e);
}
}
if (we != null) {
throw we;
}
}
}
use of com.sun.jna.platform.win32.WinDef.DWORD in project jna by java-native-access.
the class EnumMoniker method iterator.
@Override
public Iterator<IDispatch> iterator() {
return new Iterator<IDispatch>() {
@Override
public boolean hasNext() {
return null != EnumMoniker.this.rawNext;
}
@Override
public IDispatch next() {
assert COMUtils.comIsInitialized() : "COM not initialized";
final Moniker moniker = EnumMoniker.this.rawNext;
final PointerByReference ppunkObject = new PointerByReference();
WinNT.HRESULT hr = EnumMoniker.this.rawRot.GetObject(moniker.getPointer(), ppunkObject);
COMUtils.checkRC(hr);
// To assist debug, can use the following code
// PointerByReference ppbc = new
// PointerByReference();
// Ole32.INSTANCE.CreateBindCtx(new DWORD(), ppbc);
//
// BSTRByReference ppszDisplayName = new
// BSTRByReference();
// hr = moniker.GetDisplayName(ppbc.getValue(),
// moniker.getPointer(), ppszDisplayName);
// COMUtils.checkRC(hr);
// String name = ppszDisplayName.getString();
// Ole32.INSTANCE.CoTaskMemFree(ppszDisplayName.getPointer().getPointer(0));
// TODO: Can we assume that the object is an
// IDispatch ?
// Unknown unk = new
// Unknown(ppunkObject.getValue());
Dispatch dispatch = new Dispatch(ppunkObject.getValue());
EnumMoniker.this.cacheNext();
IDispatch d = EnumMoniker.this.factory.createProxy(IDispatch.class, dispatch);
//must release a COM Ref, GetObject returns a pointer with +1
int n = dispatch.Release();
return d;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
}
use of com.sun.jna.platform.win32.WinDef.DWORD in project jna by java-native-access.
the class Kernel32Util method getModules.
/**
* Returns all the executable modules for a given process ID.<br>
*
* @param processID
* The process ID to get executable modules for
* @return All the modules in the process.
*/
public static List<Tlhelp32.MODULEENTRY32W> getModules(int processID) {
HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, new DWORD(processID));
if (snapshot == null) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
Win32Exception we = null;
try {
Tlhelp32.MODULEENTRY32W first = new Tlhelp32.MODULEENTRY32W();
if (!Kernel32.INSTANCE.Module32FirstW(snapshot, first)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
List<Tlhelp32.MODULEENTRY32W> modules = new ArrayList<Tlhelp32.MODULEENTRY32W>();
modules.add(first);
Tlhelp32.MODULEENTRY32W next = new Tlhelp32.MODULEENTRY32W();
while (Kernel32.INSTANCE.Module32NextW(snapshot, next)) {
modules.add(next);
next = new Tlhelp32.MODULEENTRY32W();
}
int lastError = Kernel32.INSTANCE.GetLastError();
// or if something went wrong.
if (lastError != W32Errors.ERROR_SUCCESS && lastError != W32Errors.ERROR_NO_MORE_FILES) {
throw new Win32Exception(lastError);
}
return modules;
} catch (Win32Exception e) {
we = e;
// re-throw so finally block is executed
throw we;
} finally {
try {
closeHandle(snapshot);
} catch (Win32Exception e) {
if (we == null) {
we = e;
} else {
we.addSuppressed(e);
}
}
if (we != null) {
throw we;
}
}
}
use of com.sun.jna.platform.win32.WinDef.DWORD in project jna by java-native-access.
the class Shell32Util method getFolderPath.
/**
* Get a special folder path.
* @param hwnd
* Parent window.
* @param nFolder
* Folder CSLID.
* @param dwFlags
* Flags.
* @return
* Special folder.
*/
public static String getFolderPath(HWND hwnd, int nFolder, DWORD dwFlags) {
char[] pszPath = new char[WinDef.MAX_PATH];
HRESULT hr = Shell32.INSTANCE.SHGetFolderPath(hwnd, nFolder, null, dwFlags, pszPath);
if (!hr.equals(W32Errors.S_OK)) {
throw new Win32Exception(hr);
}
return Native.toString(pszPath);
}
Aggregations