• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            road420

            導(dǎo)航

            <2008年9月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            統(tǒng)計

            常用鏈接

            留言簿(2)

            隨筆檔案

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            dll #pragma data_seg 實現(xiàn)數(shù)據(jù)的共享

            #pragma data_seg("flag_data")
               int count=0;
            #pragma data_seg()
            #pragma comment(linker,"/SECTION:flag_data,RWS")
            這種方法只能在沒有def文件時使用,如果通過def文件進行導(dǎo)出的話,那么設(shè)置就要在def文件內(nèi)設(shè)置而不能
            在代碼里設(shè)置了。
            SETCTIONS
            flag_data READ WRITE SHARED

             

            在主文件中,用#pragma data_seg建立一

            個新的數(shù)據(jù)段并定義共享數(shù)據(jù),其具體格式為:

            #pragma data_seg ("shareddata") //名稱可以

            //自己定義,但必須與下面的一致。

            HWND sharedwnd=NULL;//共享數(shù)據(jù)

            #pragma data_seg()



            僅定義一個數(shù)據(jù)段還不能達到共享數(shù)據(jù)的目的,還要告訴編譯器該段的屬性,有兩種方法可以實現(xiàn)該目的 (其效果是相同的),一種方法是在.DEF文件中加入如下語句: SETCTIONS shareddata READ WRITE SHARED 另一種方法是在項目設(shè)置鏈接選項(Project Setting --〉Link)中加入如下語句: /SECTION:shareddata,rws

            第一點:什么是共享數(shù)據(jù)段?為什么要用共享數(shù)據(jù)段??它有什么用途??
            在Win16環(huán)境中,DLL的全局數(shù)據(jù)對每個載入它的進程來說都是相同的;而在Win32環(huán)境中,情況卻發(fā)生了變化,DLL函數(shù)中的代碼所創(chuàng)建的任何對象(包括變量)都歸調(diào)用它的線程或進程所有。當進程在載入DLL時,操作系統(tǒng)自動把DLL地址映射到該進程的私有空間,也就是進程的虛擬地址空間,而且也復(fù)制該DLL的全局數(shù)據(jù)的一份拷貝到該進程空間。也就是說每個進程所擁有的相同的DLL的全局數(shù)據(jù),它們的名稱相同,但其值卻并不一定是相同的,而且是互不干涉的。

            因此,在Win32環(huán)境下要想在多個進程中共享數(shù)據(jù),就必須進行必要的設(shè)置。在訪問同一個Dll的各進程之間共享存儲器是通過存儲器映射文件技術(shù)實現(xiàn)的。也可以把這些需要共享的數(shù)據(jù)分離出來,放置在一個獨立的數(shù)據(jù)段里,并把該段的屬性設(shè)置為共享。必須給這些變量賦初值,否則編譯器會把沒有賦初始值的變量放在一個叫未被初始化的數(shù)據(jù)段中。



            #pragma data_seg預(yù)處理指令用于設(shè)置共享數(shù)據(jù)段。例如:

            #pragma data_seg("SharedDataName") HHOOK hHook=NULL; //必須在定義的同時進行初始化!!!!#pragma data_seg()

            在#pragma data_seg("SharedDataName")和#pragma data_seg()之間的所有變量將被訪問該Dll的所有進程看到和共享。再加上一條指令#pragma comment(linker,"/section:.SharedDataName,rws"),[注意:數(shù)據(jù)節(jié)的名稱is case sensitive]那么這個數(shù)據(jù)節(jié)中的數(shù)據(jù)可以在所有DLL的實例之間共享。所有對這些數(shù)據(jù)的操作都針對同一個實例的,而不是在每個進程的地址空間中都有一份。



            當進程隱式或顯式調(diào)用一個動態(tài)庫里的函數(shù)時,系統(tǒng)都要把這個動態(tài)庫映射到這個進程的虛擬地址空間里(以下簡稱"地址空間")。這使得DLL成為進程的一部分,以這個進程的身份執(zhí)行,使用這個進程的堆棧。(這項技術(shù)又叫code Injection技術(shù),被廣泛地應(yīng)用在了病毒、黑客領(lǐng)域!呵呵^_^)



            第二點:在具體使用共享數(shù)據(jù)段時需要注意的一些問題!

            Win32 DLLs are mapped into the address space of the calling process. By default, each process using a DLL has its own instance of all the DLLs global and static variables. (注意: 即使是全局變量和靜態(tài)變量也都不是共享的!) If your DLL needs to share data with other instances of it loaded by other applications, you can use either of the following approaches:

            · Create named data sections using the data_seg pragma.

            · Use memory mapped files. See the Win32 documentation about memory mapped files.

            Here is an example of using the data_seg pragma:

            #pragma data_seg (".myseg")
            int i = 0;
            char a[32] = "hello world";
            #pragma data_seg()

            data_seg can be used to create a new named section (.myseg in this example). The most typical usage is to call the data segment .shared for clarity. You then must specify the correct sharing attributes for this new named data section in your .def file or with the linker option /SECTION:.MYSEC,RWS. (這個編譯參數(shù)既可以使用pragma指令來指定,也可以在VC的IDE中指定!)

            There are restrictions to consider before using a shared data segment:

            · Any variables in a shared data segment must be statically initialized. In the above example, i is initialized to 0 and a is 32 characters initialized to hello world.

            · All shared variables are placed in the compiled DLL in the specified data segment. Very large arrays can result in very large DLLs. This is true of all initialized global variables.

            · Never store process-specific information in a shared data segment. Most Win32 data structures or values (such as HANDLEs) are really valid only within the context of a single process.

            · Each process gets its own address space. It is very important that pointers are never stored in a variable contained in a shared data segment. A pointer might be perfectly valid in one application but not in another.

            · It is possible that the DLL itself could get loaded at a different address in the virtual address spaces of each process. It is not safe to have pointers to functions in the DLL or to other shared variables.

            posted on 2010-09-16 10:25 深邃者 閱讀(426) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            一本一本久久A久久综合精品 | 亚洲国产精品成人久久蜜臀 | 日产精品久久久一区二区| 色综合久久久久综合99| 一本久久a久久精品亚洲| 久久国产热精品波多野结衣AV| 欧美亚洲国产精品久久蜜芽| 国产午夜精品久久久久九九电影 | 久久精品国产精品亚洲毛片| 日韩亚洲欧美久久久www综合网| 香蕉久久夜色精品国产2020| 日韩精品久久无码人妻中文字幕 | 国内精品久久久久影院老司| 久久青青草原精品国产| 久久久91人妻无码精品蜜桃HD| 久久精品青青草原伊人| 久久99精品国产麻豆不卡| 久久人人爽人人爽人人AV东京热| 久久天天躁狠狠躁夜夜2020 | 久久99热这里只频精品6| 久久99国内精品自在现线| 伊人久久亚洲综合影院| 国产亚州精品女人久久久久久| 久久婷婷五月综合色奶水99啪| 欧美亚洲日本久久精品| 欧美日韩中文字幕久久伊人| 新狼窝色AV性久久久久久| 2020久久精品亚洲热综合一本| 国产精品成人久久久久久久| 精品久久777| 国产精品久久久久久福利69堂| 亚洲国产欧洲综合997久久| 伊人 久久 精品| 亚洲人成无码网站久久99热国产| 国产精品激情综合久久| 99久久免费只有精品国产| 久久久中文字幕| 91久久精品电影| 国产精品无码久久综合网| 久久精品18| 久久久无码精品亚洲日韩京东传媒 |