Recent

Author Topic: My code doesn't executed  (Read 231 times)

whitehat

  • Jr. Member
  • **
  • Posts: 86
My code doesn't executed
« on: December 05, 2018, 12:05:34 pm »
i can't execute my command to delete some file can any one help me ?
Code: Pascal  [Select]
  1. procedure removefinal(name: AnsiString);
  2. var
  3.   comand: AnsiString;
  4.   si: STARTUPINFOA;
  5.   pi: PROCESS_INFORMATION;
  6. begin
  7.  
  8.   comand :='del /q '+name+':\a.txt';
  9.  
  10.   ZeroMemory(@si, sizeof(si));
  11.   si.cb := sizeof(si);
  12.   si.dwFlags := STARTF_USESHOWWINDOW;
  13.   si.wShowWindow := SW_NORMAL;
  14.  
  15.   if CreateProcessA(nil, PAnsiChar(comand), nil, nil, False, 0, nil, nil, @si, @pi) then
  16.   begin
  17.     WaitForSingleObject(pi.hProcess, INFINITE);
  18.     CloseHandle(pi.hThread);
  19.     CloseHandle(pi.hProcess);
  20.   clrscr;
  21.   end else
  22.   begin
  23.     // error handling, use GetLastError() to find out why CreateProcess() failed...
  24.   end;
  25. end;
  26. begin
  27. readln(name);
  28. removefinal(name);
  29. end.
  30.  

rvk

  • Hero Member
  • *****
  • Posts: 3502
Re: My code doesn't executed
« Reply #1 on: December 05, 2018, 12:10:34 pm »
First, you might want to provide us with some more information.

*) What OS are you using (and version)

*) Does your code reach WaitForSingleObject? (debug and trace it yourself)

*) If it comes to // error handling, why do you not provide any error handling-code?

*) What did you provide for "name" in this procedure. If it's just C, you should know that a user might not have permission to delete a file from the root directory on Windows 8+ systems.

whitehat

  • Jr. Member
  • **
  • Posts: 86
Re: My code doesn't executed
« Reply #2 on: December 05, 2018, 12:12:34 pm »
windows 7 my real code is a simple program that remove shortcut virus

Code: Pascal  [Select]
  1. program remove;
  2. uses
  3.   crt,
  4.   Windows,   // for constant SW_NORMAL
  5.   ShellApi;  // for function ShellExecute
  6. var
  7.   name:AnsiString;
  8. procedure remove(var name: AnsiString);
  9. var
  10.   comand: AnsiString;
  11.   si: STARTUPINFOA;
  12.   pi: PROCESS_INFORMATION;
  13. begin
  14.    writeln('Put the name of flash disk :');
  15.    readln(name);
  16.    comand := 'attrib -r -a -s -h /s /d '+name+':\*.*';
  17.  
  18.   ZeroMemory(@si, sizeof(si));
  19.   si.cb := sizeof(si);
  20.   si.dwFlags := STARTF_USESHOWWINDOW;
  21.   si.wShowWindow := SW_NORMAL;
  22.  
  23.   if CreateProcessA(nil, PAnsiChar(comand), nil, nil, False, 0, nil, nil, @si, @pi) then
  24.   begin
  25.     WaitForSingleObject(pi.hProcess, INFINITE);
  26.     CloseHandle(pi.hThread);
  27.     CloseHandle(pi.hProcess);
  28.   clrscr;
  29.   end else
  30.   begin
  31.     // error handling, use GetLastError() to find out why CreateProcess() failed...
  32.   end;
  33. end;
  34.  
  35. procedure removefinal(name: AnsiString);
  36. var
  37.   comand: AnsiString;
  38.   si: STARTUPINFOA;
  39.   pi: PROCESS_INFORMATION;
  40. begin
  41.  
  42.   comand :='del /q '+name+':\a.txt';
  43.  
  44.   ZeroMemory(@si, sizeof(si));
  45.   si.cb := sizeof(si);
  46.   si.dwFlags := STARTF_USESHOWWINDOW;
  47.   si.wShowWindow := SW_NORMAL;
  48.  
  49.   if CreateProcessA(nil, PAnsiChar(comand), nil, nil, False, 0, nil, nil, @si, @pi) then
  50.   begin
  51.     WaitForSingleObject(pi.hProcess, INFINITE);
  52.     CloseHandle(pi.hThread);
  53.     CloseHandle(pi.hProcess);
  54.   clrscr;
  55.   end else
  56.   begin
  57.     // error handling, use GetLastError() to find out why CreateProcess() failed...
  58.   end;
  59. end;
  60. begin
  61. remove(name);
  62. removefinal(name);
  63. end.

rvk

  • Hero Member
  • *****
  • Posts: 3502
Re: My code doesn't executed
« Reply #3 on: December 05, 2018, 12:17:07 pm »
windows 7 my real code is a simple program that remove shortcut virus
Oops. The Windows 8+ I mentioned should be Windows 7+.

If you don't run your program as administrator (i.e. elevated), you don't have permission to remove (or create) a file from the root directory. You are trying to delete C:\a.txt which is not permitted. (try it from a simple cmd-command line yourself)

A virus probably ran as administrator (elevated) and could create the C:\a.txt.

So try running your program elevated and see if it works.

whitehat

  • Jr. Member
  • **
  • Posts: 86
Re: My code doesn't executed
« Reply #4 on: December 05, 2018, 12:19:55 pm »
i just try to remove the virus from my flash disc

rvk

  • Hero Member
  • *****
  • Posts: 3502
Re: My code doesn't executed
« Reply #5 on: December 05, 2018, 12:49:06 pm »
Ok, in that case the command should work. But have you traced it yourself (one of my recommendations)??

It gives an error 2 (file not found). Even though you provide a complete commandline, the command you give should be a program, not an internal command (which del is). If you put "cmd /c" before the commandline, it will work.

Quote
To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file.
lpCommandLine
The command line to be executed.
So it should point to a .exe. Windows itself strips the executable from the parameters and runs that executable. But "del" is not an executable but an internal command from cmd.exe.

So set lpApplicationName to "cmd.exe" and put "/c" in front of "del" or set "cmd /c" in front of the command, which works as well.

But I think it is a really bad way to do it like this. Why don't you just use DeleteFile(name + ':\a.txt'); ?
Works just as well with much less code (that can go wrong).

« Last Edit: December 05, 2018, 12:53:38 pm by rvk »

whitehat

  • Jr. Member
  • **
  • Posts: 86
Re: My code doesn't executed
« Reply #6 on: December 05, 2018, 02:01:04 pm »
i need to execute the command del *.lnk to delete all the virus that is why

Handoko

  • Hero Member
  • *****
  • Posts: 2675
  • My goal: build my own game engine using Lazarus
Re: My code doesn't executed
« Reply #7 on: December 05, 2018, 02:16:46 pm »
Some of the files maybe unremoveable because the virus is active in memory. Even if they can be deleted, the virus will regenerate those files immediate after the removal.

If it is a media autorun virus, you can check by doing these:

Insert an empty flashdisk in the computer. If some files (which usually are hidden) generated automatically, the computer probably already infected by virus.

Plug the flashdisk to another computer, preferably with Linux OS to avoid being infected. Delete those files.

Then plug it back to the suspicious computer. If the files are being generated again, you can be sure the virus is active.

rvk

  • Hero Member
  • *****
  • Posts: 3502
Re: My code doesn't executed
« Reply #8 on: December 05, 2018, 02:21:02 pm »
i need to execute the command del *.lnk to delete all the virus that is why
You can't use wildcards with DeleteFile but you can with SHFileOperation.

Working example:
Code: Pascal  [Select]
  1. program remove;
  2. uses
  3.   ShellApi;  // for function SHFileOperationA
  4. var
  5.   Name: ansistring = 'H'; // this should be user-defined/asked
  6.   Op: SHFILEOPSTRUCTA;
  7. begin
  8.   FillChar(op, Sizeof(op), #0);
  9.   op.wFunc := FO_DELETE;
  10.   op.pFrom := pAnsiChar(name + ':\*.lnk' + #0); // note the extra #0
  11.   op.fFlags := FOF_FILESONLY or FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI or FOF_NORECURSION;
  12.   if SHFileOperationA(@op) <> 0 then writeln('error') else writeln('success');
  13. end.

You could also use recursion with this etc. Everything is better than running cmd.exe for this task. Especially if you don't check for errors (which you didn't).