99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀實(shí)現(xiàn)進(jìn)程間數(shù)據(jù)交換的兩種方法和應(yīng)用
實(shí)現(xiàn)進(jìn)程間數(shù)據(jù)交換的兩種方法和應(yīng)用
2016-03-05
收藏

實(shí)現(xiàn)進(jìn)程間數(shù)據(jù)交換的兩種方法和應(yīng)用

Windows操作系統(tǒng)是一個(gè)多任務(wù)系統(tǒng),每個(gè)任務(wù)都有相應(yīng)的進(jìn)程對應(yīng)。熟悉windows系統(tǒng)的用戶知道,每個(gè)進(jìn)程都有自己獨(dú)立的內(nèi)存地址和內(nèi)存空間。這對進(jìn)程間之間的數(shù)據(jù)相互訪問和相互交換帶來一定的不便,但是在實(shí)際應(yīng)用中有時(shí)要在進(jìn)程間進(jìn)行數(shù)據(jù)交換。windows系統(tǒng)提供了許多方法實(shí)現(xiàn)進(jìn)程間的數(shù)據(jù)交換,比如我們可以通過磁盤文件來進(jìn)行交換,這實(shí)際是最簡單可行的辦法,但運(yùn)行速度大打折扣,特別是對于大批量數(shù)據(jù)交換時(shí)。另外我們可以通過Windows系統(tǒng)提供的CreateFileMaping、OpenFileMapping、MapViewofFile等API函數(shù)來實(shí)現(xiàn)進(jìn)程間的數(shù)據(jù)交換,但實(shí)現(xiàn)方法相對復(fù)雜。 本文將介紹另外兩種進(jìn)程間數(shù)據(jù)交換的方法,一種是通過剪貼板的進(jìn)程數(shù)據(jù)交換方法,另一種是直接內(nèi)存讀取的進(jìn)程數(shù)據(jù)交換方法。最后將用Delphi語言舉例加以實(shí)現(xiàn)應(yīng)用。

 一、通過剪貼板的進(jìn)程數(shù)據(jù)交換方法 剪貼板是windows系統(tǒng)專門提供用來不同應(yīng)用程序之間的數(shù)據(jù)交換,用戶只要通過“復(fù)制”和“粘帖”兩個(gè)動(dòng)作就可以實(shí)現(xiàn)應(yīng)用程序之間的數(shù)據(jù)交換。用戶可以自定義剪貼板格式實(shí)現(xiàn)數(shù)據(jù)交換的目的。 其方法步驟為: 1、數(shù)據(jù)發(fā)送進(jìn)程向剪貼板“復(fù)制”用戶自定義的數(shù)據(jù)內(nèi)容 2、數(shù)據(jù)發(fā)送進(jìn)程向數(shù)據(jù)接受進(jìn)程發(fā)送數(shù)據(jù)交換的消息。 3、數(shù)據(jù)接受進(jìn)程收到消息以后向剪貼板“粘貼”用戶自定義的數(shù)據(jù)內(nèi)容。 這種方法的實(shí)現(xiàn)過程如圖一所示

該方法涉及到的重要API函數(shù)主要有: 1、RegisterClipboardFormat 這個(gè)函數(shù)是向系統(tǒng)剪貼板注冊新的剪貼板數(shù)據(jù)格式其函數(shù)調(diào)用格式為:

UINT RegisterClipboardFormat(
    LPCTSTR lpszFormat 	  // 新剪貼板數(shù)據(jù)格式的名稱
   );
如果該名稱的剪貼板格式已經(jīng)注冊函數(shù)返回已經(jīng)的標(biāo)示號,否則函數(shù)返回一個(gè)新的標(biāo)示號。 2、GlobalAlloc、GlobalLock 這兩個(gè)函數(shù)前者是用于從當(dāng)前進(jìn)程的內(nèi)存空間分配一定字節(jié)數(shù)的內(nèi)存,后者是對指定的內(nèi)存加鎖。它們的函數(shù)調(diào)用格式分別為:
HGLOBAL GlobalAlloc(
    UINT uFlags,  	//分配到的內(nèi)存屬性
    DWORD dwBytes 	// 需要內(nèi)存分配的空間大小
   );
LPVOID GlobalLock(
    HGLOBAL hMem  	// 需要加鎖的內(nèi)存“句柄”
   );
3、Clipboard的Open、SetAsHandle、Close、GetAsHandle 這四個(gè)函數(shù)是Delphi語言Clipboard全局變量提供專門用于對剪貼板數(shù)據(jù)“復(fù)制”和“粘帖”動(dòng)作所要用到的函數(shù)。在Clipbrd.pas單元中實(shí)現(xiàn)。其函數(shù)格式略。 二、直接內(nèi)存讀取的進(jìn)程數(shù)據(jù)交換方法 這種方法是通過Windows系統(tǒng)一系列API函數(shù)調(diào)用直接讀取另一個(gè)進(jìn)程的內(nèi)存,來實(shí)現(xiàn)數(shù)據(jù)交換的目的。 其方法步驟為: 1、數(shù)據(jù)發(fā)送方進(jìn)程數(shù)據(jù)接受方進(jìn)程發(fā)送數(shù)據(jù)交換的消息,Wparam參數(shù)值為數(shù)據(jù)發(fā)送方進(jìn)程標(biāo)示號,Lparam參數(shù)值為要發(fā)送的數(shù)據(jù)的地址。 2、數(shù)據(jù)接受方進(jìn)程收到消息以后直接讀取數(shù)據(jù)發(fā)送方進(jìn)程內(nèi)存地址的數(shù)據(jù)。 這種方法的實(shí)現(xiàn)過程如圖二所示:

該方法涉及的重要API函數(shù)主要有: 1、GetCurrentProcessId 這個(gè)函數(shù)主要獲取當(dāng)前運(yùn)行進(jìn)程的進(jìn)程標(biāo)示號,其函數(shù)格式為:

DWORD GetCurrentProcessId(VOID)
2、openProcess 這個(gè)函數(shù)主要獲得一個(gè)已經(jīng)運(yùn)行的進(jìn)程的“進(jìn)程句柄”,以便在另外的API函數(shù)調(diào)用。其函數(shù)格式為:
HANDLE OpenProcess(
    DWORD dwDesiredAccess,	// 要打開的進(jìn)程的訪問模式
    BOOL bInheritHandle,	// 該進(jìn)程句柄是否在子線程中繼承
    DWORD dwProcessId 	// 進(jìn)程的標(biāo)示號
   );
3、ReadProcessMemory 這個(gè)函數(shù)是直接內(nèi)存讀取的進(jìn)程數(shù)據(jù)交換方法最核心的一個(gè)函數(shù),它實(shí)現(xiàn)從另一個(gè)進(jìn)程中讀取內(nèi)存數(shù)據(jù)。其函數(shù)格式為:
BOOL ReadProcessMemory(
    HANDLE hProcess,  	    //進(jìn)程句柄
    LPCVOID lpBaseAddress,	// 要讀取內(nèi)存的起始地址
    LPVOID lpBuffer,	       //存放讀取的數(shù)據(jù)的變量
    DWORD nSize,	           //需要讀取的字節(jié)數(shù)
    LPDWORD lpNumberOfBytesRead 	// 實(shí)際讀取到的字節(jié)數(shù)
   );
三、兩種進(jìn)程數(shù)據(jù)交換方法的應(yīng)用舉例 為了更加詳細(xì)地介紹上述兩種方法,下面利用Delphi語言結(jié)合實(shí)際的例子來闡述它的應(yīng)用。該例子分別用通過剪貼板的進(jìn)程數(shù)據(jù)交換的方法和直接內(nèi)存讀取的進(jìn)程數(shù)據(jù)交換方法實(shí)現(xiàn)兩個(gè)進(jìn)程間的交換一個(gè)記錄數(shù)據(jù),該記錄的數(shù)據(jù)格式為:
TFriend=packed record
     name    :array[1..8] of char;
     age     :byte;
     address :array[1..30] of char;
  end;
另外定義兩條消息和幾個(gè)需要用到的常量:
1.WM_Datasend_byClpbrd=WM_user+1000;  //用于通過剪貼板的進(jìn)程數(shù)據(jù)交換的方法的消息;
2.WM_DataSend_byMemory=WM_user+1001;// 直接內(nèi)存讀取的進(jìn)程數(shù)據(jù)交換方法的消息;
3.receivewindowcaption='DataReceiver';     //接收數(shù)據(jù)進(jìn)程窗口的標(biāo)題;
4.receivewindowclassname='Data Receive Window';//接受數(shù)據(jù)進(jìn)程窗口的類別名稱;
5.userDefineformat='process data exchange'; //自定義剪貼板數(shù)據(jù)格式名稱;
以上常量和記錄結(jié)構(gòu)是發(fā)送進(jìn)程和接受進(jìn)程的兩個(gè)程序都要用到的數(shù)據(jù)??梢詥为?dú)存放在一個(gè)文件中用uses引用,或用{$I }的方法引用。 (一)數(shù)據(jù)發(fā)送進(jìn)程程序的實(shí)現(xiàn) 1、在Delphi的IDE環(huán)境中新建Application 2、在Form1中加入一個(gè)Tspinedit、兩個(gè)Tedit和兩個(gè)Tbutton控件(分別為Button1和button2),兩個(gè)Edit控件分別供輸入姓名和地址,SpinEdit控件供輸入年齡。 3、重載Button1的onClick程序用于剪貼板數(shù)據(jù)交換,如下所示:
procedure TForm1.Button1Click(Sender: TObject);
var
   wnd     :Hwnd;
   Friend  :TFriend;
   hmem    :Thandle;
   datapointer :pointer;
   CF_User  :Uint;
begin
   //尋找數(shù)據(jù)接受進(jìn)程的窗口
   wnd:=findwindow(receivewindowclassname,receivewindowcaption);
   //如果找到了向剪貼板復(fù)制數(shù)據(jù)并且向數(shù)據(jù)接受進(jìn)程發(fā)送消息
   if wnd<>0 then  
