主题:帮我看看这段
我建立了个简单的DLL,
但是为什么在调用中没法调用?帮我看看:
这是DLL的代码:
#include <condefs.h>
#include <windows.h>
#pragma hdrstop
#pragma argsused
extern "C" void __declspec(dllexport) atort();//声明为外部导出
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD ul, LPVOID lpre)
{
return 1;
}
//---------------------------------------------------------------------------
void __declspec(dllexport) atort()
{
::MessageBox(NULL,"dd","kk",MB_OK);
}
下面是执行文件的代码(x.cpp和x.h):
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
typedef void(*A)();
extern "C" void __declspec(dllimport) atort();//声明为外部函数
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HMODULE hModule=::LoadLibrary("D:\\Project1.dll");
if(hModule!=NULL)
{
A e=(A)::GetProcAddress(hModule,"atort");
Edit1->Text=IntToStr(e);
if(e!=NULL) { e();}
::FreeLibrary(hModule);
}
}
//---------------------------------------------------------------------------
x.h文件:
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <windows.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TEdit *Edit1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
但是为什么在调用中没法调用?帮我看看:
这是DLL的代码:
#include <condefs.h>
#include <windows.h>
#pragma hdrstop
#pragma argsused
extern "C" void __declspec(dllexport) atort();//声明为外部导出
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD ul, LPVOID lpre)
{
return 1;
}
//---------------------------------------------------------------------------
void __declspec(dllexport) atort()
{
::MessageBox(NULL,"dd","kk",MB_OK);
}
下面是执行文件的代码(x.cpp和x.h):
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
typedef void(*A)();
extern "C" void __declspec(dllimport) atort();//声明为外部函数
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HMODULE hModule=::LoadLibrary("D:\\Project1.dll");
if(hModule!=NULL)
{
A e=(A)::GetProcAddress(hModule,"atort");
Edit1->Text=IntToStr(e);
if(e!=NULL) { e();}
::FreeLibrary(hModule);
}
}
//---------------------------------------------------------------------------
x.h文件:
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <windows.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TEdit *Edit1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif