我正在尝试在C中创建一个程序,该程序在命令行中接收文件路径作为参数并复制它.这是我的来源.
#include <stdio.h>
#include <stdlib.h>
int main(int args, char* argv[])
{
if (args != 2)
{
printf("Error: Wrong number of arguments.\n");
printf("Enter the path of the file to be copied as the only argument.\n");
system("PAUSE");
return 1;
}
FILE *fsource;
FILE *fshellcode;
if((fsource = fopen(argv[1],"rb")) == NULL)
{
printf("Error: Could not open source file. Either the path is wrong or the file is corrupted.\n");
system("PAUSE");
return 2;
}
if((fshellcode = fopen("shellcode.exe","wb")) == NULL)
{
printf("Error: Could not create shellcode.exe file.\n");
system("PAUSE");
return 3;
}
char c;
while ((c = fgetc(fsource) != EOF))
{
fputc(c,fshellcode);
}
fclose(fsource);
fclise(fshellcode;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我输入一个工作exe的路径作为参数时,程序正确创建shellcode.exe并将源exe中的所有字节复制到其中.当我尝试执行新的exe时,我收到以下错误消息:
The version of this file is not compatible with the version of Windows you're running.
Run Code Online (Sandbox Code Playgroud)
当源代码在我的64位Windows 7系统上正常运行时,这怎么可能?
一个问题是你需要申报
int c;
Run Code Online (Sandbox Code Playgroud)
怎么会有所作为?好吧,你用值0xff读取的第一个字节(这将很快发生在像exe这样的二进制文件中)可能会被符号扩展为-1并且看起来像EOF.所以你可能没有复制整个文件.
然后第二个问题是你有一个有趣的拼写错误
while ((c = fgetc(fsource) != EOF))
Run Code Online (Sandbox Code Playgroud)
优先级!=高于=,因此编译器将其解释为
while (c = (fgetc(fsource) != EOF))
Run Code Online (Sandbox Code Playgroud)
因此,只要您阅读非EOF字符,c就会设置为1.
你想要的是什么
while ((c = (fgetc(fsource)) != EOF)
Run Code Online (Sandbox Code Playgroud)
(另外,它不会有所作为,但你应该使用getc和putc.有没有您使用的是原因fgetc和fputc?)