use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.
the class Advapi32Util method registryValueExists.
/**
* Checks whether a registry value exists.
*
* @param root
* HKEY_LOCAL_MACHINE, etc.
* @param key
* Registry key path.
* @param value
* Value name.
* @return True if the value exists.
*/
public static boolean registryValueExists(HKEY root, String key, String value) {
HKEYByReference phkKey = new HKEYByReference();
int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, phkKey);
try {
switch(rc) {
case W32Errors.ERROR_SUCCESS:
break;
case W32Errors.ERROR_FILE_NOT_FOUND:
return false;
default:
throw new Win32Exception(rc);
}
IntByReference lpcbData = new IntByReference();
IntByReference lpType = new IntByReference();
rc = Advapi32.INSTANCE.RegQueryValueEx(phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData);
switch(rc) {
case W32Errors.ERROR_SUCCESS:
case W32Errors.ERROR_MORE_DATA:
case W32Errors.ERROR_INSUFFICIENT_BUFFER:
return true;
case W32Errors.ERROR_FILE_NOT_FOUND:
return false;
default:
throw new Win32Exception(rc);
}
} finally {
if (phkKey.getValue() != WinBase.INVALID_HANDLE_VALUE) {
rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
}
}
}
use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.
the class Secur32Util method getSecurityPackages.
/**
* Get the security packages installed on the current computer.
* @return
* An array of SSPI security packages.
*/
public static SecurityPackage[] getSecurityPackages() {
IntByReference pcPackages = new IntByReference();
PSecPkgInfo pPackageInfo = new PSecPkgInfo();
int rc = Secur32.INSTANCE.EnumerateSecurityPackages(pcPackages, pPackageInfo);
if (W32Errors.SEC_E_OK != rc) {
throw new Win32Exception(rc);
}
SecPkgInfo[] packagesInfo = pPackageInfo.toArray(pcPackages.getValue());
ArrayList<SecurityPackage> packages = new ArrayList<SecurityPackage>(pcPackages.getValue());
for (SecPkgInfo packageInfo : packagesInfo) {
SecurityPackage securityPackage = new SecurityPackage();
securityPackage.name = packageInfo.Name.toString();
securityPackage.comment = packageInfo.Comment.toString();
packages.add(securityPackage);
}
rc = Secur32.INSTANCE.FreeContextBuffer(pPackageInfo.pPkgInfo.getPointer());
if (W32Errors.SEC_E_OK != rc) {
throw new Win32Exception(rc);
}
return packages.toArray(new SecurityPackage[0]);
}
use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.
the class Shell32Util method getKnownFolderPath.
/**
* Retrieves the full path of a known folder identified by the folder's KNOWNFOLDERID. This function replaces
* {@link #getFolderPath}. That older function is now simply a wrapper for getKnownFolderPath
* @param guid the KNOWNFOLDERS GUID as defined in {@link KnownFolders}
* @return the path of the known folder. The returned path does not include a trailing backslash. For example,
* "C:\Users" is returned rather than "C:\Users\".
* @throws Win32Exception if the guid references a KNOWNFOLDERID which does not have a path (such as a folder marked
* as KF_CATEGORY_VIRTUAL) or that the KNOWNFOLDERID is not present on the system. Not all KNOWNFOLDERID values are
* present on all systems.
*/
public static String getKnownFolderPath(GUID guid) throws Win32Exception {
int flags = ShlObj.KNOWN_FOLDER_FLAG.NONE.getFlag();
PointerByReference outPath = new PointerByReference();
HANDLE token = null;
HRESULT hr = Shell32.INSTANCE.SHGetKnownFolderPath(guid, flags, token, outPath);
if (!W32Errors.SUCCEEDED(hr.intValue())) {
throw new Win32Exception(hr);
}
String result = outPath.getValue().getWideString(0);
Ole32.INSTANCE.CoTaskMemFree(outPath.getValue());
return result;
}
use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.
the class VersionUtil method getFileVersionInfo.
/**
* Gets the file's version number info
*
* @param filePath
* The path to the file
* @return The VS_FIXEDFILEINFO structure read from the file.<br>
* Use the getFileVersionMajor(), getFileVersionMinor(),
* getFileVersionRevision(), and getFileVersionBuild()
* @throws UnsupportedOperationException
* if VerQueryValue fails to get version info from the file.
*/
public static VS_FIXEDFILEINFO getFileVersionInfo(String filePath) {
IntByReference dwDummy = new IntByReference();
int versionLength = Version.INSTANCE.GetFileVersionInfoSize(filePath, dwDummy);
// throw a Win32Exception with GetLastError()
if (versionLength == 0) {
throw new Win32Exception(Native.getLastError());
}
// buffer to hold version info
Pointer lpData = new Memory(versionLength);
// pointer to pointer to location in aforementioned buffer
PointerByReference lplpBuffer = new PointerByReference();
if (!Version.INSTANCE.GetFileVersionInfo(filePath, 0, versionLength, lpData)) {
throw new Win32Exception(Native.getLastError());
}
// here to make VerQueryValue happy.
IntByReference puLen = new IntByReference();
// this does not set GetLastError, so no need to throw a Win32Exception
if (!Version.INSTANCE.VerQueryValue(lpData, "\\", lplpBuffer, puLen)) {
throw new UnsupportedOperationException("Unable to extract version info from the file: \"" + filePath + "\"");
}
VS_FIXEDFILEINFO fileInfo = new VS_FIXEDFILEINFO(lplpBuffer.getValue());
fileInfo.read();
return fileInfo;
}
use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.
the class W32Service method setFailureActions.
/**
* Set the failure actions of the specified service. Corresponds to
* <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms681988.aspx">ChangeServiceConfig2</a>
* with parameter dwInfoLevel set to SERVICE_CONFIG_FAILURE_ACTIONS.
*/
public void setFailureActions(List<SC_ACTION> actions, int resetPeriod, String rebootMsg, String command) {
SERVICE_FAILURE_ACTIONS.ByReference actionStruct = new SERVICE_FAILURE_ACTIONS.ByReference();
actionStruct.dwResetPeriod = resetPeriod;
actionStruct.lpRebootMsg = rebootMsg;
actionStruct.lpCommand = command;
actionStruct.cActions = actions.size();
actionStruct.lpsaActions = new SC_ACTION.ByReference();
SC_ACTION[] actionArray = (SC_ACTION[]) actionStruct.lpsaActions.toArray(actions.size());
boolean hasShutdownPrivilege = false;
int i = 0;
for (SC_ACTION action : actions) {
if (!hasShutdownPrivilege && action.type == Winsvc.SC_ACTION_REBOOT) {
addShutdownPrivilegeToProcess();
hasShutdownPrivilege = true;
}
actionArray[i].type = action.type;
actionArray[i].delay = action.delay;
i++;
}
if (!Advapi32.INSTANCE.ChangeServiceConfig2(_handle, Winsvc.SERVICE_CONFIG_FAILURE_ACTIONS, actionStruct)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}
Aggregations