全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1024
推到 Plurk!
推到 Facebook!

擷取net send aAlan Test之內容到TMemo內

尚未結案
aAlan
一般會員


發表:38
回覆:24
積分:12
註冊:2004-07-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-08-23 23:25:26 IP:61.228.xxx.xxx 未訂閱
前輩您好:     小弟想將由win2K命令提示字元執行之 「net send user名稱 訊息內容」所產生的結果轉向到Delphi 7的memo內,以利做字串分析,可是只能在memo1 呈現「訊息已經送到 aALAN。」這幾個字,無法把真正要傳的內容呈現在memo1上。(此例我真正想在memo1呈現的內容是:「測試發送訊息」這幾個字),請問諸位前輩有無良策?    unit Unit1;    interface    uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrls;    type   TForm1 = class(TForm)     Button1: TButton;     Memo1: TMemo;     procedure Button1Click(Sender: TObject);     function ShellApplication(AppName:String;AMemo:TMemo):string;     procedure SetNetSend(NetSendRecipient:PAnsiChar);   private     { Private declarations }         netSendRecipientBroadcast:PAnsiChar;   public     { Public declarations }   end;    var   Form1: TForm1;    implementation    {$R *.dfm}    procedure TForm1.Button1Click(Sender: TObject); var    nsr:TStringList;    i:integer;    netSendResult:string; begin       SetNetSend('Alan');       nsr:=TStringList.Create;       ExtractStrings([';'],[],netSendRecipientBroadcast,nsr);       for i:=0 to nsr.Count-1 do begin          ShowMessage(nsr.Strings[i]);          netSendResult:=ShellApplication('net.exe send '+nsr.Strings[i]+' 測試發送訊息',memo1); end; //end for end; procedure TForm1.SetNetSend(NetSendRecipient:PAnsiChar); begin netSendRecipientBroadcast:=NetSendRecipient; end; function TForm1.ShellApplication(AppName:String;AMemo:TMemo):string; const ReadBuffer = 1024; var Security : TSecurityAttributes; ReadPipe,WritePipe : THandle; start : TStartUpInfo; ProcessInfo : TProcessInformation; Buffer : Pchar; BytesRead : DWord; AppRunning : DWord; begin AMemo.Lines.Clear; With Security do begin //CreateProcess有一個security attribute的參數 nlength := SizeOf(TSecurityAttributes); binherithandle := true; lpsecuritydescriptor := nil; end; //end with Security if Createpipe(ReadPipe, WritePipe, @Security, 0) then begin Buffer := AllocMem(ReadBuffer 1); FillChar(Start,Sizeof(Start),#0); //因為startupinfo的membe太多, //故用fillChar先把所有的member //填上初值,然後在針對特定的member //給予特定的值,#0為NULL start.cb := SizeOf(start); start.hStdOutput := WritePipe; start.hStdInput := ReadPipe; start.dwFlags := STARTF_USESTDHANDLES STARTF_USESHOWWINDOW; start.wShowWindow := SW_HIDE; if CreateProcess(nil,PChar(AppName),@Security,@Security,true,NORMAL_PRIORITY_CLASS, nil,nil,start,ProcessInfo) then begin repeat AppRunning := WaitForSingleObject(ProcessInfo.hProcess,100); //Application.ProcessMessages; until (Apprunning <> WAIT_TIMEOUT); Repeat BytesRead := 0; //ReadFile讀完資料後,會把BytesRead移動到下一個未讀到的地方 ReadFile(ReadPipe,Buffer[0],ReadBuffer,BytesRead,nil); //ReadFile(ReadPipe,Buffer[1],ReadBuffer,BytesRead,nil); //ReadFile(ReadPipe,Buffer,ReadBuffer,BytesRead,nil); Buffer[BytesRead]:= #0; //利用ReadFile讀出Buffer[0]後,再將Buffer[0]設成nil表示已經讀過了。 //但如何知道資料是放在Buffer[0]? //OemToAnsi(Buffer,Buffer); AMemo.Text:=AMemo.Text String(Buffer); until (BytesRead < ReadBuffer); end; //end if CreateProcess FreeMem(Buffer); CloseHandle(ProcessInfo.hProcess); CloseHandle(ProcessInfo.hThread); CloseHandle(ReadPipe); CloseHandle(WritePipe); Result:=AMemo.Text end; //end if Createpipe end; //end procedure end.
Chance36
版主


發表:31
回覆:1033
積分:792
註冊:2002-12-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-09-02 02:04:37 IP:203.204.xxx.xxx 未訂閱
aAlan 你好     如你所述[將由win2K命令提示字元執行之 「net send user名稱 訊息內容」所產生的結果轉向到Delphi 7的memo內] 執行的結果正是[訊息已經送到 aALAN。」並沒有錯,而你希望顯示的是「測試發送訊息」,但是這幾個字並不是執行的結果呀!它是執行指令的參數,而此參數又是由程式給予的值啊!?     現在狀況:程式本身就是已知者,卻要求另一段程式(指令)傳回它不知道的值,這不是很奇怪嗎?
procedure TForm1.Button1Click(Sender: TObject);
var
  nsr:TStringList;
  i:integer;
  netSendResult:string;
begin
  SetNetSend('Alan');
  nsr:=TStringList.Create;
  ExtractStrings([';'],[],netSendRecipientBroadcast,nsr);
  for i:=0 to nsr.Count-1 do begin
    ShowMessage(nsr.Strings[i]);
    // ShellApplication 應該可以把Memo1的參數省略吧
    netSendResult:=ShellApplication('net.exe send ' nsr.Strings[i] ' 測試發送訊息',memo1);
    Memo1.Text := '' ;
    If 傳送成功 Then Begin
      Memo1.Text := nsr.Strings[i] ' 測試發送訊息' ;直接給值不就好了
  end; //end for 
end;      你可以從上述程式中得到,或從ShellApplication 中直接將訊息參數寫入到Memo1中亦可。    PS:以上說明不知是否有誤解你的意思?若有會錯意,敬請見諒!
_______________________________________ 深藍的魚,祝您好運..........連連
aAlan
一般會員


發表:38
回覆:24
積分:12
註冊:2004-07-23

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-09-03 15:09:04 IP:61.228.xxx.xxx 未訂閱
多謝您指出我的疏忽! [quote] aAlan 你好 如你所述[將由win2K命令提示字元執行之 「net send user名稱 訊息內容」所產生的結果轉向到Delphi 7的memo內] 執行的結果正是[訊息已經送到 aALAN。」並沒有錯,而你希望顯示的是「測試發送訊息」,但是這幾個字並不是執行的結果呀!它是執行指令的參數,而此參數又是由程式給予的值啊!? 現在狀況:程式本身就是已知者,卻要求另一段程式(指令)傳回它不知道的值,這不是很奇怪嗎?
系統時間:2024-05-07 20:29:24
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!