Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TValue as an event handler inside a dll


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Jul 3, 2015 6:13 AM Last Post By: Ahmed Sayed
Ahmed Sayed

Posts: 173
Registered: 8/9/07
TValue as an event handler inside a dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2015 7:50 AM
Hello everyone,

I am facing a big problem passing a TValue to dll function that after that iside the dll it is converted to an event handler.
Well, Here is what i am doing:
//Host Application
void __fastcall Login(UnicodeString User, UnicodeString Pass, bool &Accept)
{
//authenticate here
}
//Then i pass this event as a TValue to dll like this:
ExecuteForm(Handle, Params, TValue::From<TUserLogin>(Login));
 
//Dll 
 
void DLLEXPORT ShowDBForm(HWND CallingApp, TValue Value)
{
if (!ThisApp)
	{
	ThisApp = Application;
	Application->Handle = CallingApp;
	}
 
if (Value.IsEmpty())
    return;
 
if (Value.IsType<TUserLogin>())
      {
      TDllForm * Form - new TDllForm(Application);
      Form->OnLogin = Value.AsType<TUserLogin>();     // raise error on type cast
 
       }


I need to TValue so i can use this "ShowDBForm" with any form create and the TValue might hold something different every time a new dll is created. ( a multi purpose parameter ).

If any of you could help me that will be really great, and i appreciate it
Thanks in advance.
--
The limits of my language mean the limits of my world
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: TValue as an event handler inside a dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2015 3:04 PM   in response to: Ahmed Sayed in response to: Ahmed Sayed
Ahmed wrote:

I am facing a big problem passing a TValue to dll function that
after that iside the dll it is converted to an event handler.

TValue is not really a safe data type to pass across the DLL boundary. And
even if it were, your code would still fail because TUserLogin's RTTI inside
your hosting app is different than TUserLogin's RTTI inside the DLL (unless
you have compiled both host and DLL with Runtime Packages enabled) so TValue::IsType()
will return false instead of true.

ou really need to re-think your DLL design.

Well, Here is what i am doing:

Assuming TUserLogin is just a typedef for an event handler type, why not
pass it directly?

ShowDBForm(Handle, Login);


void DLLEXPORT ShowDBForm(HWND CallingApp, TUserLogin Value)
{
if (!ThisApp)
{
ThisApp = Application;
Application->Handle = CallingApp;
}
if (!Value)
return;

TDllForm *Form = new TDllForm(Application);
Form->OnLogin = Value;
//...
}
{code}

I need to TValue so i can use this "ShowDBForm" with any form create

You don't need TValue for that.

and the TValue might hold something different every time a new dll is
created. ( a multi purpose parameter ).

For purposes of a ShowDBForm() type of function, where it holds a pointer
to the event handler, it does not make sense to pass a TValue at all. Cn
youcome up with a better example to demonstrate why you need to use TValue
for multiple purposes?

--
Remy Lebeau (TeamB)
Ahmed Sayed

Posts: 173
Registered: 8/9/07
Re: TValue as an event handler inside a dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 3, 2015 6:13 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks Remy for your fast response,

As you know to call ShowDBForm function in the host application I must declare a typedef for this function like this:

typedef void (*__declspec(dllimport) __stdcall TShowDBForm)( HWND CallingApp, TValue Value);
Then call it like this,
if ((ShowDBForm = (TShowDBForm)GetProcAddress(DllLib,"ShowDBForm")) == NULL)
	{
	MessageDlg("Function not found!",
			   TMsgDlgType::mtError,
			   TMsgDlgButtons() << TMsgDlgBtn::mbOK, 0);
	return false;
	}


What I want to do is to use a single function typedef for all dll forms and then pass whatever data type in a TValue (sometimes TValue will hold a different event handler or an object).

So that’s why I am using it if I can do this with Variants then ok but I don’t know how to convert an event handler to variant and back.

Here is a thought can’t I create ‘ShowDBForm’ as a template function and instead TValue use <T> so it will look like this:

template <class T>
void DLLEXPORT ShowDBForm(HWND CallingApp, T Value)
{
//
}


If so how can I do it on both dll and hos application I think this will be much easier for me, and if that failed then what i need to do is this:

// In Dll
void DLLEXPORT ShowDBForm(HWND CallingApp, A data type that can be converted from and to anything)
{
//
}
//In host app
typedef void (*__declspec(dllimport) __stdcall TShowDBForm)( HWND CallingApp, A data type that can be converted from and to anything);


So whatever that data type will be i need to know which kind i should use (TValue, Variants, Template, etc).

Your help will be really appreciated.

--
The limits of my language mean the limits of my world
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02