本文整理汇总了Java中com.sun.jna.Native.getLastError方法的典型用法代码示例。如果您正苦于以下问题:Java Native.getLastError方法的具体用法?Java Native.getLastError怎么用?Java Native.getLastError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.jna.Native
的用法示例。
在下文中一共展示了Native.getLastError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: openServiceControlManager
import com.sun.jna.Native; //导入方法依赖的package包/类
/**
* Get a handle to the ServiceControlManager.
*
* @param machine
* name of the machine or null for localhost
* @param access
* access flags
* @return handle to ServiceControlManager or null when failed
*/
static private SC_HANDLE openServiceControlManager(String machine, int access)
{
SC_HANDLE handle = null;
Advapi32 advapi32;
advapi32 = Advapi32.INSTANCE;
handle = advapi32.OpenSCManager(machine, null, access);
if (handle == null)
{
int err = Native.getLastError();
lastWinError = err;
System.out.println("Error in OpenSCManager: " + Integer.toHexString(err));
if (err == 5)
System.out.println("Access denied: please check the user credentials");
}
return (handle);
}
示例2: lockMemory
import com.sun.jna.Native; //导入方法依赖的package包/类
public static void lockMemory() {
int result = 0;
try {
Native.register(Platform.C_LIBRARY_NAME);
result = mlockall(1);
if (result == 0) {
return;
}
} catch (Throwable t) {
throw new IllegalStateException("Error trying to lock memory", t);
}
int errno = Native.getLastError();
String msg = "mlockall failed: " + errno;
if (errno == 1 || errno == 12) { // EPERM || ENOMEM
msg = "Unable to lock memory due to insufficient free space or privileges. "
+ "Please check the RLIMIT_MEMLOCK soft resource limit (ulimit -l) and "
+ "increase the available memory if needed";
}
throw new IllegalStateException(msg);
}
示例3: init
import com.sun.jna.Native; //导入方法依赖的package包/类
/**
* Initialize the service, connect to the ServiceControlManager.
*/
public void init()
{
Advapi32 advapi32;
// Advapi32.SERVICE_TABLE_ENTRY[] entries = new
// Advapi32.SERVICE_TABLE_ENTRY[2];
Advapi32.SERVICE_TABLE_ENTRY entry;
serviceMain = new ServiceMain();
advapi32 = Advapi32.INSTANCE;
entry = new Advapi32.SERVICE_TABLE_ENTRY();
entry.size();
entry.lpServiceName = serviceName;
entry.lpServiceProc = serviceMain;
entry.write();
if (!advapi32.StartServiceCtrlDispatcher(entry.toArray(2)))
{
log("error in StartServiceCtrlDispatcher", 0);
int err = Native.getLastError();
lastWinError = err;
log(err + ":" + Kernel32Util.formatMessageFromLastErrorCode(err), 0);
}
}
示例4: lockCurrentMemory
import com.sun.jna.Native; //导入方法依赖的package包/类
@Override
public void lockCurrentMemory() {
if (mlockall(MCL_CURRENT) == 0) {
return;
}
final int errno = Native.getLastError();
final String msg, reason;
if (errno == EPERM) {
reason = "insufficient privileges";
}
else if (errno == ENOMEM) {
reason = "insufficient free space";
}
else {
reason = "errno=" + errno;
}
msg = "Unable to lock memory due to " + reason
+ ". Please check the RLIMIT_MEMLOCK soft resource limit "
+ "(ulimit -l) and increase the available memory if needed.";
throw new IllegalStateException(msg);
}
示例5: encrypt
import com.sun.jna.Native; //导入方法依赖的package包/类
public @Override byte[] encrypt(char[] cleartext) throws Exception {
byte[] cleartextB = Utils.chars2Bytes(cleartext);
CryptIntegerBlob input = new CryptIntegerBlob();
input.store(cleartextB);
Arrays.fill(cleartextB, (byte) 0);
CryptIntegerBlob output = new CryptIntegerBlob();
if (!CryptLib.INSTANCE.CryptProtectData(input, null, null, null, null, 0, output)) {
throw new Exception("CryptProtectData failed: " + Native.getLastError());
}
input.zero();
return output.load();
}
示例6: decrypt
import com.sun.jna.Native; //导入方法依赖的package包/类
public @Override char[] decrypt(byte[] ciphertext) throws Exception {
CryptIntegerBlob input = new CryptIntegerBlob();
input.store(ciphertext);
CryptIntegerBlob output = new CryptIntegerBlob();
if (!CryptLib.INSTANCE.CryptUnprotectData(input, null, null, null, null, 0, output)) {
throw new Exception("CryptUnprotectData failed: " + Native.getLastError());
}
byte[] result = output.load();
// XXX gives CCE because not a Memory: output.zero();
char[] cleartext = Utils.bytes2Chars(result);
Arrays.fill(result, (byte) 0);
return cleartext;
}
示例7: windowsImpl
import com.sun.jna.Native; //导入方法依赖的package包/类
static void windowsImpl() {
if (!Constants.WINDOWS) {
throw new IllegalStateException("bug: should not be trying to initialize ActiveProcessLimit for an unsupported OS");
}
JNAKernel32Library lib = JNAKernel32Library.getInstance();
// create a new Job
Pointer job = lib.CreateJobObjectW(null, null);
if (job == null) {
throw new UnsupportedOperationException("CreateJobObject: " + Native.getLastError());
}
try {
// retrieve the current basic limits of the job
int clazz = JNAKernel32Library.JOBOBJECT_BASIC_LIMIT_INFORMATION_CLASS;
JNAKernel32Library.JOBOBJECT_BASIC_LIMIT_INFORMATION limits = new JNAKernel32Library.JOBOBJECT_BASIC_LIMIT_INFORMATION();
limits.write();
if (!lib.QueryInformationJobObject(job, clazz, limits.getPointer(), limits.size(), null)) {
throw new UnsupportedOperationException("QueryInformationJobObject: " + Native.getLastError());
}
limits.read();
// modify the number of active processes to be 1 (exactly the one process we will add to the job).
limits.ActiveProcessLimit = 1;
limits.LimitFlags = JNAKernel32Library.JOB_OBJECT_LIMIT_ACTIVE_PROCESS;
limits.write();
if (!lib.SetInformationJobObject(job, clazz, limits.getPointer(), limits.size())) {
throw new UnsupportedOperationException("SetInformationJobObject: " + Native.getLastError());
}
// assign ourselves to the job
if (!lib.AssignProcessToJobObject(job, lib.GetCurrentProcess())) {
throw new UnsupportedOperationException("AssignProcessToJobObject: " + Native.getLastError());
}
} finally {
lib.CloseHandle(job);
}
logger.debug("Windows ActiveProcessLimit initialization successful");
}
示例8: startElevated
import com.sun.jna.Native; //导入方法依赖的package包/类
public boolean startElevated()
{
String file = getCmdFile();
if (file == null)
{
log("startElevated: Error: error in command");
return false;
}
String parameters = getCmdParameters();
if (_debug)
log("elevated exec: " + file + " " + parameters);
SHELLEXECUTEINFO lpExecInfo = new SHELLEXECUTEINFO();
lpExecInfo.fMask = Shell32.SEE_MASK_NOCLOSEPROCESS;
lpExecInfo.hwnd = null;
lpExecInfo.lpFile = file;
lpExecInfo.lpVerb = Shell32.VERB_RUNAS;
lpExecInfo.nShow = Shell32.SW_SHOWMAXIMIZED;
lpExecInfo.lpParameters = parameters;
lpExecInfo.cbSize = lpExecInfo.size();
boolean result = Shell32.INSTANCE.ShellExecuteEx(lpExecInfo);
if (!result)
{
int err = Native.getLastError();
System.out.println("Error: " + err + " "
+ Kernel32Util.formatMessageFromLastErrorCode(err));
}
else
{
_pid = MyKernel32.INSTANCE.GetProcessId(lpExecInfo.hProcess);
_processInformation = new PROCESS_INFORMATION();
_processInformation.dwProcessId = _pid;
_processInformation.hProcess = lpExecInfo.hProcess;
}
return result;
}
示例9: read
import com.sun.jna.Native; //导入方法依赖的package包/类
@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;
}
示例10: write
import com.sun.jna.Native; //导入方法依赖的package包/类
@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;
}
示例11: is64Bit
import com.sun.jna.Native; //导入方法依赖的package包/类
/**
* Whether the given process is a 64-bit application or not. A 32-bit
* application that runs in the WoW64 environment is not considered as
* 64-bit application, since they are restricted to the 32-bit memory space.
*
* @param hProcess
* Handle to the process in question
* @return <tt>True</tt> if the given process is a 64-bit application,
* <tt>false</tt> otherwise.
* @throws Win32Exception
* If the operation was not successful
*/
public static boolean is64Bit(final HANDLE hProcess) throws Win32Exception {
if (System.getenv(SystemProperties.PRC_ARCH) == Masks.PRC_ARCH_32BIT) {
return false;
}
final IntByReference isWow64 = new IntByReference();
final boolean success = Kernel32.INSTANCE.IsWow64Process(hProcess, isWow64);
if (!success) {
throw new Win32Exception(Native.getLastError());
}
return isWow64.getValue() == 0;
}
示例12: tryMlockall
import com.sun.jna.Native; //导入方法依赖的package包/类
static void tryMlockall() {
int errno = Integer.MIN_VALUE;
String errMsg = null;
boolean rlimitSuccess = false;
long softLimit = 0;
long hardLimit = 0;
try {
int result = JNACLibrary.mlockall(JNACLibrary.MCL_CURRENT);
if (result == 0) {
LOCAL_MLOCKALL = true;
return;
}
errno = Native.getLastError();
errMsg = JNACLibrary.strerror(errno);
if (Constants.LINUX || Constants.MAC_OS_X) {
// we only know RLIMIT_MEMLOCK for these two at the moment.
JNACLibrary.Rlimit rlimit = new JNACLibrary.Rlimit();
if (JNACLibrary.getrlimit(JNACLibrary.RLIMIT_MEMLOCK, rlimit) == 0) {
rlimitSuccess = true;
softLimit = rlimit.rlim_cur.longValue();
hardLimit = rlimit.rlim_max.longValue();
} else {
logger.warn("Unable to retrieve resource limits: {}", JNACLibrary.strerror(Native.getLastError()));
}
}
} catch (UnsatisfiedLinkError e) {
// this will have already been logged by CLibrary, no need to repeat it
return;
}
// mlockall failed for some reason
logger.warn("Unable to lock JVM Memory: error={}, reason={}", errno , errMsg);
logger.warn("This can result in part of the JVM being swapped out.");
if (errno == JNACLibrary.ENOMEM) {
if (rlimitSuccess) {
logger.warn("Increase RLIMIT_MEMLOCK, soft limit: {}, hard limit: {}", rlimitToString(softLimit), rlimitToString(hardLimit));
if (Constants.LINUX) {
// give specific instructions for the linux case to make it easy
String user = System.getProperty("user.name");
logger.warn("These can be adjusted by modifying /etc/security/limits.conf, for example: \n" +
"\t# allow user '{}' mlockall\n" +
"\t{} soft memlock unlimited\n" +
"\t{} hard memlock unlimited",
user, user, user
);
logger.warn("If you are logged in interactively, you will have to re-login for the new limits to take effect.");
}
} else {
logger.warn("Increase RLIMIT_MEMLOCK (ulimit).");
}
}
}
示例13: tryMlockall
import com.sun.jna.Native; //导入方法依赖的package包/类
static void tryMlockall() {
int errno = Integer.MIN_VALUE;
String errMsg = null;
boolean rlimitSuccess = false;
long softLimit = 0;
long hardLimit = 0;
try {
int result = JNACLibrary.mlockall(JNACLibrary.MCL_CURRENT);
if (result == 0) {
LOCAL_MLOCKALL = true;
return;
}
errno = Native.getLastError();
errMsg = JNACLibrary.strerror(errno);
if (Constants.LINUX || Constants.MAC_OS_X) {
// we only know RLIMIT_MEMLOCK for these two at the moment.
JNACLibrary.Rlimit rlimit = new JNACLibrary.Rlimit();
if (JNACLibrary.getrlimit(JNACLibrary.RLIMIT_MEMLOCK, rlimit) == 0) {
rlimitSuccess = true;
softLimit = rlimit.rlim_cur.longValue();
hardLimit = rlimit.rlim_max.longValue();
} else {
logger.warn("Unable to retrieve resource limits: " + JNACLibrary.strerror(Native.getLastError()));
}
}
} catch (UnsatisfiedLinkError e) {
// this will have already been logged by CLibrary, no need to repeat it
return;
}
// mlockall failed for some reason
logger.warn("Unable to lock JVM Memory: error=" + errno + ",reason=" + errMsg);
logger.warn("This can result in part of the JVM being swapped out.");
if (errno == JNACLibrary.ENOMEM) {
if (rlimitSuccess) {
logger.warn("Increase RLIMIT_MEMLOCK, soft limit: " + rlimitToString(softLimit) + ", hard limit: " + rlimitToString(hardLimit));
if (Constants.LINUX) {
// give specific instructions for the linux case to make it easy
String user = System.getProperty("user.name");
logger.warn("These can be adjusted by modifying /etc/security/limits.conf, for example: \n" +
"\t# allow user '" + user + "' mlockall\n" +
"\t" + user + " soft memlock unlimited\n" +
"\t" + user + " hard memlock unlimited"
);
logger.warn("If you are logged in interactively, you will have to re-login for the new limits to take effect.");
}
} else {
logger.warn("Increase RLIMIT_MEMLOCK (ulimit).");
}
}
}
示例14: enumProcessModulesEx
import com.sun.jna.Native; //导入方法依赖的package包/类
/**
* Retrieves a list of handles for each module in the specified process,
* that meets the filter criteria specified by the list flag.
*
* @see <a href=
* "https://msdn.microsoft.com/en-us/library/ms682631(v=vs.85).aspx">
* MSDN webpage#EnumProcessModules function</a>
* @param hProcess
* A handle to the process.
* @param listFlag
* Specifies the modules to list. Possible values are the
* following.
* <ul>
* <li>
* {@link de.zabuza.memeaterbug.winapi.jna.Psapi#LIST_MODULES_32BIT
* LIST_MODULES_32BIT}</li>
* <li>
* {@link de.zabuza.memeaterbug.winapi.jna.Psapi#LIST_MODULES_64BIT
* LIST_MODULES_64BIT}</li>
* <li>
* {@link de.zabuza.memeaterbug.winapi.jna.Psapi#LIST_MODULES_ALL
* LIST_MODULES_ALL} or <tt>null</tt></li>
* <li>
* {@link de.zabuza.memeaterbug.winapi.jna.Psapi#LIST_MODULES_DEFAULT
* LIST_MODULES_DEFAULT}</li>
* </ul>
* @return A list of handles for each module in the specified process, that
* meets the filter criteria specified by the list flag.
* @throws Win32Exception
* If the operation was not successful
*/
public static List<HMODULE> enumProcessModulesEx(final HANDLE hProcess, final Integer listFlag)
throws Win32Exception {
final int moduleSize = MemSize.getSizeOfModule(hProcess);
final List<HMODULE> list = new LinkedList<>();
final HMODULE[] lphModule = new HMODULE[MODULE_BUFFER_AMOUNT * moduleSize];
final IntByReference lpcbNeededs = new IntByReference();
if (listFlag == null) {
if (!Psapi.INSTANCE.EnumProcessModules(hProcess, lphModule, lphModule.length, lpcbNeededs)) {
throw new Win32Exception(Native.getLastError());
}
} else {
if (!Psapi.INSTANCE.EnumProcessModulesEx(hProcess, lphModule, lphModule.length, lpcbNeededs,
listFlag.intValue())) {
throw new Win32Exception(Native.getLastError());
}
}
for (int i = 0; i < lpcbNeededs.getValue() / moduleSize; i++) {
list.add(lphModule[i]);
}
return list;
}
示例15: closeHandle
import com.sun.jna.Native; //导入方法依赖的package包/类
/**
* Closes an open object handle.
*
* @see <a href=
* "https://msdn.microsoft.com/en-us/library/ms724211(v=vs.85).aspx">
* MSDN webpage#CloseHandle function</a>
* @param processHandle
* A valid handle to an open object.
* @throws Win32Exception
* If the operation was not successful
*/
public static void closeHandle(final HANDLE processHandle) throws Win32Exception {
final boolean success = Kernel32.INSTANCE.CloseHandle(processHandle);
if (!success) {
throw new Win32Exception(Native.getLastError());
}
}