Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I add Form1 to dll project using File->New->Form in dll project.
dll project
#include "Unit1.h"
extern "C" __declspec (dllexport) void Funkcja()
{
  TForm1* newform = new TForm1(Form1);
  newform->Show();
}



Main cpp
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  HINSTANCE DLLHandle = LoadLibrary(L"Projectmydll.dll");
 
   if(DLLHandle != NULL)
   {
      typedef (*aFunkcja)();
 
      aFunkcja Funkcja = (aFunkcja)GetProcAddress(DLLHandle, "_Funkcja");
 
      if( Funkcja != NULL) Funkcja();
      else ShowMessage( ""Lack of Funkcja" );
   }
   else ShowMessage( "Lack of dll." );
 
   FreeLibrary(DLLHandle);
}

But I still don`t know how to close the window without system error. While the window is creating modalessly main app free library and there is access violation. How to free library on onCloseQuery event or on onClose event in Form1? How to free library after close the window?
Posted

In your DLL function return a pointer to the form base class, then use it to close the window before unloading the library:

C++
extern "C" __declspec(dllexport) TForm* Funkcja()
{
  TForm1* newform = new TForm1(Form1);
  newform->Show();
  return newform;
}
 
Share this answer
 
Comments
Aescleal 23-Jun-10 16:41pm    
I'd loose the extern "C" as well - or at least pass the pointer out as some sort of typeless or anonymously typed handle.
Sauro Viti 23-Jun-10 16:49pm    
Ops! I absolutely agree with you!
Thanks. But what do you mean saying "close the window before unloading the library"? Close in main app or in DLL? Function return pointer to main app so in what way I have to do it? Do I have to send a pointer to dll?
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 HINSTANCE DLLHandle = LoadLibrary(L"Project1.dll");

 TForm* form;

 if(DLLHandle != NULL)
 {
  typedef TForm* (*aFunkcja)(TForm**);
  typedef void (*aClose)(TForm**);

  aFunkcja Funkcja = (aFunkcja)GetProcAddress(DLLHandle, "_Funkcja");
  aClose Close = (aClose)GetProcAddress(DLLHandle, "_Close");

  if( Funkcja != NULL ) form = Funkcja( &form );

  if ( !form ) return;

  Close( &form );
 }
 else ShowMessage( "Lack of dll." );

 FreeLibrary(DLLHandle);
}


C#
extern "C" __declspec(dllexport) TForm* Funkcja(TForm** form)
{
  *form = new TForm1(Form1);
  (*form)->Show();
  return *form;
}
void Close( TForm** form )
{
  //(*form)->Close();
  delete *form;
}

Form is creating non modal but while is visible there is access violation.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900