Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

Usar un TdataModule compartido entre un ejecutable y una dll

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Consultas
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: Usar un TdataModule compartido entre un ejecutable y una dll Responder citando

Tengo un problema.

Tengo un TDatamodule que lo usa un .exe . Quiero que determinadas operaciones estén en una DLL, así como otros formularios, recursos, etc.

¿Cómo puedo usar el mismo TDatamodule (por facilidad de diseño) en el exe y la dll?

Si, por ejemplo, el TDatamodule se llama db, al ejecutar me dice: El objeto db ya existe en la aplicación (al llamar a la dll, claro).

He probado de mil maneras, pero o no es accesible (porque hay que crearlo y, con lo cual, hay dos objetos con el mismo nombre) o sin mas no es accesible.

La idea es tener un TDatamodule entre el exe y la dll para poder usar las bases de datos bien desde un fmain o desde los form de las DLL tanto en tiempo de diseño como en runtime.

Gracias a todos por vuestra ayuda.
_________________
Un saludo
Rayder
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Haz uso de paquetes(BPL) en vez de DLL

Revisa estos ejemplos ( en C++Builder y Delphi):

http://www.obsof.com/delphi_tips/DL613.html
http://www.mdata.gr/MDataNews/DeveloperTips/advanceddynamicpackages.htm
http://bdn.borland.com/article/0,1410,27178,00.html

Cuando hagas tus paquetes asegurate de agregar la clausula PACKAGE a las clases y/o funciones que se deben exportar
Código:
class PACKAGE TMiModulo:public TDataModule ....

class PACKAGE TMiVentana:publict TForm ...

void PACKAGE MiProceso(...)
Laughing
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Gracias por responder, cecito.
Voy a revisar los enlaces que me has puesto y ya te comentare.
_________________
Un saludo
Rayder
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Si haces un paquete y en este tienes un módulo de datos que deseas compartir con otros proyectos, debes definirlo así en su cabecera:
Código:
class PACKAGE TMiModulo;
class TMiModulo:public TDataModule ...

Si no agregas la primera línea y lo haces como te indique antes
Código:
class PACKAGE TMiModulo:public TDataModule
Se produce un error muy extraño, el cual consiste que despues de abrir varias veces tu modulo de datos este se transforma en un formulario.

Si revisas el libro "C++ Builder Developer's Guide 5" (formato pdf, no papel) en la página 895 explica como colocar el módulo de datos en un dll, pero no se puede compartir con el resto de tu proyectos como se podría hacer con los paquetes: Te copio el texto por si no tienes ese pdf:
Código:
How Do I Use a Data Module in My Applications, DLLs,
and Distributed Objects?
Data modules are just like forms in the sense that they are encapsulated in their own .h and
.cpp files, that they have a .dfm file that describes the components in the data module, and that
they can reference other forms or data modules. Data modules can be autocreated (see the list in
Project, Options, or edit the project source using Project, View Source), or can be created with
the new operator (as in SomeDataModule = new TSomeDataModule(Application)). A data
module also has an owner that takes responsibility for freeing it. This is typically the application,
in which case your data module will be automatically freed by the application when the
application terminates. It can also be a form (in which case the form automatically frees the data
module when the form is freed), a component (which will automatically free the data module
when it is freed), or even NULL (in which case you must make sure the data module is freed by
your own code, usually in a destructor, since there is no owner to perform that action automatically).
To use a data module with a form, you typically need only two steps:
• Make sure the data module will be created before any form that uses it (by altering the
order of creation in changing the project source or the autocreate list, if needed). Of
course, if the form is not autocreated, it can simply be created with new as needed, and
deleted as needed.
• Use File, Include Unit Hdr on the form that will use the data module to ensure that the
data module is #included in the form .cpp file.
Using a form with a DLL, COM, or CORBA object is actually just as simple.
If you are using a data module with a DLL and need the data module to remain open across
calls to the DLL for the duration of time the DLL is loaded, you can open the data module in
the DllEntryPoint(). The code in Listing 14.1 shows how this works.

Código:
#include <vcl.h>
#include <windows.h>
#pragma hdrstop


#include <Forms.hpp>
#include <TestDataModuleUnit.h>

#pragma argsused

String InternallyMaintainedResultString;

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
    // This is called on every process attach,
    // so make sure we need to initialize by checking the global variable...

    if (TestDataModule == NULL)
    {
        Application->Initialize(); // This is essential
        Application->CreateForm(__classid(TTestDataModule),&TestDataModule);
    };

    return 1;
}

char * __declspec(dllexport) TestDLLInterfaceFunction(void)
{
    TestDataModule->Query->First();
    InternallyMaintainedResultString =
        TestDataModule->Query->FieldByName("SUMMARY")->AsString;
    return InternallyMaintainedResultString.c_str();
}

Código:
Similar procedures can be followed for COM (the data module must be created in the
DllEntryPoint() function of the generated Active Server Library) and CORBA. In CORBA,
the server is itself a data module, and so it is very easy to use data module techniques with
CORBA.

El ejemplo es solo una guía de como se puede crear el módulo de datos en tu dll, pero debes limitarte a llamar a funciones para que te devuelvan los valodrs de los querys que estan en este.
Espero haberte ayudado Laughing
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Despues de estar estudiando los enlaces que me pusiste, ya he conseguido que funcione como deseaba.
De nuevo muchas gracias por tu ayuda cecito.
_________________
Un saludo
Rayder
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mostrar mensajes de anteriores:   
Cambiar a:  
No puedes publicar temas nuevos en este foro
No puedes responder a temas en este foro
No puedes editar tus mensajes en este foro
No puedes borrar tus mensajes en este foro
No puedes votar en las encuestas de este foro


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.