begin
   //申請內(nèi)存
         hmem:=globalAlloc(GMEM_MOVEABLE,sizeof(friend));
         //給記錄賦值
         strpcopy(@friend.name,edit1.text);
         friend.age:=byte(spinedit1.Value);
         strpcopy(@friend.address,edit2.text);
         //獲取申請到的內(nèi)存地址并且把記錄的值導(dǎo)入內(nèi)存
         DataPointer := GlobalLock(hmem);
         //把記錄數(shù)據(jù)復(fù)制到Global內(nèi)存中
         move(friend,datapointer^,sizeof(friend));
         //注冊或獲取新的剪貼板數(shù)據(jù)格式標(biāo)號
         CF_User:=registerclipboardformat(userdefineformat);
         //向剪貼板復(fù)制數(shù)據(jù)
         clipboard.Open;
         clipboard.SetAsHandle(CF_USER,hmem);
         clipboard.Close;
         //向接收進(jìn)程窗口發(fā)送消息
         sendmessage(wnd,WM_Datasend_byClpbrd,0,0);
      end;
end;
4、重載button2的OnClick程序用于直接內(nèi)存讀取的數(shù)據(jù)交換,如下所示:
procedure TForm1.Button2Click(Sender: TObject);
var
   wnd       :Hwnd;
   Friend    :TFriend;
Begin
   //尋找數(shù)據(jù)接受進(jìn)程的窗口
   wnd:=findwindow(receivewindowclassname,receivewindowcaption);
   //如果找到了向數(shù)據(jù)接收進(jìn)程窗口發(fā)送消息
   if wnd<>0 then
begin
   //對記錄賦值
         strpcopy(@friend.name,edit1.text);
         friend.age:=byte(spinedit1.Value);
         strpcopy(@friend.address,edit2.text);
         //發(fā)送消息,把發(fā)送進(jìn)程的進(jìn)程標(biāo)示號作為Wparam
         //把記錄地址作為Lparam進(jìn)行傳遞
       sendmessage(wnd,WM_DataSend_byMemory,
getcurrentprocessID,lparam(@Friend));
      end;
end;
5、保存、編譯、運(yùn)行 (二)數(shù)據(jù)接受進(jìn)程程序的實(shí)現(xiàn) 1、在Delphi的IDE環(huán)境新建Application; 2、在Form1中加入三個(gè)Tedit控件,接受發(fā)送過來的數(shù)據(jù); 3、重載Form1的CreateParams程序,重新定義Form1的類,程序如下:
procedure TForm1.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      params.Caption:=receivewindowcaption;
      Params.WinClassName :=receivewindowclassname;
    end;
4、重載在Form1的wndproc程序接受兩種方法發(fā)送過來的數(shù)據(jù),程序如下:
procedure Tform1.wndproc(var message:Tmessage);
   var
    processhnd   :Thandle;
      Friend       :TFriend;
numread     :dword;
hmem       :Thandle;
clipdata      :pointer;
CF_User     :Uint;
        Begin
           Case message.msg of
               //處理剪貼板方式發(fā)送過來的消息
WM_Datasend_byClpbrd:
   Begin
     //注冊或獲取新的剪貼板數(shù)據(jù)格式標(biāo)號
     CF_User:= registerclipboardformat(userdefineformat);
     if clipboard.HasFormat(CF_User) then
        begin
           //獲取剪貼板的內(nèi)容,并且分別賦值給三個(gè)Edit
           hmem:=clipboard.GetAsHandle(CF_user);
           clipdata:=globallock(hmem);
           if clipdata<>nil then
              begin
                 move(clipdata^,Friend,sizeof(Friend));
                 edit1.text:=Friend.name;
                 edit2.text:=inttostr(Friend.age);
                 edit3.text:=Friend.address;
              end;
        end;
       message.Result:=1;
   End;
//處理直接內(nèi)存讀取發(fā)送過來的消息
               WM_DataSend_byMemory:
                  Begin
                     //獲取發(fā)送進(jìn)程的“進(jìn)程句柄”,設(shè)置訪問模式為“讀”
                     processhnd:=openprocess(PROCESS_VM_READ,
false,message.WParam);
                     //讀取發(fā)送進(jìn)程的內(nèi)存數(shù)據(jù)
  readprocessmemory(processhnd,
ptr(message.lparam),
@friend,sizeof(friend),numread);
edit1.text:=Friend.name;
    edit2.text:=inttostr(Friend.age);
    edit3.text:=Friend.address;
     closehandle(processhnd);
     message.result:=1;
                  End;
Else
   //對于其它消息依然按照原來的方法進(jìn)行消息處理
Inherited wndproc(message);
        End;
5、保存、編譯、運(yùn)行 到這里為止已經(jīng)實(shí)現(xiàn)上述進(jìn)程間記錄數(shù)據(jù)的交換的兩種方法。運(yùn)行結(jié)果如圖三所示:

圖三以上程序僅僅是上面介紹的兩種方法最簡單應(yīng)用,用戶可以根據(jù)自己的實(shí)際需要加以拓展應(yīng)用。上述程序在Windows 2000+Delphi 5.0的環(huán)境下運(yùn)行通過

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }