2012-04-25 27 views
7

Có một chương trình tự xóaHiểu một chương trình tự xóa trong C++

#include <windows.h> 
#include <stdio.h> 

void main(int argc, char* argv[]) 
{ 
    STARTUPINFO si = {0}; 
    PROCESS_INFORMATION pi = {0}; 
    si.cb = sizeof(si); 

    if (argc == 1) 
    { 
     SECURITY_ATTRIBUTES sa; 
     sa.nLength = sizeof(sa); 
     sa.lpSecurityDescriptor = NULL; 
     sa.bInheritHandle = TRUE; 

     CopyFile(argv[0], "1.exe", FALSE); 
     MoveFile(argv[0], "2.exe"); 

     CreateFile("1.exe", 0, FILE_SHARE_READ, &sa, 
      OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL); 

     CreateProcess(NULL, "1.exe x", NULL, NULL, 
      TRUE, 0, NULL, NULL, &si, &pi); 
    } 
    else if (argc == 2) 
    { 
     while(!DeleteFile("2.exe")); 

     CreateProcess(NULL, "net", NULL, NULL, TRUE, 
      DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi); 
    } 
} 

Nếu tôi loại bỏ điều này: CreateProcess(NULL, "net", NULL, NULL, TRUE, DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi); nó không thể làm việc.
Có ai có thể giải thích cho tôi cách thức hoạt động của nó không?

+0

Nếu bạn chuyển đổi này để sử dụng wmain và chạy unicode, nó không hoạt động ... Odd. – Benj

+0

Vì chuỗi tên tệp là mã ascii. Có thể bạn thêm _T() quanh khuấy. – pl8787

+0

Haha, yeh tôi đã làm điều đó, nó đã biên dịch, tôi có nghĩa là nó không hoạt động trong thời gian chạy. – Benj

Trả lời

8

Dưới đây là một lời giải thích (như tôi hiểu điều)

void main(int argc, char* argv[]) 
{ 
    STARTUPINFO si = {0}; 
    PROCESS_INFORMATION pi = {0}; 
    si.cb = sizeof(si); 

    if (argc == 1) 
    { 
     SECURITY_ATTRIBUTES sa; 
     sa.nLength = sizeof(sa); 
     sa.lpSecurityDescriptor = NULL; 
     sa.bInheritHandle = TRUE; 

     // Make a copy of ourselves which we'll use to delete the version we were run from 
     CopyFile(argv[0], "1.exe", FALSE); 

     // Rename the running copy of ourself to another name 
     MoveFile(argv[0], "2.exe"); 

     // Make sure we delete the copy of ourselves that's going to delete us when we die 
     CreateFile("1.exe", 0, FILE_SHARE_READ, &sa, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL); 

     // Invoke the process that will delete us 
     // allowing it to inherit the handle we just created above. 
     CreateProcess(NULL, "1.exe x", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); 
    } 
    else if (argc == 2) 
    { 
     // Wait for the original program to die (deleting us and closing a handle), then delete it 
     while(!DeleteFile("2.exe")); 

     // Launch a child process which will inherit our file handles 
     // -- This keeps the file handle with FILE_FLAG_DELETE_ON_CLOSE (which we inherited) alive beyond our lifetime 
     // this allowing us to be deleted after we've died and our own handle is closed. 
     CreateProcess(NULL, "notepad", NULL, NULL, TRUE, DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi); 
    } 
} 
+0

Bạn có nghĩa là CreatProcess giữ Handle của chương trình này? Nhưng tôi vẫn không hiểu nó hoạt động như thế nào. Nó có nghĩa là nếu tôi gỡ bỏ nó, không có đủ thời gian cho chương trình để xóa chính nó? – pl8787

+1

Nếu xử lý FILE_FLAG_DELETE_ON_CLOSE bị đóng quá sớm, quá trình xóa sẽ thất bại vì quá trình 1.exe vẫn đang chạy. Bằng cách khởi chạy một đứa trẻ thừa kế tay cầm, tay cầm của 1.exe đóng lại cho phép nó bị xóa khi đứa trẻ đóng lại. – Benj

+0

Cảm ơn rất nhiều ~ Tôi nghĩ rằng tôi hiểu ý bạn là gì. – pl8787

Các vấn đề liên quan