當前位置: 首頁>>代碼示例>>Java>>正文


Java Native.getLastError方法代碼示例

本文整理匯總了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);
}
 
開發者ID:yajsw,項目名稱:yajsw,代碼行數:28,代碼來源:Win32Service.java

示例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);
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:22,代碼來源:GemFireCacheImpl.java

示例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);
	}
}
 
開發者ID:yajsw,項目名稱:yajsw,代碼行數:27,代碼來源:Win32Service.java

示例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);
}
 
開發者ID:gemxd,項目名稱:gemfirexd-oss,代碼行數:23,代碼來源:NativeCallsJNAImpl.java

示例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();
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:13,代碼來源:Win32Protect.java

示例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;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:14,代碼來源:Win32Protect.java

示例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");
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:40,代碼來源:SystemCallFilter.java

示例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;
}
 
開發者ID:yajsw,項目名稱:yajsw,代碼行數:40,代碼來源:WindowsXPProcess.java

示例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;
}
 
開發者ID:Jonatino,項目名稱:Java-Memory-Manipulation,代碼行數:8,代碼來源:Win32Process.java

示例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;
}
 
開發者ID:Jonatino,項目名稱:Java-Memory-Manipulation,代碼行數:8,代碼來源:Win32Process.java

示例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;
}
 
開發者ID:ZabuzaW,項目名稱:Mem-Eater-Bug,代碼行數:25,代碼來源:Kernel32Util.java

示例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).");
        }
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:55,代碼來源:JNANatives.java

示例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).");
        }
    }
}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:54,代碼來源:JNANatives.java

示例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;
}
 
開發者ID:ZabuzaW,項目名稱:Mem-Eater-Bug,代碼行數:57,代碼來源:PsapiUtil.java

示例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());
	}
}
 
開發者ID:ZabuzaW,項目名稱:Mem-Eater-Bug,代碼行數:18,代碼來源:Kernel32Util.java


注:本文中的com.sun.jna.Native.getLastError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。