variable procesos c++ c windows linux dll

c++ - variable - procesos en c



Compartir una variable global/estática entre un proceso y una DLL (8)

Me gustaría compartir una variable estática / global solo entre un proceso y un dll invocado por el proceso. El exe y dll están en el mismo espacio de direcciones de memoria. No quiero que la variable se comparta entre otros procesos.

Elaboración del problema:

Diga que hay una variable estática / global x en a.cpp . Tanto el exe foo.exe como el dll bar.dll tienen un a.cpp , por lo que la variable x está en ambas imágenes.

Ahora, foo.exe carga dinámicamente (o estáticamente) bar.dll . Entonces, el problema es si la variable x es compartida por el exe y dll, o no.

En Windows, estos dos tipos nunca comparten la x : el exe y dll tendrán una copia separada de x . Sin embargo, en Linux, el exe y dll comparten la variable x .

Desafortunadamente, quiero el comportamiento de Linux. Primero consideré usar pragma data_seg en Windows. Sin embargo, incluso si configuro correctamente el segmento de datos compartidos, foo.exe y bar.dll nunca comparten la x . Recuerde que bar.dll se carga en el espacio de direcciones de foo.exe . Sin embargo, si ejecuto otra instancia de foo.exe , entonces x se comparte. Pero, no quiero que x sea ​​compartido por diferentes procesos. Entonces, el uso de data_seg fue fallido.

Puedo usar un archivo mapeado en memoria haciendo un nombre único entre exe y dll, que estoy intentando ahora.

Dos preguntas:

  1. ¿Por qué el comportamiento de Linux y Windows es diferente? ¿Alguien puede explicar más sobre esto?
  2. ¿Cuál sería la forma más fácil de resolver este problema en Windows?

Gracias por proporcionar varias soluciones sobre esto. Miré estas opciones y decidí implementar el singleton de módulo cruzado usando la memoria compartida y también funcionó bien para mí. He utilizado Qt QSharedMemory para lograr mi tarea, pero el prototipo lo escribí usando Win32 CreateFileMapping, etc.


He visto muchas respuestas a esta pregunta y dado que es un poco complicado y poco claro, me gustaría presentar el siguiente escenario. Queremos compartir una variable global entre una DLL y un programa principal, y también permitir el acceso a esta variable desde diferentes módulos en la DLL y en el programa principal.

La variable es un BOOL que indica si el programa debe continuar ejecutándose o detenerse. El nombre de la variable es ShouldRun ;

En el programa principal, tenemos que poner:

__declspec(dllexport) bool ShouldRun;

En el módulo principal de la DLL tenemos que poner:

extern "C" BOOL __declspec(dllexport) ShouldRun = TRUE;

En cualquier otro módulo dentro del proyecto DLL usaremos:

extern "C" BOOL ShouldRun;


