dll # pragma data_seg data sharing

2010-10-18  来源:本站原创  分类:CPP  人气:347 

# Pragma data_seg ("flag_data")
int count = 0;
# Pragma data_seg ()
# Pragma comment (linker, "/ SECTION: flag_data, RWS")
This method can only be used when there is no def file, if the exported file by def, then set the necessary settings in the document can not be def in the code set.
SETCTIONS
flag_data READ WRITE SHARED

In the main file, use # pragma data_seg establish a

A new data segment and define the shared data, the specific format:

# Pragma data_seg ("shareddata") / / name can be

/ / Define their own, but must be consistent with the following.

HWND sharedwnd = NULL; / / shared data

# Pragma data_seg ()

Only the definition of a data segment can not achieve the purpose of sharing data, but also tells the compiler the properties of the paragraph, there are two ways to achieve that purpose (the effect is the same), a method is. DEF file, add the following statement: SETCTIONS shareddata READ WRITE SHARED Another method is to set the link option in the project (Project Setting -> Link) add the following statement: / SECTION: shareddata, rws

The first point: What is the shared data segment? Why use shared data segment? ? It is used for? ?
In the Win16 environment, DLL's global data for each of the process of loading it is the same; in Win32 environment, the situation has changed, DLL function in the code to create any objects (including variables) owned by calling thread or process it all. When the process of loading the DLL, the operating system automatically maps the DLL into the process address private space, that is, the process's virtual address space, but also copy the DLL's global data a copy to the process space. Means that each process has the same DLL's global data, they have the same name, but its value is not necessarily the same, and is non-interference in the.

Therefore, in the Win32 environment in order to share data across multiple processes, it must make the necessary settings. Dll in all access the same shared memory between processes is through memory-mapped file technology to achieve. Also need to share data in these isolated, placed in a separate paragraph in the data, and to the paragraph property to share. The initial value must be assigned to these variables, or the compiler will not assign initial values of variables in a called uninitialized data section.

# Pragma data_seg directive sets the shared data segment. For example:

# Pragma data_seg ("SharedDataName") HHOOK hHook = NULL; / / must be initialized while the definition of !!!!# pragma data_seg ()

In the # pragma data_seg ("SharedDataName") and # pragma data_seg () among all the variables will be access to all processes of the Dll to see and share. Coupled with a directive # pragma comment (linker, "/ section:. SharedDataName, rws"), [Note: The data section name is case sensitive] data section then the data can be shared among all instances of DLL. All of these data operations for the same instance instead of the address space of each process has a copy.

When the process is called implicit or explicit function of a dynamic library, the system should be mapped to the dynamic libraries to this process's virtual address space (hereinafter referred to as "address space"). This makes the DLL as part of the process to run as this process, the use of the process stack. (The technology known as code Injection technology, is widely used in the virus, hacker fields! Ha ha ^ _ ^)

The second point: to share data in a specific period of time using a number of issues need to pay attention!

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. (Note: Even if the global variables and static variables are not shared! ) 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. (This parameter can either use the compiler pragma directive to specify that the IDE can also be specified in the VC!)

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.

相关文章
  • dll # pragma data_seg data sharing 2010-10-18

    # Pragma data_seg ("flag_data") int count = 0; # Pragma data_seg () # Pragma comment (linker, "/ SECTION: flag_data, RWS") This method can only be used when there is no def file, if the exported file by def, then set the necessary sett

  • DLL shared data segment, the process of data sharing for multi Summary 2010-02-02

    DLL shared data segment, the process of data sharing for multi Summary Some time ago has been realized with the WM_COPYDATA message the process of data communication, the software upgrades would like to learn about, DLL shared application data segmen

  • Data sharing between applications Android-ContentResolver 2010-08-13

    Android is how to implement data sharing between applications? An application can be fully exposed to their own data, even the outside world do not see this application has not Kandao exposed how the data is stored, or use the database or use a file,

  • [Transfer] PHP SESSION multi-server data sharing 2010-12-14

    First, the problem of origin of some of the larger sites, they usually have several servers, each server runs a different function modules, using a different second level domain, and a strong overall site, the user system is unified, that is Apply us

  • Multi-domain THINKPHP use MEMCACHE way data sharing SESSION 2010-02-09

    First, the problem of origin Slightly larger for Wang Zhan, there are often several servers, each server running different functions of the modules, using a different second level domain, and a holistic Jiang's Web site, user system is The consolidat

  • Multi server data sharing SESSION 2010-06-08

    Reprinted First, the problem of origin Large sites often have multiple servers and multiple secondary domain name. This session generated a server can not be shared by all servers. So that users can not log in one place all the passage (hereinafter f

  • The following are installed with the visualbox win7 xp, and data sharing 2010-10-07

    Although online tutorials have been more, but still make their own about it. . . First-time installation guide, doing a good job, please forgive me. 1. Preparation: First google about visualbox, and then to the official website to download a visualbo

  • C # call DLL C + +, all data collected more types of conversion 2010-12-16

    Collecting the code that this would be a very good article, spent a day, the search to the last surprise from an article called "C # and C + + data types table," the article. Almost won out, and most of the data, so blow me. This is not part of

  • data sharing under virtualbox ubuntu 2011-06-09

    Environment: Host ubuntu11.04 virtualbox 4.0.8 Client windows xp One: the establishment of shared hosts file data space 1 client install virtualbox enhancements 2. Virtualbox data for the client to create shared space ("space allocation data") 3

  • vbox windows and ubuntu server to achieve data sharing between the [pure command line] 2011-07-12

    windows machine, install the vbox, and installed ubuntu server in vbox Start ubuntu server Assigned a temporary data space, write down the name of the distribution of drive space allocated VBoxGuestAdditions.iso Log uname -r sudo passwd root su - apt

  • VirtualBox virtual machine xp set up data sharing with the host ubuntu10 2010-07-04

    1, through the Ubuntu installation VirtualBOX software center 2, install xp. (Recommended xp install version, ghost version of the installation will have many problems) 3, installed in a virtual machine interface enhancements 4, to install virtualbox

  • spc integrated data sharing 2011-05-10

    Based bizt, integrated supply chain financial services, supply chain financial services platform designed to share the user.

  • shared dll section of some of the issues requiring attention 2010-02-02

    shared dll section of some of the issues requiring attention Visual C + + How to share applications or other DLL DLL in your data? Win32 DLL is mapped to the calling process's address space. By default, each process using the DLL all the DLL has its

  • #Pragma 用法总结 2012-06-12

    百科名片 http://baike.baidu.com/view/1451188.htm 在所有的预处理指令中,#Pragma 指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或操作系统专有的特征.依据定义,编译指示是机器或操作系统专有的,且对于每个编译器都是不同的. 1.message 参数 Message 参数能够在编译信息输出窗口中输出相应的信息,这对于 源代码

  • [转]预编译指令#pragma的用法 2012-11-28

    [转]预编译指令#pragma的用法 在所有的预处理指令中,#Pragma 指令可能是最复杂的了,它的作用是设定 编译器 的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或 操作系统 专有的特征.依据定义,编译指示是机器或操作系统专有的,且对于每个编译器都是不同的. 其格式一般为: #pragma Para.其中Para 为参数,下面来看一些常用的参数 1.message 参数 Message 参数能够在编译

  • pragma comment(linker,"/SECTION:shared,RWS") 2012-06-26

    pragma comment(linker,"/SECTION:shared,RWS") Windows在一个Win32程序的地址空间周围筑了一道墙.通常,一个程序的地址空间中的数据是私有的,对别的程序而言是不可见的.但是执行多个执行实体表示了程序的所有执行实体之间共享数据是毫无问题的.当您在一个窗口中增加或者删除一个字符串时,这种改变将立即反映在其它的窗口中. 在全部例程之间,STRLIB共享两个变量:一个字符数组和一个整数(记录已储存的有效字符串的个数).这两个变量储存在共享的一个

  • #pragma用法详解 2014-07-29

    Author :Jeffrey My Blog:http://blog.csdn.net/gueter/ 目录: (0) 前言 (1) #pragma message能够在编译信息输出窗口中输出相应的信息 (2) #pragma code_seg能够设置程序中函数代码存放的代码段,开发驱动程序的时会用到 (3) #pragma once若用在头文件的最开始处就能够保证头文件被编译一次 (4) #pragma hdrstop表示预编译头文件到此为止 (5) #pragma resource "*.

  • 2010.03.12 - large data export excel the traditional flow method io 2009-11-04

    2010.03.12 - large data export excel the traditional method of io stream, Changping made yesterday to demand a written log no boss today so I have studied the issue of large data export excel first, check a lot of information saying that there will b

  • Silverlight data binding (Binding) 2010-02-09

    http://blog.csdn.net/banmuhuangci/archive/2009/05/08/4160092.aspx Developed using Silverlight when the operation will feel the data is very simple, whether or Webclient with WCF is a communication server, Silverlight data processing and display are v

  • <Programming Pearls> binary search in the use of large amounts of data (not find a file in the data) 2010-03-18

    "Programming Pearls" problem mentioned in Chapter II A: Given a 32-bit integer that contains the order file, which contains up to four billion such integer, and the sequence is random. Please find this file does not exist a 32-bit integer. Of co