use of com.sun.jna.Pointer in project jna by java-native-access.
the class Kernel32UtilTest method testFreeLocalMemory.
public void testFreeLocalMemory() {
try {
Pointer ptr = new Pointer(0xFFFFFFFFFFFFFFFFL);
Kernel32Util.freeLocalMemory(ptr);
fail("Unexpected success to free bad local memory");
} catch (Win32Exception e) {
HRESULT hr = e.getHR();
int code = W32Errors.HRESULT_CODE(hr.intValue());
assertEquals("Mismatched failure reason code", WinError.ERROR_INVALID_HANDLE, code);
}
}
use of com.sun.jna.Pointer in project jna by java-native-access.
the class SystemBTest method testSysctl.
public void testSysctl() {
final String mibName = "hw.logicalcpu";
final int nCpu = Runtime.getRuntime().availableProcessors();
IntByReference size = new IntByReference(SystemB.INT_SIZE);
Pointer p = new Memory(size.getValue());
int ret = SystemB.INSTANCE.sysctlbyname(mibName, p, size, null, 0);
assertEquals(ret, 0);
// These values should be equal unless affinity is set, limiting nCpu
assertTrue(p.getInt(0) >= nCpu);
size = new IntByReference();
ret = SystemB.INSTANCE.sysctlnametomib(mibName, null, size);
assertEquals(ret, 0);
// Size should be 2
assertEquals(size.getValue(), 2);
Pointer mibp = new Memory(size.getValue() * SystemB.INT_SIZE);
ret = SystemB.INSTANCE.sysctlnametomib(mibName, mibp, size);
assertEquals(ret, 0);
// Size should be 2
assertEquals(size.getValue(), 2);
int[] mib = mibp.getIntArray(0, size.getValue());
// mib should be { 6, 103(?) }
assertEquals(mib.length, 2);
assertEquals(mib[0], 6);
size = new IntByReference(SystemB.INT_SIZE);
p = new Memory(size.getValue());
ret = SystemB.INSTANCE.sysctl(mib, mib.length, p, size, null, 0);
assertTrue(p.getInt(0) >= nCpu);
}
use of com.sun.jna.Pointer in project jna by java-native-access.
the class Kernel32Util method formatMessage.
/**
* Format a message from the value obtained from
* {@link Kernel32#GetLastError()} or {@link Native#getLastError()}.
*
* @param code The error code
* @return Formatted message.
*/
public static String formatMessage(int code) {
PointerByReference buffer = new PointerByReference();
int nLen = Kernel32.INSTANCE.FormatMessage(WinBase.FORMAT_MESSAGE_ALLOCATE_BUFFER | WinBase.FORMAT_MESSAGE_FROM_SYSTEM | WinBase.FORMAT_MESSAGE_IGNORE_INSERTS, null, code, // TODO: // MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)
0, buffer, 0, null);
if (nLen == 0) {
throw new LastErrorException(Native.getLastError());
}
Pointer ptr = buffer.getValue();
try {
String s = ptr.getWideString(0);
return s.trim();
} finally {
freeLocalMemory(ptr);
}
}
use of com.sun.jna.Pointer in project jna by java-native-access.
the class OaIdlUtil method toPrimitiveArray.
/**
* Read SAFEARRAY into a java array. Not all VARTYPEs are supported!
*
* <p>
* Supported types:</p>
* <ul>
* <li>VT_BOOL</li>
* <li>VT_UI1</li>
* <li>VT_I1</li>
* <li>VT_UI2</li>
* <li>VT_I2</li>
* <li>VT_UI4</li>
* <li>VT_UINT</li>
* <li>VT_I4</li>
* <li>VT_INT</li>
* <li>VT_ERROR</li>
* <li>VT_R4</li>
* <li>VT_R8</li>
* <li>VT_DATE</li>
* <li>VT_BSTR</li>
* <li>VT_VARIANT (Onle the following VARTYPES):
* <ul>
* <li>VT_EMPTY (converted to NULL)</li>
* <li>VT_NULL</li>
* <li>VT_BOOL</li>
* <li>VT_UI1</li>
* <li>VT_I1</li>
* <li>VT_UI2</li>
* <li>VT_I2</li>
* <li>VT_UI4</li>
* <li>VT_UINT</li>
* <li>VT_I4</li>
* <li>VT_INT</li>
* <li>VT_ERROR</li>
* <li>VT_R4</li>
* <li>VT_R8</li>
* <li>VT_DATE</li>
* <li>VT_BSTR</li>
* </ul>
* </li>
* </ul>
*
* @param sa SAFEARRAY to convert
* @param destruct if true the supplied SAFEARRAY is destroyed, there must
* not be additional locks on the array!
* @return Java array corresponding to the given SAFEARRAY
*/
public static Object toPrimitiveArray(SAFEARRAY sa, boolean destruct) {
Pointer dataPointer = sa.accessData();
try {
int dimensions = sa.getDimensionCount();
int[] minIdx = new int[dimensions];
int[] maxIdx = new int[dimensions];
int[] elements = new int[dimensions];
int[] cumElements = new int[dimensions];
int varType = sa.getVarType().intValue();
for (int i = 0; i < dimensions; i++) {
minIdx[i] = sa.getLBound(i);
maxIdx[i] = sa.getUBound(i);
elements[i] = maxIdx[i] - minIdx[i] + 1;
}
for (int i = dimensions - 1; i >= 0; i--) {
if (i == (dimensions - 1)) {
cumElements[i] = 1;
} else {
cumElements[i] = cumElements[i + 1] * elements[i + 1];
}
}
if (dimensions == 0) {
throw new IllegalArgumentException("Supplied Array has no dimensions.");
}
int elementCount = cumElements[0] * elements[0];
Object sourceArray;
switch(varType) {
case VT_UI1:
case VT_I1:
sourceArray = dataPointer.getByteArray(0, elementCount);
break;
case VT_BOOL:
case VT_UI2:
case VT_I2:
sourceArray = dataPointer.getShortArray(0, elementCount);
break;
case VT_UI4:
case VT_UINT:
case VT_I4:
case VT_INT:
case VT_ERROR:
sourceArray = dataPointer.getIntArray(0, elementCount);
break;
case VT_R4:
sourceArray = dataPointer.getFloatArray(0, elementCount);
break;
case VT_R8:
case VT_DATE:
sourceArray = dataPointer.getDoubleArray(0, elementCount);
break;
case VT_BSTR:
sourceArray = dataPointer.getPointerArray(0, elementCount);
break;
case VT_VARIANT:
VARIANT variant = new VARIANT(dataPointer);
sourceArray = variant.toArray(elementCount);
break;
case VT_UNKNOWN:
case VT_DISPATCH:
case VT_CY:
case VT_DECIMAL:
case VT_RECORD:
default:
throw new IllegalStateException("Type not supported: " + varType);
}
Object targetArray = Array.newInstance(Object.class, elements);
toPrimitiveArray(sourceArray, targetArray, minIdx, maxIdx, elements, cumElements, varType, new int[0]);
return targetArray;
} finally {
sa.unaccessData();
if (destruct) {
sa.destroy();
}
}
}
use of com.sun.jna.Pointer in project jna by java-native-access.
the class W32Service method getFailureActionsFlag.
/**
* Get the failure actions flag of the specified service. Corresponds to
* <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms681988.aspx">QueryServiceConfig2</a>
* with parameter dwInfoLevel set to SERVICE_CONFIG_FAILURE_ACTIONS_FLAG.
*/
public boolean getFailureActionsFlag() {
Pointer buffer = queryServiceConfig2(Winsvc.SERVICE_CONFIG_FAILURE_ACTIONS_FLAG);
SERVICE_FAILURE_ACTIONS_FLAG result = new SERVICE_FAILURE_ACTIONS_FLAG(buffer);
return result.fFailureActionsOnNonCrashFailures != 0;
}
Aggregations