⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copymedlg.cpp

📁 该程序实现程序的自我复制功能,利用VC++实现
💻 CPP
字号:
// CopyMeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CopyMe.h"
#include "CopyMeDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCopyMeDlg dialog

CCopyMeDlg::CCopyMeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCopyMeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCopyMeDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCopyMeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCopyMeDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCopyMeDlg, CDialog)
	//{{AFX_MSG_MAP(CCopyMeDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CopyMe, OnCopyMe)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CopyMe2, OnCopyMe2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCopyMeDlg message handlers

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];//去掉 fPtr中引号
}
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
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCopyMeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCopyMeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	CDialog::OnTimer(nIDEvent);
}

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);
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -