BCB 的DLL建立线程并运行
如何在BCB的DLL里建立线程并运行?试了一下午,居然不行
extern "C" __declspec(dllexport)void __stdcall BaD_Init()
{
ThreadIniQfb *at = new ThreadIniQfb(true);
at->Resume();
at->FreeOnTerminate=true;
}
//线程什么都不运行,就出错 ,XXX地址不能访问之类的错误 ,我这样子调用 有问题?
__fastcall ThreadIniQfb::ThreadIniQfb(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------
void __fastcall ThreadIniQfb::PushTheButton(void)
{
//
}
void __fastcall ThreadIniQfb::Execute()
{ // IniQfb();
// Synchronize((ThreadIniQfb)PushTheButton);
}
//---------------------------------------
[解决办法]
没啥问题
//---------------------------------------
// dll
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#include "Unit2.h"
#pragma argsused
extern "C" __declspec(dllexport)void __stdcall BaD_Init()
{
ThreadIniQfb *at = new ThreadIniQfb(true);
at->FreeOnTerminate=true;
at->Resume();
}
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------
// dll 中的线程类
#ifndef Unit2H
#define Unit2H
//---------------------------------------
#include <Classes.hpp>
//---------------------------------------
class ThreadIniQfb : public TThread
{
private:
protected:
void __fastcall Execute();
public:
__fastcall ThreadIniQfb(bool CreateSuspended);
};
//---------------------------------------
#endif
//.cpp
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#pragma package(smart_init)
__fastcall ThreadIniQfb::ThreadIniQfb(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------
void __fastcall ThreadIniQfb::Execute()
{
// 显示该线程的ID
char* sTid = IntToStr(this->ThreadID).c_str();
MessageBoxA(0, sTid,"ThreadIniQfb->Execute",0);
}
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit3.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
extern "C"
{
__declspec(dllexport) void __stdcall BaD_Init();
};
TForm3 *Form3;
//---------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm3::btn1Click(TObject *Sender)
{
HMODULE hInc = NULL;
hInc = LoadLibrary("project1.dll");
void __stdcall (*MyBaD_Init)(void);
(void*)MyBaD_Init = GetProcAddress(hInc,"BaD_Init");
MyBaD_Init();
}
//---------------------------------------