DLL和非模式形式 [英] DLL and non modal form

查看:33
本文介绍了DLL和非模式形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用dll项目中的File-> New-> Form将Form1添加到dll项目中.
dll项目

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();
}




主cpp




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);
}


但是我仍然不知道如何在没有系统错误的情况下关闭窗口.当窗口创建modalessly主应用程序免费库时,出现访问冲突.如何在Form1中的onCloseQuery事件或onClose事件上释放库?


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?

推荐答案

在您的DLL函数中,返回一个指向表单基类的指针,然后使用它来关闭窗口,然后再卸载库:

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

extern "C" __declspec(dllexport) TForm* Funkcja()
{
  TForm1* newform = new TForm1(Form1);
  newform->Show();
  return newform;
}


谢谢.但是,您说的是卸载库之前先关闭窗口"是什么意思?在主应用程序或DLL中关闭?函数返回指向主应用程序的指针,所以我必须以哪种方式进行操作?我必须发送一个指向dll的指针吗?
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);
}





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.


这篇关于DLL和非模式形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