use of com.sun.jna.Pointer in project jna by java-native-access.
the class Advapi32Test method testReadEncryptedFileRaw.
public void testReadEncryptedFileRaw() throws Exception {
// create an encrypted file
File file = createTempFile();
String lpFileName = file.getAbsolutePath();
assertTrue("EncryptFile(" + lpFileName + ")", Advapi32.INSTANCE.EncryptFile(lpFileName));
// open file for export
ULONG ulFlags = new ULONG(0);
PointerByReference pvContext = new PointerByReference();
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lpFileName, ulFlags, pvContext));
// read encrypted file
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
FE_EXPORT_FUNC pfExportCallback = new FE_EXPORT_FUNC() {
@Override
public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONG ulLength) {
if (pbData == null) {
throw new NullPointerException("Callback data unexpectedly missing");
}
byte[] arr = pbData.getByteArray(0, ulLength.intValue());
try {
outputStream.write(arr);
} catch (IOException e) {
throw new RuntimeException(e);
}
return new DWORD(W32Errors.ERROR_SUCCESS);
}
};
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.ReadEncryptedFileRaw(pfExportCallback, null, pvContext.getValue()));
outputStream.close();
Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
file.delete();
}
use of com.sun.jna.Pointer in project jna by java-native-access.
the class ITypeLibTest method testFindName.
public void testFindName() {
ITypeLib shellTypeLib = loadShellTypeLib();
// The found member is Count, search done with lowercase value to test
// correct behaviour (search is case insensitive)
String memberValue = "count";
String memberValueOk = "Count";
Pointer p = Ole32.INSTANCE.CoTaskMemAlloc((memberValue.length() + 1L) * Native.WCHAR_SIZE);
WTypes.LPOLESTR olestr = new WTypes.LPOLESTR(p);
olestr.setValue(memberValue);
short maxResults = 100;
ULONG lHashVal = new ULONG(0);
USHORTByReference pcFound = new USHORTByReference(maxResults);
Pointer[] pointers = new Pointer[maxResults];
MEMBERID[] rgMemId = new MEMBERID[maxResults];
HRESULT hr = shellTypeLib.FindName(olestr, lHashVal, pointers, rgMemId, pcFound);
assertTrue(COMUtils.SUCCEEDED(hr));
// If a reader can come up with more tests it would be appretiated,
// the documentation is unclear what more can be expected
// 2 matches come from manual tests
assertTrue(pcFound.getValue().intValue() == 2);
// Check that function return corrected member name (Count) - see uppercase C
assertEquals(memberValueOk, olestr.getValue());
// There have to be as many pointers as reported by pcFound
assertNotNull(pointers[0]);
assertNotNull(pointers[1]);
// Might be flaky, contract only defined positions 0 -> (pcFound - 1)
assertNull(pointers[2]);
// Test access to second value
TypeInfo secondTypeInfo = new TypeInfo(pointers[1]);
PointerByReference pbr = new PointerByReference();
hr = secondTypeInfo.GetTypeAttr(pbr);
assertTrue(COMUtils.SUCCEEDED(hr));
OaIdl.TYPEATTR pTypeAttr = new OaIdl.TYPEATTR(pbr.getValue());
// Either interface FolderItemVerbs ({1F8352C0-50B0-11CF-960C-0080C7F4EE85})
// or FolderItems ({744129E0-CBE5-11CE-8350-444553540000})
String typeGUID = pTypeAttr.guid.toGuidString();
assertTrue(typeGUID.equals("{1F8352C0-50B0-11CF-960C-0080C7F4EE85}") || typeGUID.equals("{744129E0-CBE5-11CE-8350-444553540000}"));
secondTypeInfo.ReleaseTypeAttr(pTypeAttr);
Ole32.INSTANCE.CoTaskMemFree(olestr.getPointer());
}
use of com.sun.jna.Pointer in project jna by java-native-access.
the class ITypeLibTest method testIsName.
public void testIsName() {
ITypeLib shellTypeLib = loadShellTypeLib();
String memberValue = "Folder";
Pointer p = Ole32.INSTANCE.CoTaskMemAlloc((memberValue.length() + 1L) * Native.WCHAR_SIZE);
WTypes.LPOLESTR olestr = new WTypes.LPOLESTR(p);
olestr.setValue(memberValue);
WinDef.BOOLByReference boolByRef = new WinDef.BOOLByReference();
HRESULT hr = shellTypeLib.IsName(olestr, new ULONG(0), boolByRef);
assertTrue(COMUtils.SUCCEEDED(hr));
// Folder is a member
assertTrue(boolByRef.getValue().booleanValue());
Ole32.INSTANCE.CoTaskMemFree(p);
}
use of com.sun.jna.Pointer in project jmonkeyengine by jMonkeyEngine.
the class OpenVRUtil method getTrackedDeviceStringProperty.
/**
* Get the value of the given string {@link JOpenVRLibrary.ETrackedDeviceProperty property} attached to the given device.
* @param system the underlying OpenVR system.
* @param deviceIndex the index of the device to query.
* @param property the property to query.
* @param bufferSize the size of the buffer to use for storing native string.
* @return the value of the given string property attached to the given device.
* @see OpenVRInput#getTrackedControllerCount()
* @see JOpenVRLibrary.ETrackedDeviceProperty
* @see #getTrackedDeviceStringProperty(VR_IVRSystem_FnTable, int, int)
*/
public static String getTrackedDeviceStringProperty(VR_IVRSystem_FnTable system, int deviceIndex, int property, int bufferSize) {
String str = "";
int unBufferSize = 256;
Pointer pchValue = new Memory(unBufferSize);
IntByReference pError = new IntByReference();
system.GetStringTrackedDeviceProperty.apply(deviceIndex, property, pchValue, unBufferSize, pError);
if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_Success) {
str = pchValue.getString(0);
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_BufferTooSmall) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_CouldNotContactServer) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_InvalidDevice) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_InvalidOperation) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_NotYetAvailable) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_PermissionDenied) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_StringExceedsMaximumLength) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_UnknownProperty) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_ValueNotProvidedByDevice) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_WrongDataType) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_WrongDeviceClass) {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
} else {
throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
}
return str;
}
use of com.sun.jna.Pointer in project jna by java-native-access.
the class SAFEARRAYTest method toArrayDirect.
private Object[] toArrayDirect(SAFEARRAY wrap) {
Pointer dataPointer = wrap.accessData();
long rowMax = wrap.getUBound(2);
long columnMax = wrap.getUBound(1);
VARIANT[] variantData = (VARIANT[]) new VARIANT(dataPointer).toArray((int) ((rowMax + 1) * (columnMax + 1)));
Object[][] result = new Object[(int) (rowMax + 1)][(int) (columnMax + 1)];
for (long i = 0; i <= rowMax; i++) {
long rowOffset = i * columnMax;
for (long j = 0; j <= columnMax; j++) {
VARIANT cell = variantData[(int) (rowOffset + j)];
result[(int) i][(int) j] = cell.getValue();
}
}
wrap.unaccessData();
return result;
}
Aggregations