La diferencia entre GCC y Visual Studio es que en Linux, implícitamente permite que el código vea símbolos de otras bibliotecas dinámicamente vinculadas (compartidas), sin que el programador tenga que hacer nada especial. Todos los símbolos están disponibles en la biblioteca compartida (vinculada dinámicamente) para que el enlazador dinámico se resuelva cuando se ejecuta el programa. En Windows, debe exportar específicamente el símbolo de la DLL y también importarlo explícitamente en el programa o la biblioteca que lo está utilizando. (Normalmente esto se realiza a través de una macro (#define) que se expande para tener la declaración dllexport en un archivo de encabezado cuando se construye el dll, pero cuando el archivo de encabezado está incluido en algún otro programa que usa el dll, se expande para tener el dllimport declaración en su lugar. En mi opinión, este es un dolor en el cuello, y el comportamiento de GCC es más fácil, ya que no tienes que hacer nada especial para obtener el comportamiento que generalmente deseas.

En la versión más nueva de GCC, puede configurar el valor predeterminado para ocultar símbolos al compilar una biblioteca dinámica (compartida), si así lo desea.


Me pareció una pregunta tan interesante que me tomé el tiempo de escribir un extenso tutorial sobre cómo usar las DLL para compartir datos entre varias DLL (implícita o explícitamente) pero también me aseguro de que los datos no se compartan entre procesos separados del mismo archivo ejecutable.

Puede encontrar el artículo completo aquí: http://3dgep.com/?p=1759

Una solución a este problema que encontré que funciona bastante bien es crear un dll "común" o "compartido" que define todos los datos y métodos que desea compartir en múltiples DLL (pero no compartidos entre procesos).

Supongamos que desea definir una clase singleton a la que se pueda acceder desde el código de la aplicación principal (el EXE) pero también desea acceder a la instancia singleton en DLL compartido (ya sea implícita o explícitamente). Primero, necesitaría declarar la clase singleton en la DLL "común":

// Export the class when compiling the DLL, // otherwise import the class when using the DLL. class __declspec(dllexport) MySingleton { public: static MySingleton& Instance(); };

Al compilar el proyecto CommonDLL, debe exportar el declaratoin de clase decorando la clase con __declspec(dllexport) y cuando está usando el DLL (en la aplicación por ejemplo), la definición de clase debe ser importada decorando la clase con __declspec(dllimport) .

Al exportar una clase decorando la clase con el __declspec(dllexport) , todos los métodos y datos de la clase (incluso datos privados) se exportan de la DLL y se pueden utilizar mediante cualquier DLL o EXE que implícitamente se vincule con la DLL común.

La definición de la clase MySingleton podría verse más o menos así:

MySingleton& MySingleton::Instance() { static MySingleton instance; return instance; }

Al compilar el dll común, se generarán dos archivos:

  1. El archivo Common.DLL que es la biblioteca compartida que define los métodos y los datos exportados utilizados por el archivo DLL.
  2. El archivo Common.LIB que declara stubs para los métodos y miembros exportados de la DLL.

Si vincula su aplicación con el archivo LIB exportado, el archivo DLL se vinculará implícitamente en tiempo de ejecución (siempre que el archivo DLL se encuentre en las rutas de búsqueda de DLL) y tendrá acceso al singleton definido en CommonDLL.DLL archivo.

Además, cualquier biblioteca compartida (plug-ins, por ejemplo) que también se vincule con el archivo CommonDLL.LIB tendrá acceso a las mismas instancias singleton cuando la aplicación la cargue dinámicamente.

Para obtener una explicación completa de esta solución, incluida una muestra del código fuente, consulte el siguiente artículo que publiqué titulado "Uso de bibliotecas de vínculos dinámicos (DLL) para crear complementos":

http://3dgep.com/?p=1759


Para obtener el comportamiento de Linux donde tanto el programa principal como un dll comparten la misma x , puede exportar esa variable desde el dll o desde el programa principal. El otro módulo debe importar esa variable.

Para ello, utilice los archivos DEF ( consulte la documentación de Microsoft ) o marque los usos con la variable con __declspec(dllexport) donde está definida, y __declspec(dllimport) en cualquier otro módulo que se utilice ( consulte la documentación de microsoft ). Esto es lo mismo que cualquier función, objeto o variable que se comparte entre los módulos en Windows.

En el caso en que desee que un programa cargue una biblioteca en tiempo de ejecución, pero es posible que el programa principal tenga que usar la variable antes de que se cargue la biblioteca, el programa debería exportar la variable y el dll debería importarla. Aquí hay un pequeño problema de huevo y gallina porque el dll depende del programa principal y el programa principal depende del dll. Ver http://www.lurklurk.org/linkers/linkers.html#wincircular

He escrito un ejemplo de cómo puedes hacer esto usando tanto el compilador de Microsoft como mingw (gcc en Windows), incluidas todas las formas diferentes en que un programa y una biblioteca pueden enlazarse entre sí (estáticamente, dll cargado al inicio del programa, dll cargado durante el tiempo de ejecución)

main.h

#ifndef MAIN_H #define MAIN_H // something that includes this // would #include "linkage_importing.h" // or #include "linkage_exporting.h" // as appropriate #ifndef EXPLICIT_MAIN LINKAGE int x; #endif // EXPLICIT_MAIN #endif // MAIN_H

C Principal

#ifdef EXPLICIT_DLL #include "dyn_link.h" #endif // EXPLICIT_DLL #include <stdio.h> #include "linkage_exporting.h" #include "main.h" #include "linkage_importing.h" #include "dll.h" FNCALL_DLL get_call_dll(void); int main(int argc, char* argv[]) { FNCALL_DLL fncall_dll; fncall_dll = get_call_dll(); if (fncall_dll) { x = 42; printf("Address of x as seen from main() in main.c: %p/n", &x); printf("x is set to %i in main()/n", x); fncall_dll(); // could also be called as (*fncall_dll)(); // if you want to be explicit that fncall_dll is a function pointer printf("Value of x as seen from main() after call to call_dll(): %i/n", x); } return 0; } FNCALL_DLL get_call_dll(void) { #ifdef EXPLICIT_DLL return get_ptr("dll.dll", "call_dll"); #else return call_dll; #endif // EXPLICIT_DLL }

dll.h

#ifndef DLL_H #define DLL_H // something that includes this // would #include "linkage_importing.h" // or #include "linkage_exporting.h" // as appropriate // declaration of type to hold a // pointer to the function typedef void(*FNCALL_DLL)(void); #ifndef EXPLICIT_DLL LINKAGE void call_dll(void); #endif // EXPLICIT_DLL #endif // DLL_H

dll.c

#ifdef EXPLICIT_MAIN #include "dyn_link.h" #endif // EXPLICIT_MAIN #include <stdio.h> #include "linkage_importing.h" #include "main.h" #include "linkage_exporting.h" #include "dll.h" int* get_x_ptr(void); LINKAGE void call_dll(void) { int* x_ptr; x_ptr = get_x_ptr(); if (x_ptr) { printf("Address of x as seen from call_dll() in dll.c: %p/n", x_ptr); printf("Value of x as seen in call_dll: %i()/n", *x_ptr); *x_ptr = 31415; printf("x is set to %i in call_dll()/n", *x_ptr); } } int* get_x_ptr(void) { #ifdef EXPLICIT_MAIN return get_ptr("main.exe", "x"); // see note in dyn_link.c about using the main program as a library #else return &x; #endif //EXPLICIT_MAIN }

dyn_link.h

#ifndef DYN_LINK_H #define DYN_LINK_H // even though this function is used by both, we link it // into both main.exe and dll.dll as necessary. // It''s not shared in a dll, because it helps us load dlls :) void* get_ptr(const char* library, const char* object); #endif // DYN_LINK_H

dyn_link.c

#include "dyn_link.h" #include <windows.h> #include <stdio.h> void* get_ptr(const char* library, const char* object) { HINSTANCE hdll; FARPROC ptr; hdll = 0; ptr = 0; hdll = LoadLibrary(library); // in a better dynamic linking library, there would be a // function that would call FreeLibrary(hdll) to cleanup // // in the case where you want to load an object in the main // program, you can use // hdll = GetModuleHandle(NULL); // because there''s no need to call LoadLibrary on the // executable if you can get its handle by some other means. if (hdll) { printf("Loaded library %s/n", library); ptr = GetProcAddress(hdll, object); if (ptr) { printf("Found %s in %s/n", object, library); } else { printf("Could not find %s in %s/n", object, library); } } else { printf("Could not load library %s/n", library); } return ptr; }

linkage_importing.h

// sets up some macros to handle when to use "__declspec(dllexport)", // "__declspec(dllimport)", "extern", or nothing. // when using the LINKAGE macro (or including a header that does): // use "#include <linkage_importing.h>" to make the LINKAGE macro // do the right thing for importing (when using functions, // variables, etc...) // // use "#include <linkage_exporting.h>" to make the LINKAGE macro // do the right thing for exporting (when declaring functions, // variables, etc). // // You can include either file at any time to change the meaning of // LINKAGE. // if you declare NO_DLL these macros do not use __declspec(...), only // "extern" as appropriate #ifdef LINKAGE #undef LINKAGE #endif #ifdef NO_DLL #define LINKAGE extern #else #define LINKAGE extern __declspec(dllimport) #endif

linkage_exporting.h

// See linkage_importing.h to learn how this is used #ifdef LINKAGE #undef LINKAGE #endif #ifdef NO_DLL #define LINKAGE #else #define LINKAGE __declspec(dllexport) #endif

construir mingw explícito both.sh

#! /bin/bash echo Building configuration where both main echo and dll link explicitly to each other rm -rf mingw_explicit_both mkdir -p mingw_explicit_both/obj cd mingw_explicit_both/obj # compile the source code (dll created with position independent code) gcc -c -fPIC -DEXPLICIT_MAIN ../../dll.c gcc -c -DEXPLICIT_DLL ../../main.c gcc -c ../../dyn_link.c #create the dll from its object code the normal way gcc -shared -odll.dll dll.o dyn_link.o -Wl,--out-implib,libdll.a # create the executable gcc -o main.exe main.o dyn_link.o mv dll.dll .. mv main.exe .. cd ..

construir mingw explícita dll.sh

#! /bin/bash echo Building configuration where main explicitly echo links to dll, but dll implicitly links to main rm -rf mingw_explicit_dll mkdir -p mingw_explicit_dll/obj cd mingw_explicit_dll/obj # compile the source code (dll created with position independent code) gcc -c -fPIC ../../dll.c gcc -c -DEXPLICIT_DLL ../../main.c gcc -c ../../dyn_link.c # normally when linking a dll, you just use gcc # to create the dll and its linking library (--out-implib...) # But, this dll needs to import from main, and main''s linking library doesn''t exist yet # so we create the linking library for main.o # make sure that linking library knows to look for symbols in main.exe (the default would be a.out) gcc -omain.exe -shared main.o -Wl,--out-implib,main.a #note this reports failure, but it''s only a failure to create main.exe, not a failure to create main.a #create the dll from its object code the normal way (dll needs to know about main''s exports) gcc -shared -odll.dll dll.o dyn_link.o main.a -Wl,--out-implib,libdll.a # create the executable gcc -o main.exe main.o dyn_link.o mv dll.dll .. mv main.exe .. cd ..

construir mingw explícita main.sh

#! /bin/bash echo Building configuration where dll explicitly echo links to main, but main implicitly links to dll rm -rf mingw_explicit_main mkdir -p mingw_explicit_main/obj cd mingw_explicit_main/obj # compile the source code (dll created with position independent code) gcc -c -fPIC -DEXPLICIT_MAIN ../../dll.c gcc -c ../../main.c gcc -c ../../dyn_link.c # since the dll will link dynamically and explicitly with main, there is no need # to create a linking library for main, and the dll can be built the regular way gcc -shared -odll.dll dll.o dyn_link.o -Wl,--out-implib,libdll.a # create the executable (main still links with dll implicitly) gcc -o main.exe main.o -L. -ldll mv dll.dll .. mv main.exe .. cd ..

construir mingw implicit.sh

#! /bin/bash echo Building configuration where main and echo dll implicitly link to each other rm -rf mingw_implicit mkdir -p mingw_implicit/obj cd mingw_implicit/obj # compile the source code (dll created with position independent code) gcc -c -fPIC ../../dll.c gcc -c ../../main.c # normally when linking a dll, you just use gcc # to create the dll and its linking library (--out-implib...) # But, this dll needs to import from main, and main''s linking library doesn''t exist yet # so we create the linking library for main.o # make sure that linking library knows to look for symbols in main.exe (the default would be a.out) gcc -omain.exe -shared main.o -Wl,--out-implib,main.a #note this reports failure, but it''s only a failure to create main.exe, not a failure to create main.a # create the dll from its object code the normal way (dll needs to know about main''s exports) gcc -shared -odll.dll dll.o main.a -Wl,--out-implib,libdll.a # create the executable (exe needs to know about dll''s exports) gcc -o main.exe main.o -L. -ldll mv dll.dll .. mv main.exe .. cd ..

construir mingw static.sh

#! /bin/bash echo Building configuration where main and dll echo statically link to each other rm -rf mingw_static mkdir -p mingw_static/obj cd mingw_static/obj # compile the source code gcc -c -DNO_DLL ../../dll.c gcc -c -DNO_DLL ../../main.c # create the static library ar -rcs dll.a dll.o # link the executable gcc -o main.exe main.o dll.a mv main.exe ../ cd ..

compilar msvc explícita both.bat

@echo off echo Building configuration where both main echo and dll link explicitly to each other rd /s /q win_explicit_both md win_explicit_both/obj cd win_explicit_both/obj rem compile the source code cl /nologo /c /DEXPLICIT_MAIN ../../dll.c cl /nologo /c /DEXPLICIT_DLL ../../main.c cl /nologo /c ../../dyn_link.c rem create the dll from its object code the normal way link /nologo /dll dll.obj dyn_link.obj rem create the executable link /nologo main.obj dyn_link.obj move dll.dll ../ move main.exe ../ cd ..

compilar msvc explícita dll.bat

@echo off echo Building configuration where main explicitly echo links to dll, but dll implicitly links to main rd /s /q win_explicit_dll md win_explicit_dll/obj cd win_explicit_dll/obj rem compile the source code cl /nologo /c ../../dll.c cl /nologo /c /DEXPLICIT_DLL ../../main.c cl /nologo /c ../../dyn_link.c rem normally when linking a dll, you just use the link command rem that creates the dll and its linking library. rem But, this dll needs to import from main, and main''s linking library doesn''t exist yet rem so we create the linking library for main.obj rem make sure that linking library knows to look for symbols in main.exe (the default would be main.dll) lib /nologo /def /name:main.exe main.obj rem create the dll from its object code the normal way (dll needs to know about main''s exports) link /nologo /dll dll.obj main.lib rem create the executable link /nologo main.obj dyn_link.obj move dll.dll ../ move main.exe ../ cd ..

compilar msvc explícita main.bat

@echo off echo Building configuration where dll explicitly echo links to main, but main implicitly links to dll rd /s /q win_explicit_main md win_explicit_main/obj cd win_explicit_main/obj rem compile the source code cl /nologo /c /DEXPLICIT_MAIN ../../dll.c cl /nologo /c ../../main.c cl /nologo /c ../../dyn_link.c rem since the dll will link dynamically and explicitly with main, there is no need rem to create a linking library for main, and the dll can be built the regular way link /nologo /dll dll.obj dyn_link.obj rem create the executable (main still links with dll implicitly) link /nologo main.obj dll.lib move dll.dll ../ move main.exe ../ cd ..

construir msvc implicit.bat

@echo off echo Building configuration where main and echo dll implicitly link to each other rd /s /q win_implicit md win_implicit/obj cd win_implicit/obj rem compile the source code cl /nologo /c ../../dll.c cl /nologo /c ../../main.c rem normally when linking a dll, you just use the link command rem that creates the dll and its linking library. rem But, this dll needs to import from main, and main''s linking library doesn''t exist yet rem so we create the linking library for main.obj rem make sure that linking library knows to look for symbols in main.exe (the default would be main.dll) lib /nologo /def /name:main.exe main.obj rem create the dll from its object code the normal way (dll needs to know about main''s exports) link /nologo /dll dll.obj main.lib rem create the executable (exe needs to know about dll''s exports) link /nologo main.obj dll.lib move dll.dll ../ move main.exe ../ cd ..

construir msvc static.bat

@echo off echo Building configuration where main and dll echo statically link to each other rd /s /q win_static md win_static/obj cd win_static/obj rem compile the source code cl /nologo /DNO_DLL /c ../../dll.c cl /nologo /DNO_DLL /c ../../main.c rem create the static library lib /nologo dll.obj rem link the executable link /nologo main.obj dll.lib move main.exe ../ cd ..


Primero, encontré que este artículo era una lectura muy interesante y concisa de las bibliotecas de vínculos dinámicos (el artículo es solo específico de Linux, pero los conceptos también se aplican seguramente a Windows y es posible que tenga una idea del comportamiento diferente que tiene viendo). Especialmente la diferencia fundamental entre la carga estática y dinámica.

Creo que lo que quiere o está tratando de implementar es un patrón de "singleton de módulos cruzados". Si lees las respuestas a este hilo , no sé cómo podría responder tu pregunta mejor de lo que Ben Voigt respondió esa publicación. He implementado un singleton de módulos cruzados antes (algunas veces en realidad) usando el método que describe, y funciona como un amuleto.

Por supuesto, no podrá conservar la limpieza de tener la variable global en el archivo cpp. Deberá usar un puntero estático y algunas funciones de acceso y recuento de referencias. Pero puede funcionar No estoy tan seguro de cómo sería posible evitar que foo.exe y foo.exe compartan la misma instancia de datos globales de un bar.dll, nunca tuve que hacer eso y realmente no puedo pensar en una forma de hacerlo lo siento


Si entiendo correctamente su pregunta, está conectando estáticamente a.cpp con foo.exe y bar.dll, por lo que obtiene 2 instancias de x.

Si creó un tercer dll (por ejemplo, a.dll) y enlaza dinámicamente foo.exe y bar.dll con a.dll, obtendrá el comportamiento que desea:

  1. foo.exe carga a.dll y crea x.
  2. bar.dll se carga y ve que a.dll se carga y no lo carga de nuevo, comparten x.
  3. Otro proceso carga a.dll, obtiene su propia x.

Si foo.exe siempre carga bar.dll, entonces puede implementar la variable en bar.dll y exportarla. Por ejemplo, algunos archivos b.cpp compilados solo en bar.dll, no en foo.exe:

__declspec(dllexport) int x;

A continuación, impórtelo en un archivo fuente c.cpp compilado en foo.exe:

__declspec(dllimport) int x;

Sin embargo, si a veces foo.exe no carga bar.dll entonces esto no funcionará. Además, estoy escribiendo esto desde la memoria y puede haber algunos errores sintácticos, pero espero que sea suficiente para apuntar en la dirección correcta.

No puedo responder por qué es diferente Linux.