C++调整进程的特权的实现方法

[摘要]  Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了。如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归。

Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了。如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归。

下面是的一个解决方案,借助Apache Commons IO工具包来简单实现文件(夹)的复制、移动、删除、获取大小等操作。

import org.apache.commons.io.FileUtils;

import org.apache.commons.io.filefilter.*;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import java.io.*;

/**

* 文件工具箱

*

* @author leizhimin 2008-12-15 13:59:16

*/

public final class FileToolkit {

private static final Log log = LogFactory.getLog(FileToolkit.class);

/**

* 复制文件或者目录,复制前后文件完全一样。

*

* @param resFilePath 源文件路径

* @param distFolder 目标文件夹

* @IOException 当操作发生异常时抛出

*/

public static void copyFile(String resFilePath, String distFolder) throws IOException {

File resFile = new File(resFilePath);

File distFile = new File(distFolder);

if (resFile.isDirectory()) {

FileUtils.copyDirectoryToDirectory(resFile, distFile);

} else if (resFile.isFile()) {

FileUtils.copyFileToDirectory(resFile, distFile, true);

}

}

/**

* 删除一个文件或者目录

*

* @param targetPath 文件或者目录路径

* @IOException 当操作发生异常时抛出

*/

public static void deleteFile(String targetPath) throws IOException {

File targetFile = new File(targetPath);

if (targetFile.isDirectory()) {

FileUtils.deleteDirectory(targetFile);

} else if (targetFile.isFile()) {

targetFile.delete();

}

}

/**

* 移动文件或者目录,移动前后文件完全一样,如果目标文件夹不存在则创建。

*

* @param resFilePath 源文件路径

* @param distFolder 目标文件夹

* @IOException 当操作发生异常时抛出

*/

public static void moveFile(String resFilePath, String distFolder) throws IOException {

File resFile = new File(resFilePath);

File distFile = new File(distFolder);

if (resFile.isDirectory()) {

FileUtils.moveDirectoryToDirectory(resFile, distFile, true);

} else if (resFile.isFile()) {

FileUtils.moveFileToDirectory(resFile, distFile, true);

}

}

/**

* 重命名文件或文件夹

*

* @param resFilePath 源文件路径

* @param newFileName 重命名

* @return 操作成功标识

*/

public static boolean renameFile(String resFilePath, String newFileName) {

String newFilePath = StringToolkit.formatPath(StringToolkit.getParentPath(resFilePath) + "/" + newFileName);

File resFile = new File(resFilePath);

File newFile = new File(newFilePath);

return resFile.renameTo(newFile);

}

/**

* 读取文件或者目录的大小

*

* @param distFilePath 目标文件或者文件夹

* @return 文件或者目录的大小,如果获取失败,则返回-1

*/

public static long genFileSize(String distFilePath) {

File distFile = new File(distFilePath);

if (distFile.isFile()) {

return distFile.length();

} else if (distFile.isDirectory()) {

return FileUtils.sizeOfDirectory(distFile);

}

return -1L;

}

/**

* 判断一个文件是否存在

*

* @param filePath 文件路径

* @return 存在返回true,否则返回false

*/

public static boolean isExist(String filePath) {

return new File(filePath).exists();

}

/**

* 本地某个目录下的文件列表(不递归)

*

* @param folder ftp上的某个目录

* @param suffix 文件的后缀名(比如.mov.xml)

* @return 文件名称列表

*/

public static String[] listFilebySuffix(String folder, String suffix) {

IOFileFilter fileFilter1 = new SuffixFileFilter(suffix);

IOFileFilter fileFilter2 = new NotFileFilter(DirectoryFileFilter.INSTANCE);

FilenameFilter filenameFilter = new AndFileFilter(fileFilter1, fileFilter2);

return new File(folder).list(filenameFilter);

}

/**

* 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)

*

* @param res 原字符串

* @param filePath 文件路径

* @return 成功标记

*/

public static boolean string2File(String res, String filePath) {

boolean flag = true;

BufferedReader bufferedReader = null;

BufferedWriter bufferedWriter = null;

try {

File distFile = new File(filePath);

if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs();

bufferedReader = new BufferedReader(new StringReader(res));

bufferedWriter = new BufferedWriter(new FileWriter(distFile));

char buf[] = new char[1024]; //字符缓冲区

int len;

while ((len = bufferedReader.read(buf)) != -1) {

bufferedWriter.write(buf, 0, len);

}

bufferedWriter.flush();

bufferedReader.close();

bufferedWriter.close();

} catch (IOException e) {

flag = false;

e.printStackTrace();

}

return flag;

}

}

通过OpenProcess(PROCESS_ALL_ACCESS,FALSE,dId)获取ID较低进程的句柄时可能会得到错误代码,这些进程都是保持系统活动的系统服务。一个普通用户进程不允许执行针对系统服务的所有操作。如果一个程序意外终止了一个系统服务,那么整个系统都将崩溃。因此,一个进程只有拥有确切的访问权限才会有适当的特权。

由于多种原因,调试器必须拥有大量的权限来完成他的工作。改变进程的特权可以通过以下三个简单的基本步骤:

1. 首先,必须打开进程的访问令牌(access token),使用advapi32.dll中的函数OpenProcessToken()。

2. 如果上一步正确完成,接下来就是准备TOKEN_PRIVILEGES结构,该结构包含有关要请求的特权的信息。这个工作需要advapi32.dll中的另一个函数LookupPrivilegeValue()的帮助。特权通过名称来指定。SDK文档winnt.h定义了27中特权名称和其对应的符号名称。例如,调试权限的符号名称为:SE_DEBUG_NAME,该名称和字符串“SeDebugPrivilege”等效。

3. 如果上一步正确完成,就可以使用进程的令牌句柄(Token Handle)来调用AdjustTokenPrivileges()函数以初始化TOKEN_PRIVILEGES结构。该函数也是advapi32.dll导出的。

4. 如果OpenProcessToken()调用成功,要记得关闭其返回的令牌句柄(Token Handle)。w2k_dbg.dll包含一个dbgPrivilegeSet()函数,该函数合并了这几个步骤,还有w2k_dbg.dll中的另一个函数:dbgPrivilegeDebug()。此函数是dbgPrivilegeSet()的一个外包函数,为了便于设定调试特权。




免责声明:

本站系本网编辑转载,会尽可能注明出处,但不排除无法注明来源的情况,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与本网联系, 来信: liujun@soft6.com 我们将在收到邮件后第一时间删除内容!

[声明]本站文章版权归原作者所有,内容为作者个人观点,不代表本网站的观点和对其真实性负责,本站拥有对此声明的最终解释权。