[英]C++ Rename File @ C:\Windows\System32\Drivers
SOLVED: have a look on the later posts已解决:看看后面的帖子
TASK: rename a file called TabletFilter.sys ( its my graphic tablet driver ) because windows 8 (my OS) apps needs an other driver then photoshop for the pin pressure.任务:重命名一个名为 TabletFilter.sys 的文件(它是我的图形平板电脑驱动程序),因为 Windows 8(我的操作系统)应用程序需要另一个驱动程序,然后是 photoshop 以获取引脚压力。 And I want to write a c++ program that just rename all driver files to .old我想编写一个 C++ 程序,将所有驱动程序文件重命名为 .old
The code based on the rename example from cplusplus.com基于 cplusplus.com 重命名示例的代码
#include <stdio.h>
int main ()
{
int result;
char oldname[] ="TabletFilter.sys";
char newname[] ="TabletFilter.sys.old";
result= rename( oldname , newname );
if ( result == 0 )
puts ( "File successfully renamed" );
else{
result= rename( newname , oldname );
if( result == 0)
puts ( "File successfully renamed" );
else
perror( "Error renaming file" );
}
return 0;
}
I tried "run as Admin" as well, but I still get我也试过“以管理员身份运行”,但我仍然得到
Error renaming file: No such file or directory
what can I do?我能做什么?
EDIT:编辑:
The file is definitly in the same folder ... i copyed them both there... even at c:\\windows\\ i tryed it ... and i use a manifest该文件肯定在同一个文件夹中......我将它们都复制到了那里......即使在 c:\\windows\\ 我也尝试过......并且我使用了清单
Executable: TabletRenameDriver.exe
Manifest: TabletRenameDriver.exe.manifest
Sample application manifest file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0"
processorArchitecture="X86"
name="TabletRenameDriver"
type="win32"/>
<description>Description of your application</description>
<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="requireAdministrator"
uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
i tryed uiAccess true and false.我试过 uiAccess 真假。
Yes I have a problem with the rights The driver is loaded but iam still able to change his name ... dont know why, but its possbile.是的,我的权利有问题 驱动程序已加载,但我仍然可以更改他的名字...不知道为什么,但可能会。 I tryed it.我试过了。 I want to solve this problem with c++ so pls dont tell me that there are many script languages out there that can handle the problem very well ;-) I know this我想用 C++ 解决这个问题,所以请不要告诉我有很多脚本语言可以很好地处理这个问题;-) 我知道这一点
The error message错误信息
No such file or directory
Means that the current directory does not contain files names "TabletFilter.sys"
or "TabletFilter.sys.old"
.表示当前目录不包含文件名"TabletFilter.sys"
或"TabletFilter.sys.old"
。
What you can do is either:你可以做的是:
However, even when you do this, your program may very well fail.但是,即使您这样做,您的程序也很可能会失败。 You may not have sufficient rights to modify the contents of that folder.您可能没有足够的权限来修改该文件夹的内容。 And the file you are trying to rename may well be locked if the OS has loaded the driver.如果操作系统已加载驱动程序,您尝试重命名的文件很可能会被锁定。
And as @Hans points out, the file system redirector could very well be confounding matters for you.正如@Hans 指出的那样, 文件系统重定向器很可能会让您感到困惑。 The best way to avoid that is to us a 64 bit process.避免这种情况的最佳方法是我们使用 64 位进程。
I see no reason for using a C++ program here.我认为没有理由在这里使用 C++ 程序。 Renaming files is a task best suited to a scripting language.重命名文件是最适合脚本语言的任务。
Thanks for all your help感谢你的帮助
I finished my programm and it works perfectly now我完成了我的程序,现在它运行良好
note: added "requireAdministrator" uiAccess="false" via VS2012 Settings注意:通过 VS2012 设置添加了“requireAdministrator”uiAccess="false"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
void main ()
{
bool success = false;
bool somethingWentWrong = false;
PVOID OldValue = NULL;
if( Wow64DisableWow64FsRedirection(&OldValue) )
{
success = MoveFile("C:\\Windows\\System32\\Drivers\\TabletFilter.sys", "C:\\Windows\\System32\\Drivers\\TabletFilter.sys.old");
if(success){
puts("from .sys to .sys.old");
success = MoveFile("C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.sys", "C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.sys.old");
if(!success){ puts("ERROR: C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.sys not found"); somethingWentWrong = true;}
success = MoveFile("C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.inf", "C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.inf.old");
if(!success){ puts("ERROR: C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.inf not found"); somethingWentWrong = true;}
success = MoveFile("C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.cat", "C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.cat.old");
if(!success){ puts("ERROR: C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.cat not found"); somethingWentWrong = true;}
}else{
puts("from .sys.old to .sys");
success = MoveFile("C:\\Windows\\System32\\Drivers\\TabletFilter.sys.old", "C:\\Windows\\System32\\Drivers\\TabletFilter.sys");
if(!success){ puts("ERROR: C:\\Windows\\System32\\Drivers\\TabletFilter.sys.old not found"); somethingWentWrong = true;}
success = MoveFile("C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.sys.old", "C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.sys");
if(!success){ puts("ERROR: C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.sys.old not found"); somethingWentWrong = true;}
success = MoveFile("C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.inf.old", "C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.inf");
if(!success){ puts("ERROR: C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.inf.old not found"); somethingWentWrong = true;}
success = MoveFile("C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.cat.old", "C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.cat");
if(!success){ puts("ERROR: C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.cat.old not found"); somethingWentWrong = true;}
}
if(somethingWentWrong){
puts ( "ERROR: File were set to standart!" );
success = MoveFile("C:\\Windows\\System32\\Drivers\\TabletFilter.sys.old", "C:\\Windows\\System32\\Drivers\\TabletFilter.sys");
if(!success){ puts("ERROR: C:\\Windows\\System32\\Drivers\\TabletFilter.sys.old not found"); somethingWentWrong = true;}
success = MoveFile("C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.sys.old", "C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.sys");
if(!success){ puts("ERROR: C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.sys.old not found"); somethingWentWrong = true;}
success = MoveFile("C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.inf.old", "C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.inf");
if(!success){ puts("ERROR: C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.inf.old not found"); somethingWentWrong = true;}
success = MoveFile("C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.cat.old", "C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.cat");
if(!success){ puts("ERROR: C:\\Windows\\System32\\DriverStore\\FileRepository\\tabletfilter.inf_amd64_e7d03235d8288d3b\\TabletFilter.cat.old not found"); somethingWentWrong = true;}
}else
puts ( "File successfully renamed" );
Wow64RevertWow64FsRedirection(OldValue);
}
for(unsigned long tick = GetTickCount() / 1000; tick + 2 > GetTickCount() / 1000;){
}
}
With only 48 lines of code ... can a script be shorter?
只有 48 行代码……脚本可以更短吗?
PS: Revisiting this post after some time, I think there are quite a few ways to approach this issue differently. PS:一段时间后重温这篇文章,我认为有很多方法可以以不同的方式处理这个问题。 However, the main point still stands: If you want to do something like this, you have to run with admin permissions ... and maybe use loops, lists/vectors and some proper string manipulation :-P然而,要点仍然存在:如果你想做这样的事情,你必须以管理员权限运行......并且可能使用循环、列表/向量和一些适当的字符串操作:-P
Many of those drivers are needed by Windows to startup. Windows 需要其中许多驱动程序才能启动。 If you did succeed in renaming them, bad things will happen to your machine when you restart - namely your machine would not boot.如果您确实成功地重命名了它们,那么当您重新启动时,您的机器会发生不好的事情 - 即您的机器将无法启动。
As a general rule, leave all of the stuff in C:\\Windows alone, but this advice is ten-fold more important for the C:\\Windows\\drivers directory.作为一般规则,将所有内容留在 C:\\Windows 中,但此建议对于 C:\\Windows\\drivers 目录的重要性要高十倍。 Touch this folder and anything in it at your peril.触摸此文件夹及其中的任何内容,您都将面临危险。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.