下面列出了怎么用com.sun.jna.platform.win32.Win32Exception的API类实例代码及写法,或者点击链接到github查看源代码。
private void checkKeyExists(
String rootKey,
String keyPath,
String keyName ) {
try {
WinReg.HKEY rootHKey = getHKey(rootKey);
if (!Advapi32Util.registryValueExists(rootHKey, keyPath, keyName)) {
throw new RegistryOperationsException("Registry key does not exist. "
+ getDescription(rootKey, keyPath, keyName));
}
} catch (Win32Exception e) {
throw new RegistryOperationsException("Registry key path does not exist. "
+ getDescription(rootKey, keyPath, keyName), e);
}
}
/**
* Returns the osu! installation directory.
* @return the directory, or null if not found
*/
private static File getOsuInstallationDirectory() {
if (!System.getProperty("os.name").startsWith("Win"))
return null; // only works on Windows
// registry location
final WinReg.HKEY rootKey = WinReg.HKEY_CLASSES_ROOT;
final String regKey = "osu\\DefaultIcon";
final String regValue = null; // default value
final String regPathPattern = "\"(.+)\\\\[^\\/]+\\.exe\"";
String value;
try {
value = Advapi32Util.registryGetStringValue(rootKey, regKey, regValue);
} catch (Win32Exception e) {
return null; // key/value not found
}
Pattern pattern = Pattern.compile(regPathPattern);
Matcher m = pattern.matcher(value);
if (!m.find())
return null;
File dir = new File(m.group(1));
return (dir.isDirectory()) ? dir : null;
}
public static boolean isAddedToContextMenu() {
if (!Platform.isWindows()) {
return false;
}
final WinReg.HKEY REG_CLASSES_HKEY = WinReg.HKEY_LOCAL_MACHINE;
final String REG_CLASSES_PATH = "Software\\Classes\\";
try {
if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY, REG_CLASSES_PATH + ".swf")) {
return false;
}
String clsName = Advapi32Util.registryGetStringValue(REG_CLASSES_HKEY, REG_CLASSES_PATH + ".swf", "");
if (clsName == null) {
return false;
}
return Advapi32Util.registryKeyExists(REG_CLASSES_HKEY, REG_CLASSES_PATH + clsName + "\\shell\\ffdec");
} catch (Win32Exception ex) {
return false;
}
}
String getToken(
final Sspi.CtxtHandle continueCtx,
final Sspi.SecBufferDesc continueToken,
final String targetName) {
final IntByReference attr = new IntByReference();
final ManagedSecBufferDesc token = new ManagedSecBufferDesc(
Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE);
sspiContext = new Sspi.CtxtHandle();
final int rc = Secur32.INSTANCE.InitializeSecurityContext(clientCred,
continueCtx, targetName, Sspi.ISC_REQ_DELEGATE | Sspi.ISC_REQ_MUTUAL_AUTH, 0,
Sspi.SECURITY_NATIVE_DREP, continueToken, 0, sspiContext, token,
attr, null);
switch(rc) {
case WinError.SEC_I_CONTINUE_NEEDED:
continueNeeded = true;
break;
case WinError.SEC_E_OK:
dispose(); // Don't keep the context
continueNeeded = false;
break;
default:
dispose();
throw new Win32Exception(rc);
}
return Base64.encodeBase64String(token.getBuffer(0).getBytes());
}
private File loadOsuInstallationDirectory() {
if (!System.getProperty("os.name").startsWith("Win")) {
return null;
}
final WinReg.HKEY rootKey = WinReg.HKEY_CLASSES_ROOT;
final String regKey = "osu\\DefaultIcon";
final String regValue = null; // default value
final String regPathPattern = "\"(.+)\\\\[^\\/]+\\.exe\"";
String value;
try {
value = Advapi32Util.registryGetStringValue(rootKey, regKey, regValue);
} catch (Win32Exception ignored) {
return null;
}
Pattern pattern = Pattern.compile(regPathPattern);
Matcher m = pattern.matcher(value);
if (!m.find()) {
return null;
}
File dir = new File(m.group(1));
if (dir.isDirectory()) {
return dir;
}
return null;
}
@Override
public MemoryBuffer read(Pointer address, int size, MemoryBuffer buffer) {
if (Kernel32.ReadProcessMemory(pointer(), address, buffer, size, 0) == 0) {
throw new Win32Exception(Native.getLastError());
}
return buffer;
}
@Override
public Process write(Pointer address, MemoryBuffer buffer) {
if (Kernel32.WriteProcessMemory(pointer(), address, buffer, buffer.size(), 0) == 0) {
throw new Win32Exception(Native.getLastError());
}
return this;
}
@Override
public MemoryBuffer read(Pointer address, int size, MemoryBuffer buffer) {
if (Kernel32.ReadProcessMemory(pointer(), address, buffer, size, 0) == 0) {
throw new Win32Exception(Native.getLastError());
}
return buffer;
}
@Override
public Process write(Pointer address, MemoryBuffer buffer) {
if (Kernel32.WriteProcessMemory(pointer(), address, buffer, buffer.size(), 0) == 0) {
throw new Win32Exception(Native.getLastError());
}
return this;
}
@Nullable
public static String readString(WinReg.HKEY hkey, String key, String valueName) {
if (! isAvailable()) {
return null;
}
try {
return Advapi32Util.registryGetStringValue(hkey, key, valueName);
} catch (Win32Exception e) {
return null;
}
}
/**
* Gets the computer name.
*
* <p>This is the also known as the NetBIOS name, although NetBIOS is
* hardly used anymore. It is the same value as can be seen from the
* {@code COMPUTERNAME} environment variable.
*
* <p>
* Windows API equivalent: {@code GetComputerName()} function from
* {@code Kernel32} library.
*
* @return computer name
* @throws NativeException if there was an error executing the
* system call.
*/
public static String getComputerName() throws NativeException {
try {
return Kernel32Util.getComputerName();
} catch (Win32Exception ex) {
LOGGER.log(Level.FINE, "Kernel32.GetComputerName error : {0}", ex.getHR().intValue());
String env = System.getenv("COMPUTERNAME");
if (env != null) {
return env;
}
throw new NativeException(ex.getHR().intValue(), "error calling 'GetComputerName()' function");
}
}