⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kernel32.java

📁 jnative java 调用动态库需要的包和dll
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		}
		gms.dispose();
		return mem.getValueFromPointer();
	}
	
	/*
	BOOL SetFilePointerEx(
	  HANDLE hFile,
	  LARGE_INTEGER liDistanceToMove,
	  PLARGE_INTEGER lpNewFilePointer,
	  DWORD dwMoveMethod
	);
	 */
	public static final boolean SetFilePointerEx(HANDLE hFile, INT64 liDistanceToMove,
			INT64 lpNewFilePointer, MoveMode dwMoveMethod)
			throws NativeException, IllegalAccessException {
		
		JNative SetFilePointer = new JNative(DLL_NAME, "SetFilePointerEx");
		try {
			SetFilePointer.setRetVal(Type.INT);
			
			int i = 0;
			SetFilePointer.setParameter(i++, hFile.getValue());
			SetFilePointer.setParameter(i++, Type.LONG, ""+liDistanceToMove.getValue());
			SetFilePointer.setParameter(i++, lpNewFilePointer.getPointer());
			SetFilePointer.setParameter(i++, dwMoveMethod.getValue());
			
			SetFilePointer.invoke();
			
			return (SetFilePointer.getRetValAsInt() != 0);
		} finally {
			if(SetFilePointer != null)
				SetFilePointer.dispose();
		}
	}
	/*
	BOOL ReadFile(
	  HANDLE hFile,
	  LPVOID lpBuffer,
	  DWORD nNumberOfBytesToRead,
	  LPDWORD lpNumberOfBytesRead,
	  LPOVERLAPPED lpOverlapped
	);
	 */
	public static final boolean ReadFile(HANDLE hFile, Pointer lpBuffer,
			int nNumberOfBytesToRead, DWORD lpNumberOfBytesRead,
			Pointer lpOverlapped)
			throws NativeException, IllegalAccessException {
		
		if(nReadFile == null) {
			nReadFile = new JNative(DLL_NAME, "ReadFile");
			nReadFile.setRetVal(Type.INT);
		}
		
		int i = 0;
		nReadFile.setParameter(i++, hFile.getValue());
		nReadFile.setParameter(i++, lpBuffer);
		nReadFile.setParameter(i++, nNumberOfBytesToRead);
		nReadFile.setParameter(i++, lpNumberOfBytesRead.getPointer());
		nReadFile.setParameter(i++, lpOverlapped);
		
		nReadFile.invoke();
		
		return (nReadFile.getRetValAsInt() != 0);
	}
	/*
	BOOL WriteFile(
	  HANDLE hFile,
	  LPCVOID lpBuffer,
	  DWORD nNumberOfBytesToWrite,
	  LPDWORD lpNumberOfBytesWritten,
	  LPOVERLAPPED lpOverlapped
	);
	 **/
	public static final boolean WriteFile(HANDLE hFile, Pointer lpBuffer,
			int nNumberOfBytesToWrite, DWORD lpNumberOfBytesWritten,
			Pointer lpOverlapped)
			throws NativeException, IllegalAccessException {
		
		if(nWriteFile == null) {
			nWriteFile = new JNative(DLL_NAME, "WriteFile");
			nWriteFile.setRetVal(Type.INT);
		}
		
		int i = 0;
		nWriteFile.setParameter(i++, hFile.getValue());
		nWriteFile.setParameter(i++, lpBuffer);
		nWriteFile.setParameter(i++, nNumberOfBytesToWrite);
		nWriteFile.setParameter(i++, lpNumberOfBytesWritten.getPointer());
		nWriteFile.setParameter(i++, lpOverlapped);
		
		nWriteFile.invoke();
		
		return (nWriteFile.getRetValAsInt() != 0);
	}
	/**
	 * HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD
	 * dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD
	 * dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile );
	 */
	public static final HANDLE CreateFile(String fileName, int desiredAccess,
			int shareMode, SecurityAttributes securityAttributes,
			int creationDisposition, int flagsAndAttributes, int templateFile)
			throws NativeException, IllegalAccessException {
		JNative createFile = new JNative(DLL_NAME, "CreateFileA");
		createFile.setRetVal(Type.INT);
		int i = 0;
		createFile.setParameter(i++, Type.STRING, fileName);
		createFile.setParameter(i++, Type.INT, "" + desiredAccess);
		createFile.setParameter(i++, Type.INT, "" + shareMode);
		createFile.setParameter(i++, securityAttributes == null ? NullPointer.NULL : securityAttributes.getPointer());
		createFile.setParameter(i++, Type.INT, "" + creationDisposition);
		createFile.setParameter(i++, Type.INT, "" + flagsAndAttributes);
		createFile.setParameter(i++, Type.INT, "" + templateFile);
		createFile.invoke();
		HANDLE handle = new HANDLE(Integer.parseInt(createFile.getRetVal()));
		createFile.dispose();
		return handle;
	}
	
	public static final HANDLE CreateFile(String fileName,
			AccessMask desiredAccess, ShareMode shareMode,
			SecurityAttributes securityAttributes,
			CreationDisposition creationDisposition,
			FileAttribute flagsAndAttributes, int templateFile)
			throws NativeException, IllegalAccessException {
		return CreateFile(fileName, desiredAccess.getValue(),
				shareMode.getValue(), securityAttributes, creationDisposition
				.getValue(), flagsAndAttributes.getValue(),
				templateFile);
	}
	
	public static final int getLastError() throws NativeException,
			IllegalAccessException {
		JNative lastError = null;
		try {
			lastError = new JNative(DLL_NAME, "GetLastError");
			lastError.setRetVal(Type.INT);
			lastError.invoke();
			return Integer.parseInt(lastError.getRetVal());
		} finally {
			if (lastError != null)
				lastError.dispose();
		}
	}
	
	/**
	 * typedef struct _SYSTEM_INFO { union { DWORD dwOemId; struct { WORD
	 * wProcessorArchitecture; WORD wReserved; }; }; DWORD dwPageSize; LPVOID
	 * lpMinimumApplicationAddress; LPVOID lpMaximumApplicationAddress;
	 * DWORD_PTR dwActiveProcessorMask; DWORD dwNumberOfProcessors; DWORD
	 * dwProcessorType; DWORD dwAllocationGranularity; WORD wProcessorLevel;
	 * WORD wProcessorRevision; } SYSTEM_INFO;
	 */
	
	public static final SystemInfo getNativeSystemInfo()
	throws NativeException, IllegalAccessException {
		JNative info = new JNative(DLL_NAME, "GetNativeSystemInfo");
		Pointer lpSystemInfo = new Pointer(new HeapMemoryBlock(4 + 4 * 7 + 2
				* 2));
		info.setParameter(0, lpSystemInfo);
		info.invoke();
		SystemInfo infoS = new SystemInfo(lpSystemInfo);
		lpSystemInfo.dispose();
		info.dispose();
		return infoS;
	}
	
	public static final String getComputerName() throws NativeException,
			IllegalAccessException {
		JNative v = new JNative(DLL_NAME, "GetComputerNameA");
		int i = 0;
		v.setRetVal(Type.INT);
		Pointer pName = new Pointer(new HeapMemoryBlock(1024));
		LONG pSize = new LONG(1024);
		v.setParameter(i++, pName);
		v.setParameter(i++, pSize.createPointer());
		v.invoke();
		int ret = Integer.parseInt(v.getRetVal());
		if (ret == 0) {
			// return "null";
			throw new NativeException(
					"GetComputerNameA failed call GetLastError()");
		} else {
			// System.err.println("pSize = " + pSize.getValueFromPointer());
			// System.err.println("pName = " + pName.getAsString());
			String name = pName.getAsString().substring(0,
					pSize.getValueFromPointer());
			pName.dispose();
			v.dispose();
			return name;
		}
	}
	
	public static final FreeDiskSpace getDiskFreeSpaceEx(String drive)
	throws NativeException, IllegalAccessException {
		if (drive == null)
			throw new NullPointerException("The drive name cannot be null !");
		
		INT64 lFreeBytesAvailable = new INT64(0);
		INT64 lTotalNumberOfBytes = new INT64(0);
		INT64 lTotalNumberOfFreeBytes = new INT64(0);
		
		int i = 0;
		JNative fs = new JNative(DLL_NAME, "GetDiskFreeSpaceExA");
		fs.setRetVal(Type.INT);
		fs.setParameter(i++, Type.STRING, drive);
		fs.setParameter(i++, lFreeBytesAvailable.createPointer());
		fs.setParameter(i++, lTotalNumberOfBytes.createPointer());
		fs.setParameter(i++, lTotalNumberOfFreeBytes.createPointer());
		fs.invoke();
		return new FreeDiskSpace(drive, lFreeBytesAvailable
				.getValueFromPointer(), lTotalNumberOfBytes
				.getValueFromPointer(), lTotalNumberOfFreeBytes
				.getValueFromPointer());
	}
	
	/**
	 * <pre>
	 *   Opens an existing process object.
	 *
	 *  	 HANDLE WINAPI OpenProcess(
	 *  	 DWORD dwDesiredAccess,
	 *  	 BOOL bInheritHandle,
	 *  	 DWORD dwProcessId
	 *  	 );
	 *
	 *  	 Parameters
	 *
	 *  	 dwDesiredAccess
	 *  	 [in] The access to the process object. This access right is checked against the security descriptor for the process. This parameter can be one or more of the process access rights.
	 *
	 *  	 If the caller has enabled the SeDebugPrivilege privilege, the requested access is granted regardless of the contents of the security descriptor.
	 *  	 bInheritHandle
	 *  	 [in] If this value is TRUE, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.
	 *  	 dwProcessId
	 *  	 [in] The identifier of the process to be opened.
	 *
	 *  	 Return Values
	 *
	 *  	 If the function succeeds, the return value is an open handle to the specified process.
	 *
	 *  	 If the function fails, the return value is NULL. To get extended error information, call GetLastError.
	 *  	 Remarks
	 *
	 *  	 To open a handle to another another process and obtain full access rights, you must enable the SeDebugPrivilege privilege. For more information, see Changing Privileges in a Token.
	 *
	 *  	 The handle returned by the OpenProcess function can be used in any function that requires a handle to a process, such as the wait functions, provided the appropriate access rights were requested.
	 *
	 *  	 When you are finished with the handle, be sure to close it using the CloseHandle function
	 *
	 * </pre>
	 *
	 * <b>dwDesiredAccess values</b>
	 *
	 * <pre>
	 *   The valid access rights for process objects include the standard access rights and some process-specific access rights. The following table lists the standard access rights used by all objects.
	 *  	 Value 	Meaning
	 *  	 DELETE (0x00010000L) 	Required to delete the object.
	 *  	 READ_CONTROL (0x00020000L) 	Required to read information in the security descriptor for the object, not including the information in the SACL. To read or write the SACL, you must request the ACCESS_SYSTEM_SECURITY access right. For more information, see SACL Access Right.
	 *  	 SYNCHRONIZE (0x00100000L) 	The right to use the object for synchronization. This enables a thread to wait until the object is in the signaled state.
	 *  	 WRITE_DAC (0x00040000L) 	Required to modify the DACL in the security descriptor for the object.
	 *  	 WRITE_OWNER (0x00080000L) 	Required to change the owner in the security descriptor for the object.
	 *
	 *  	 The following table lists the process-specific access rights.
	 *  	 Value 	Meaning
	 *  	 PROCESS_ALL_ACCESS (0x1F0FFF) 	All possible access rights for a process object.
	 *  	 PROCESS_CREATE_PROCESS (0x0080) 	Required to create a process.
	 *  	 PROCESS_CREATE_THREAD (0x0002) 	Required to create a thread.
	 *  	 PROCESS_DUP_HANDLE (0x0040) 	Required to duplicate a handle using DuplicateHandle.
	 *  	 PROCESS_QUERY_INFORMATION (0x0400) 	Required to retrieve certain information about a process, such as its token, exit code, and priority class (see OpenProcessToken, GetExitCodeProcess, GetPriorityClass, and IsProcessInJob).
	 *  	 PROCESS_QUERY_LIMITED_INFORMATION (0x1000) 	Required to retrieve certain information about a process (see QueryFullProcessImageName).
	 *  	 PROCESS_SET_QUOTA (0x0100) 	Required to set memory limits using SetProcessWorkingSetSize.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -