简体   繁体   English

CPP自我复制访问被拒绝

[英]Cpp self copy access denied

So i am trying to create a self replicating .exe in c++ using code::blocks . 所以我正在尝试使用code::blocks在c ++中创建一个自我复制的.exe。 I have written the code and on my local machine it works flawlessly. 我已经编写了代码,并且在我的本地计算机上它可以完美运行。 After loading it on a Windows 7 Virtual machine and 2 other PCs, i realized it does not work. 在Windows 7虚拟机和2台其他PC上加载后,我意识到它不起作用。 It comes up with the error 它提出了错误

"Can not copy file, access denied" “无法复制文件,访问被拒绝”

My PC probably has different permissions set i am assuming which is why it works on mine and not others. 我假设我的PC设置了不同的权限,这就是为什么它可以在我的而不是其他设备上运行的原因。 Here is the section of code: 这是代码部分:

ifstream check( "C:\\newfile.exe" );

if( !check.is_open() )
{
    char bufferf[MAX_PATH];
    GetModuleFileName( NULL, bufferf, MAX_PATH );
    std::stringstream str;

    str << "copy " << bufferf << " C:\\newfile.exe";
    system( str.str().c_str() );
    system( "start c:\\newfile.exe" );
    cout << __argv[0] << endl;
    return 1;
}
else
{
    return 0;

}

Remember i am very new to c++, long time java, php and c# developer but i figured it is time to learn a lower level language. 记得我对C ++,Java,PHP和C#开发人员很陌生,但是我认为是时候学习一种较低级的语言了。 If you have any suggestions on making this a more efficient function then please let me know.show any code you can since im not 100% familiar with all the libraries yet. 如果您对使此功能更有效有任何建议,请让我知道。显示您可以的所有代码,因为我不是100%熟悉所有库。 long story short i want to copy the current exe to the cdrive, run that copied file and close the original. 长话短说,我想将当前exe复制到cdrive,运行复制的文件并关闭原始文件。 here is another way i tried 这是我尝试过的另一种方式

ifstream check( "C:\\newFile.exe" );

if( !check.is_open() )
{
    char bufferf[MAX_PATH];
    GetModuleFileName( NULL, bufferf, MAX_PATH );

    char * original_file = bufferf;
    char * new_file = ( char * )"c:\\newFile.exe";
    ( void ) CopyFile( ( LPCTSTR )original_file, ( LPCTSTR )new_file, FALSE );
    system( "start c:\\newFile.exe" );
    cout << __argv[0];
    return 1;
}
else
{
    return 0;
}

Still no success. 仍然没有成功。 but both work flawlessly on my development machine (from what i can see) i heard people say you can not copy running exe files in windows but that is not true, i feel it is a permissions issue but i may be wrong. 但是两者都可以在我的开发机器上完美地工作(从我所看到的),我听说人们说您不能在Windows中复制正在运行的exe文件,但这不是真的,我觉得这是一个权限问题,但我可能是错的。

Any tips on self replication are welcome, i'm open to new methods. 欢迎提供有关自我复制的任何技巧,我欢迎新方法。

please know that, Windows Vista and Windows 7 have UAC enabled which doesn't allow you to "write" to certain folders. 请注意,Windows Vista和Windows 7启用了UAC,不允许您“写入”某些文件夹。 You might have to run the program in Administrator mode or disable UAC altogether for your program to copy/create file from one location to the protected special folder. 您可能必须在管理员模式下运行该程序,或者完全禁用UAC,程序才能将文件从一个位置复制/创建到受保护的特殊文件夹。

To request the user to allow a program to be run in administrative mode, you must define and link a manifest file to your application: This tells you how to do it. 要请求用户允许程序在管理模式下运行,您必须定义清单文件并将其链接到您的应用程序:这告诉您如何做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM