使用sharememory為何仍存在前一次的資料 |
缺席
|
macchen
初階會員 發表:66 回覆:102 積分:33 註冊:2006-07-07 發送簡訊給我 |
造成這個問題的原因,是因為我在fillchar(p, sizeof(p), 0),p是一個pointer的型態,因為我不知道sizeof()的涵數是取得變數的type,所以怎麼取都是只4bytes,所以導致輸入長取超過這個長度時,會導致長度後面的字母仍會留著,不知我這個觀念是否有錯(因為我覺的如果用sizeof(p)就取出4bytes,那麼長度應該是32bit才對,可是我打10個字母就有問題了),請大家指導一下,謝謝。
原程式碼如下(希望大家給點觀念上的指導): [code delphi] unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Memo2: TMemo; Button1: TButton; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; p,p1:pointer; h, h1:hwnd; implementation {$R *.dfm} procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin if h <> 0 then closehandle(h); if h1 <> 0 then closehandle(h1); if p <> nil then unmapviewoffile(p); if p1 <> nil then unmapviewoffile(p1); end; procedure TForm1.Button1Click(Sender: TObject); var s:pchar; i:integer; begin s := pchar(trim(memo1.Lines.Text)); copymemory(@(p^), s, length(s)); //memo1.GetTextBuf(p, 255); h1 := OpenFileMapping(File_MAP_ALL_ACCESS, False, pchar('tester')); if h1 = 0 then application.Terminate; p1 := mapviewoffile(h1, file_map_all_access, 0,0, 0); if p1 = nil then application.Terminate ; memo2.Lines.Clear ; memo2.Lines.Add(strpas(p)); //FillChar(p1^, Length(s), 0);FillChar(p1^, Length(s), 0); FillChar(p1^, Length(s), 0); end; procedure TForm1.FormCreate(Sender: TObject); begin h := createfilemapping($FFFFFFFF, nil, page_readwrite, 0, sizeof(p), pchar('tester')); if h = 0 then application.Terminate ; p := mapviewoffile(h, file_map_all_access, 0,0,0); if p = nil then application.Terminate ; end; end. [/code]
------
DELPHI初學者 |
P.D.
版主 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |