为什么这么个简单的DLL会出错?!
我太菜了!我要死了!
Test.bpf是生成.dll的工程文件,其中代码如下:
Test.cpp的代码:
//------------------------------------------------------------------------#include <vcl.h>
#include <windows.h>
#include <stdio.h>
#include "Test.h"
#pragma hdrstop
#pragma argsused
//------------------------------------------------------------------------BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
return 1;
}
//------------------------------------------------------------------------void DoFunc(void)
{
FILE *fs=fopen("XXX.txt", "a");
fprintf(fs,"I am a Programmer.");
fclose(fs);
}
//------------------------------------------------------------------------
Test.h的代码:
extern "C" __declspec(dllexport) void DoFunc(void);
CallDll.bpr是调用Test.dll的工程,其中的主程式如下:
CallMe.cpp的代码如下:
//------------------------------------------------------------------------
#include <vcl.h>
#include "CallMe.h"
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
{
void (__stdcall *DoFunc)(void);
HANDLE hInst;
hInst = LoadLibrary("Test.dll");
(FARPROC &)DoFunc=GetProcAddress(hInst,"DoFunc");
DoFunc();
FreeLibrary(hInst);
}
catch(...)
{
ShowMessage("Error!!!!!!!");
}
}
//------------------------------------------------------------------------
分别编译后,运行CallDll.exe调用Test.dll,在执行DoFunc()时出错。
出错信息如下:
Access Voilation at address 00000000. Read of address 00000000.
大侠,请问为什么呀?!为什么?!