Search in sources :

Example 51 with Win32Exception

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);
            }
        }
    }
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference) IntByReference(com.sun.jna.ptr.IntByReference)

Example 52 with Win32Exception

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]);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) SecPkgInfo(com.sun.jna.platform.win32.Sspi.SecPkgInfo) PSecPkgInfo(com.sun.jna.platform.win32.Sspi.PSecPkgInfo) ArrayList(java.util.ArrayList) PSecPkgInfo(com.sun.jna.platform.win32.Sspi.PSecPkgInfo)

Example 53 with Win32Exception

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;
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 54 with Win32Exception

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;
}
Also used : VS_FIXEDFILEINFO(com.sun.jna.platform.win32.VerRsrc.VS_FIXEDFILEINFO) IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer) Win32Exception(com.sun.jna.platform.win32.Win32Exception)

Example 55 with Win32Exception

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());
    }
}
Also used : SC_ACTION(com.sun.jna.platform.win32.Winsvc.SC_ACTION) SERVICE_FAILURE_ACTIONS(com.sun.jna.platform.win32.Winsvc.SERVICE_FAILURE_ACTIONS) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) IntByReference(com.sun.jna.ptr.IntByReference)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)35 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)18 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 Memory (com.sun.jna.Memory)15 PointerByReference (com.sun.jna.ptr.PointerByReference)11 ArrayList (java.util.ArrayList)11 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Pointer (com.sun.jna.Pointer)6 File (java.io.File)6 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)5 Test (org.junit.Test)5 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)4 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)4 PSID (com.sun.jna.platform.win32.WinNT.PSID)4 Win32Exception (com.sun.jna.platform.win32.Win32Exception)3 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)3 LOCALGROUP_INFO_1 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_INFO_1)2 LOCALGROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0)2 DATA_BLOB (com.sun.jna.platform.win32.WinCrypt.DATA_BLOB)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2