加急!!有一个很怪的事!!往高手们能够解答!!

lixiaolei 2002-03-29 10:54:10
大致如下:很简单的一个Dll中的函数

extern "C" __declspec(dllexport) int WINAPI play(void)
{
TForm2 *A;
A=new TForm2(NULL); //这个地方!!第二次被调用的时候出错!!
A->ShowModal();
delete A;
A=NULL;
return 1;
}
被外部程序调用第二次是出错!!(而且如果把TForm,换成TLabel什么的不会报错
以!)

类型的代码在Delphi下面,一点问题都没有!!
望那位高手能够解答!!!!急!!




...全文
25 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
lixiaolei 2002-03-30
  • 打赏
  • 举报
回复
看来我以后用C++Builder要小心了!问题真是太奇怪了!!送分了!!
lixiaolei 2002-03-30
  • 打赏
  • 举报
回复
感谢各位的帮助!!我还是有一些不解!!
我已经知道一些原因了!

我的例子是选择【Dll Wizard】,然后去掉了【VCL】选择,后来编程的时候没有注意!没想到居然出现这种情况!!!为什么C++Builder编译的时候不提示一下??

invalid 2002-03-29
  • 打赏
  • 举报
回复
具体到你上面的代码一点问题都没有!
问题在其它方面!
kingcaiyao 2002-03-29
  • 打赏
  • 举报
回复
extern "C" __declspec(dllexport) int WINAPI play(void)
{
TForm2 *A;
A=new TForm2(Application); //这个地方!!第二次被调用的时候出错!!
A->ShowModal();
delete A;
return 1;
}
xdspower 2002-03-29
  • 打赏
  • 举报
回复
把最后的A=NULL;删除了
augur 2002-03-29
  • 打赏
  • 举报
回复
没有问题的啊 我试了!^_^

extern "C" __declspec(dllexport) int WINAPI play(void)
{
TForm2 *A;
A=new TForm2(NULL); //这个地方!!第二次被调用的时候出错!!
A->ShowModal();
delete A;
return 1;
}
invalid 2002-03-29
  • 打赏
  • 举报
回复
试试,把你调用程序的Option中的Link with RTL选中。
我的BCB5的主程序,调用dll,第二次非法,解决方法就是选中。
不知道你的是不是这种情况。试试吧。
lixiaolei2000 2002-03-29
  • 打赏
  • 举报
回复
关注!
lixiaolei 2002-03-29
  • 打赏
  • 举报
回复
顶一下!
lixiaolei 2002-03-29
  • 打赏
  • 举报
回复
???
lixiaolei 2002-03-29
  • 打赏
  • 举报
回复
感谢楼上的老兄的回复,不过好像还是有错!!你可以试试好吗?
txf168 2002-03-29
  • 打赏
  • 举报
回复
A=new TForm2(NULL); //这个地方!!第二次被调用的时候出错!!
这个null


TForm *A;
A=new TForm(Form2); //这个地方!!第二次被调用的时候出错!!
A->ShowModal();
delete A;
A=NULL;
return 1;
}

lixiaolei 2002-03-29
  • 打赏
  • 举报
回复
顶一下!
invalid 2002-03-29
  • 打赏
  • 举报
回复
call的unit.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
typedef int WINAPI (* play)(void);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE hDll;
hDll=LoadLibrary("Project2.dll");
if (hDll)
{
play a=(play)GetProcAddress(hDll,"play");
if(a)
{
a();
a();//第二次出错!
a();
a();//第二次出错!
a();
a();//第二次出错!
a();
a();//第二次出错!
a();
a();//第二次出错!
a();
a();//第二次出错!
a();
a();//第二次出错!
a();
a();//第二次出错!

}
FreeLibrary(hDll);
}
}
dll的主cpp unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#include "unit2.h"
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) int WINAPI play(void)
{
TForm2 *A;
A=new TForm2(NULL);
A->ShowModal();
delete A;
return 100;
}
form2的。h文件
//---------------------------------------------------------------------------

#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);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif
form2的。cpp文件
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
invalid 2002-03-29
  • 打赏
  • 举报
回复
试验结果出来了,没有任何问题!!!!
我不是调用2次,而是16次。
具体操作如下:
1.调用程序就放置了一个按钮,在按钮事件中调用。就加了按钮事件和
typedef int WINAPI (* play)(void);
2.动态库,直接用File new ...的Dll向导生成了一个dll。
把上面的代码贴入。补齐括号。
又new了一个form。然后在dll的中包含form的头文件。
编译dll和call。试验没有任何问题。设置都是缺省的。
代码如下:
call的unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
invalid 2002-03-29
  • 打赏
  • 举报
回复
我等下试验一下.
nickgg 2002-03-29
  • 打赏
  • 举报
回复
A=new TForm2(NULL);//Error
A=new TForm2(Application);//ok
A=new TForm2(MainApp);//MainApp由调用传递进来,Ok
pepo2000 2002-03-29
  • 打赏
  • 举报
回复
还要把delete A换成
Form2->ShowModal();
delete Form2;
pepo2000 2002-03-29
  • 打赏
  • 举报
回复
哈哈?
我也遇到了类似的问题,不过我解决了,你试一下面的代码吧?我想应该没有问题了。
Form2=new TForm2(Application); //这个地方!!第二次被调用的时候 Form2->ShowModal();
delete A;
return 1;
lixiaolei2000 2002-03-29
  • 打赏
  • 举报
回复
TFORM2 没有任何代码!!!
加载更多回复(7)

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