Advertisement
mixster

mixster

Jul 20th, 2009
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.88 KB | None | 0 0
  1. library ExecProg;
  2.  
  3.  
  4.  
  5. uses
  6.  
  7.   FastShareMem,
  8.  
  9.   SysUtils,
  10.  
  11.   Classes,
  12.  
  13.   Windows,
  14.  
  15.   Graphics,
  16.  
  17.   ShellAPI;
  18.  
  19.  
  20.  
  21. {$R *.res}
  22.  
  23.  
  24.  
  25. type
  26.  
  27.   TSCARPlugFunc = record
  28.  
  29.     Name: string;
  30.  
  31.     Ptr: Pointer;
  32.  
  33.   end;
  34.  
  35.  
  36.  
  37. var
  38.  
  39.   curRun: Boolean;
  40.  
  41.   ProcessInfo: TProcessInformation;
  42.  
  43.   ReadPipe,WritePipe : THandle;
  44.  
  45.   Buffer : Pchar;
  46.  
  47.  
  48.  
  49. function InitProgram(DosApp: string): Boolean; stdcall;
  50.  
  51. const
  52.  
  53.   ReadBuffer = 2400;
  54.  
  55. var
  56.  
  57.   Security : TSecurityAttributes;
  58.  
  59.   Start : TStartUpInfo;
  60.  
  61. begin
  62.  
  63.   Result := False;
  64.  
  65.   if(curRun) then
  66.  
  67.     exit;
  68.  
  69.  
  70.  
  71.   with Security do begin
  72.  
  73.     nlength := SizeOf(TSecurityAttributes);
  74.  
  75.     binherithandle := true;
  76.  
  77.     lpsecuritydescriptor := nil;
  78.  
  79.   end;
  80.  
  81.   if Createpipe (ReadPipe, WritePipe, @Security, 0) then
  82.  
  83.   begin
  84.  
  85.     Buffer := AllocMem(ReadBuffer + 1);
  86.  
  87.     FillChar(Start,Sizeof(Start), #0);
  88.  
  89.     start.cb := SizeOf(start);
  90.  
  91.     start.hStdOutput := WritePipe;
  92.  
  93.     start.hStdInput := ReadPipe;
  94.  
  95.     start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
  96.  
  97.     start.wShowWindow := SW_HIDE;
  98.  
  99.  
  100.  
  101.     if CreateProcess(nil, PChar(DosApp), @Security, @Security, true, NORMAL_PRIORITY_CLASS, nil, nil, start, ProcessInfo) then
  102.  
  103.     begin
  104.  
  105.       Result := True;
  106.  
  107.       curRun := True;
  108.  
  109.       exit;
  110.  
  111.     end;
  112.  
  113.   end;
  114.  
  115. end;
  116.  
  117.  
  118.  
  119. function CheckProgram: Boolean; stdcall;
  120.  
  121. begin
  122.  
  123.   Result := (curRun) and (WaitForSingleObject(ProcessInfo.hProcess,100) <> WAIT_TIMEOUT);
  124.  
  125. end;
  126.  
  127.  
  128.  
  129. function FreeProgram: string; stdcall;
  130.  
  131. const
  132.  
  133.   ReadBuffer = 2400;
  134.  
  135. var
  136.  
  137.   BytesRead : DWord;
  138.  
  139.   RetVal: string;
  140.  
  141. begin
  142.  
  143.   if ((not curRun) or (not CheckProgram)) then
  144.  
  145.   begin
  146.  
  147.     Result := StrAlloc(1);
  148.  
  149.     if (curRun) then
  150.  
  151.       Result := '1'
  152.  
  153.     else
  154.  
  155.       Result := '0';
  156.  
  157.     exit;
  158.  
  159.   end;
  160.  
  161.  
  162.  
  163.   repeat
  164.  
  165.     BytesRead := 0;
  166.  
  167.     ReadFile(ReadPipe, Buffer[0], ReadBuffer, BytesRead,nil);
  168.  
  169.     Buffer[BytesRead]:= #0;
  170.  
  171.     OemToAnsi(Buffer, Buffer);
  172.  
  173.     RetVal := RetVal + String(Buffer);
  174.  
  175.   until (BytesRead < ReadBuffer);
  176.  
  177.  
  178.  
  179.   FreeMem(Buffer);
  180.  
  181.   CloseHandle(ProcessInfo.hProcess);
  182.  
  183.   CloseHandle(ProcessInfo.hThread);
  184.  
  185.   CloseHandle(ReadPipe);
  186.  
  187.   CloseHandle(WritePipe);
  188.  
  189.   curRun := False;
  190.  
  191.   Result := StrAlloc(Length(RetVal));
  192.  
  193.   Result := RetVal;
  194.  
  195. end;
  196.  
  197.  
  198.  
  199. function ExecProgram(DosApp: string): string; stdcall;
  200.  
  201. const
  202.  
  203.   ReadBuffer = 2400;
  204.  
  205. var
  206.  
  207.   Security : TSecurityAttributes;
  208.  
  209.   ReadPipe,WritePipe : THandle;
  210.  
  211.   Start : TStartUpInfo;
  212.  
  213.   ProcessInfo : TProcessInformation;
  214.  
  215.   Buffer : Pchar;
  216.  
  217.   BytesRead : DWord;
  218.  
  219.   Apprunning : DWord;
  220.  
  221.   RetVal: string;
  222.  
  223. begin
  224.  
  225.   With Security do begin
  226.  
  227.     nlength := SizeOf(TSecurityAttributes);
  228.  
  229.     binherithandle := true;
  230.  
  231.     lpsecuritydescriptor := nil;
  232.  
  233.   end;
  234.  
  235.   if Createpipe (ReadPipe, WritePipe, @Security, 0) then
  236.  
  237.   begin
  238.  
  239.     Buffer := AllocMem(ReadBuffer + 1);
  240.  
  241.     FillChar(Start,Sizeof(Start), #0);
  242.  
  243.     start.cb := SizeOf(start);
  244.  
  245.     start.hStdOutput := WritePipe;
  246.  
  247.     start.hStdInput := ReadPipe;
  248.  
  249.     start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
  250.  
  251.     start.wShowWindow := SW_HIDE;
  252.  
  253.  
  254.  
  255.     if CreateProcess(nil, PChar(DosApp), @Security, @Security, true, NORMAL_PRIORITY_CLASS, nil, nil, start, ProcessInfo) then
  256.  
  257.     begin
  258.  
  259.       repeat
  260.  
  261.         Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100);
  262.  
  263.       until (Apprunning <> WAIT_TIMEOUT);
  264.  
  265.       repeat
  266.  
  267.         BytesRead := 0;
  268.  
  269.         ReadFile(ReadPipe,Buffer[0], ReadBuffer,BytesRead,nil);
  270.  
  271.         Buffer[BytesRead]:= #0;
  272.  
  273.         OemToAnsi(Buffer,Buffer);
  274.  
  275.         RetVal := RetVal + String(Buffer);
  276.  
  277.       until (BytesRead < ReadBuffer);
  278.  
  279.     end;
  280.  
  281.     FreeMem(Buffer);
  282.  
  283.     CloseHandle(ProcessInfo.hProcess);
  284.  
  285.     CloseHandle(ProcessInfo.hThread);
  286.  
  287.     CloseHandle(ReadPipe);
  288.  
  289.     CloseHandle(WritePipe);
  290.  
  291.     Result := StrAlloc(Length(RetVal));
  292.  
  293.     Result := RetVal;
  294.  
  295.   end;
  296.  
  297. end;
  298.  
  299.  
  300.  
  301. function GetFunctionCount(): Integer; stdcall; export;
  302.  
  303. begin
  304.  
  305.   Result := 4;
  306.  
  307. end;
  308.  
  309.  
  310.  
  311. function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall;
  312.  
  313. begin
  314.  
  315.   case x of
  316.  
  317.     0:
  318.  
  319.       begin
  320.  
  321.         ProcAddr := @ExecProgram;
  322.  
  323.         StrPCopy(ProcDef, 'function ExecProgram(DosApp: string): string;');
  324.  
  325.       end;
  326.  
  327.     1:
  328.  
  329.       begin
  330.  
  331.         ProcAddr := @InitProgram;
  332.  
  333.         StrPCopy(ProcDef, 'function InitProgram(DosApp: string): Boolean;');
  334.  
  335.       end;
  336.  
  337.     2:
  338.  
  339.       begin
  340.  
  341.         ProcAddr := @CheckProgram;
  342.  
  343.         StrPCopy(ProcDef, 'function CheckProgram(): Boolean;');
  344.  
  345.       end;
  346.  
  347.     3:
  348.  
  349.       begin
  350.  
  351.         ProcAddr := @FreeProgram;
  352.  
  353.         StrPCopy(ProcDef, 'function FreeProgram(): string;');
  354.  
  355.       end;
  356.  
  357.   else
  358.  
  359.     x := -1;
  360.  
  361.   end;
  362.  
  363.   Result := x;
  364.  
  365. end;
  366.  
  367.  
  368.  
  369. exports GetFunctionCount;
  370.  
  371. exports GetFunctionInfo;
  372.  
  373. end.
  374.  
  375.  
  376.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement