Helpful Information
 
 
Category: Delphi Programming
using event in DLL

HI!
I'm doing my work of thesis on a c++ project using C++ Builder 5.
My tutor has obliged me to do a thing (that I've had yet in a simpler way) using a DLL.
Here come the problems.
In fact in a common application like this:



__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Application->OnShortCut = Key;
}
void __fastcall TForm1::KeyEvent(TWMKey &Msg, bool &Handled)
{
if (Msg.CharCode == VK_RETURN)
{
ShowMessage("Enter disabled");
Handled = true;
}
}


, I'm able to use the command Application->OnShortCut. And the application does what required (the message is displayed).
Instead if I try to do a similar thing using a DLL, everything is harder (actually too hard for me).
In fact I think it's not possible to use a declaration like
void __fastcall TForm1::KeyEvent(TWMKey &Msg, bool &Handled)
in the body of the DLL, because of the fact that I cannot declare it anywhere (there isn't a header file to do this like in common applications).
So to avoid this problem I decided to add to my DLL a fictitious Form (Form2) where I declared the event KeyEvent: in the main body of the DLL I used the command Application->OnShortCut=Form2->KeyEvent.
According to me, everything should work, but it seems that the pushing of the key is not recognized. But I don't know why.




//---------------------------------------------------------------------------
/* Unit1.cpp ----it's the body of the DLL*/
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
#include "Unit2.h"
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
Form2 = new TForm2(NULL);
Application->OnShortCut = Form2->KeyEvent;
return 1;
}
//---------------------------------------------------------------------------
extern "C"
{
bool __export GetEvent()
{
bool ret = false;
if (come == true)
{
ret=true;
}
return ret;
}
}


/* Unit2.h ------ it's the header file of Form2 */


//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------

class TForm2 : public TForm
{
__published: // IDE-managed Components
private: // User declarations
public: // User declarations
__fastcall TForm2(TComponent* Owner);
void __fastcall TForm2::KeyEvent(Messages::TWMKey &Msg, bool &Handled);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
extern bool come;
//---------------------------------------------------------------------------
#endif


/* Unit2.cpp */


//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
bool come;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::KeyEvent(Messages::TWMKey &Msg, bool &Handled)
{
if (Msg.CharCode==VK_RETURN)
come=true;
else
come=false;
}




This simple application should return true if the RETURN key has been pushed, false in the other cases.
Any ideas?
Thanx

Try showing your form?


int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
Form2 = new TForm2(NULL);
Application->OnShortCut = Form2->KeyEvent;
Form2->Show();
return 1;
}

The problem wasn't due to the showing of the form but to the unchecked option "Build with runtime packages" in Projects|Options|Packages.
Thanx 4 your help!










privacy (GDPR)