我的程序是根据一个MFC程序改过来的,结果不能完成复制。
程序的目的是运行时能够对自身对行复制。#include<windows.h>
#include<string.h>
#include<stdio.h>
void main()
{
char name[256]; //当前程序的全路径,不包括文件名
char SelfName[256]; //自己的文件名
char *fPtr=GetCommandLine();
strncpy(name,fPtr,255);
for(int i=0;i<256;i++)
{
if(i>0&&name[i]=='\'')

name[i-1]=0;
break; 
}
name[i]=fPtr[i+1];
}
i=strlen(name)-1;
int len=strlen(name)-1;
for(i=len;i>=0;i--)
{
if(name[i]=='\\') break;
}
memset(SelfName,0,256);
strcpy(SelfName,name+i+1);
name[i+1]=0;


HANDLE hFile=NULL;
DWORD dwSize, bytes_read;
BYTE *ptr;
char * filename=NULL;
filename=(char*)malloc(100*sizeof(char));
filename=name;
strcat(filename,SelfName);

hFile=CreateFile(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"不能打开文件!\n\n","1",MB_OK);
return;
}
dwSize = GetFileSize(hFile, NULL);
    ptr=(BYTE*)calloc(1,dwSize);
ReadFile(hFile, ptr, dwSize, &bytes_read, NULL);
CloseHandle(hFile);

char* newFile=NULL;
newFile=(char*)malloc(100*sizeof(char));
newFile="c:\\";
strcat(newFile,SelfName);
hFile = CreateFile(newFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS,
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"不能建立文件.\n\n","2",MB_OK);
return;
}
    WriteFile(hFile, ptr, dwSize, &bytes_read, NULL);
CloseHandle(hFile);
    free(ptr);
    MessageBox(NULL,"自我复制成功!","3",MB_OK);


}源MFC程序:http://down.csdn.net/source/1253821

解决方案 »

  1.   

    帮你调了一下,发现有个值不对#include<windows.h>
    #include<string.h>
    #include<stdio.h>
    void main()
    {    
        char name[256]; //当前程序的全路径,不包括文件名
        char SelfName[256]; //自己的文件名
        char *fPtr=GetCommandLine();
        strncpy(name,fPtr,255);
        for(int i=0;i<256;i++)
        {
            if(i>0&&name[i]=='\'')
            { 
                name[i-1]=0;
                break; 
            }
            name[i]=fPtr[i+1];
        }
        i=strlen(name)-1;
        int len=strlen(name)-1;
        for(i=len;i>=0;i--)
        {
            if(name[i]=='\\') break;
        }
        memset(SelfName,0,256);
        strcpy(SelfName,name+i+1);
        name[i+1]=0;
        
        
        HANDLE hFile=NULL;
        DWORD dwSize, bytes_read;
        BYTE *ptr;
        char * filename=NULL;
        filename=(char*)malloc(100*sizeof(char));
        filename=name;
        strcat(filename,SelfName);
        
        hFile=CreateFile(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,  //这里filename应该是源文件吧,盘符被你处理掉了
            NULL, OPEN_EXISTING,
            FILE_FLAG_SEQUENTIAL_SCAN, NULL);
        if(hFile == INVALID_HANDLE_VALUE)
        {
            MessageBox(NULL,"不能打开文件!\n\n","1",MB_OK);
            return;
        }
        dwSize = GetFileSize(hFile, NULL);
        ptr=(BYTE*)calloc(1,dwSize);
        ReadFile(hFile, ptr, dwSize, &bytes_read, NULL);
        CloseHandle(hFile);
        
        char* newFile=NULL;
        newFile=(char*)malloc(100*sizeof(char));
        newFile="c:\\";
        strcat(newFile,SelfName);
        hFile = CreateFile(newFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
            NULL, CREATE_ALWAYS,
            FILE_FLAG_SEQUENTIAL_SCAN, NULL);
        if(hFile == INVALID_HANDLE_VALUE)
        {
            MessageBox(NULL,"不能建立文件.\n\n","2",MB_OK);
            return;
        }
        WriteFile(hFile, ptr, dwSize, &bytes_read, NULL);
        CloseHandle(hFile);
        free(ptr);
        MessageBox(NULL,"自我复制成功!","3",MB_OK);
        
        
    }你应该可以直接用CopyFile就可以了,不用那么多代码。
      

  2.   

    复制自身的话,CopyFile 好像不行,试过了
      

  3.   

    刚才把那年MFC程序传上去了但是半天还没出来。
    干脆把代码发到这吧:
    BOOL CCopyMeDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here
    char *fPtr=::GetCommandLine();
    strncpy(name,fPtr,255);
    for(int i=0;i<256;i++){
    if(i>0&&name[i]=='\"'){ name[i-1]=0; break; }
         name[i]=fPtr[i+1];
    }
    i=strlen(name)-1;int len=strlen(name)-1;
    for(i=len;i>=0;i--){
    if(name[i]=='\\')break;
    }
    memset(SelfName,0,256);
    strcpy(SelfName,name+i+1);
    name[i+1]=0; return TRUE;  // return TRUE  unless you set the focus to a control
    }void CCopyMeDlg::OnCopyMe() 
    {CString FilePtr=name;
    //fptr 很长,必须使到空格处中断//MessageBox(fPtr,FilePtr,MB_OK);
    //CString FilePtr1=fPtr;
    //CString inf; //可试验出fptr很长
    //inf.Format("%s,,%d,%d",name,FilePtr.GetLength(),strlen(name));
    //AfxMessageBox(inf);//
    //return;
    HANDLE hFile = {NULL};
    DWORD dwSize, bytes_read;
    BYTE *ptr;
    CString filename=name;
    filename+=SelfName;
    hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL, OPEN_EXISTING,
    FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if(hFile == INVALID_HANDLE_VALUE)
    {
    MessageBox("不能打开文件...\n\n");
    return;
    }
    dwSize = GetFileSize(hFile, NULL);
        ptr=(BYTE*)calloc(1,dwSize);
    ReadFile(hFile, ptr, dwSize, &bytes_read, NULL);
    CloseHandle(hFile); CString newFile="c:\\";
    newFile+=SelfName;
    hFile = CreateFile(newFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL, CREATE_ALWAYS,
    FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if(hFile == INVALID_HANDLE_VALUE)
    {
    MessageBox("不能建立文件.\n\n");
    return;
    }
        WriteFile(hFile, ptr, dwSize, &bytes_read, NULL);
    CloseHandle(hFile);
        free(ptr);
        AfxMessageBox("自我复制成功!");
    }void CCopyMeDlg::OnCopyMe2() 
    {
    HMODULE h=GetModuleHandle(NULL);
    char name[256];
    FILE *fp;
    if(GetModuleFileName(h,name,256))
    {
    if(!(fp=fopen(name,"rb"))){
           AfxMessageBox("无法复制!");
       return;}
    FILE *fp2;
    CString str=name;
    CString newName;
    int pos=str.ReverseFind('\\');
        newName=str.Mid(pos+1,str.GetLength()-pos-1);
    str="c:\\";
    str+=newName;
    if(!(fp2=fopen(str.GetBuffer(0),"wb"))){
           AfxMessageBox("无法建立!");
       return;}         
    char buf[1024];int len;
        do{
          len=fread(buf,1,1024,fp);
          if(len==0)break;
      fwrite(buf,1,len,fp2);
    }while(1);
        fclose(fp);
    fclose(fp2);
    }
    }