动态加载DLL 运行程序就死了

xxgamexx 2007-04-14 05:36:16
/*-------------------------------FDLL.h-------------------*/
#include <windows.h>
#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)BOOL CALLBACK EdrCenterText (HDC, PRECT, PCSTR) ;
#define EXPORT __declspec (dllexport)
#endif

EXPORT BOOL CALLBACK EdrCenterTextA (HDC, PRECT, PCSTR) ;
EXPORT BOOL CALLBACK EdrCenterTextW (HDC, PRECT, PCWSTR) ;

#ifdef UNICODE
#define EdrCenterText EdrCenterTextW
#else
#define EdrCenterText EdrCenterTextA
#endif

/*-------------------------------FDLL.cpp-------------------*/

#include "FDLL.h"

int WINAPI DllMain ( HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
return TRUE ;
}

EXPORT BOOL CALLBACK EdrCenterTextA ( HDC hdc, PRECT prc, PCSTR pString)
{
int iLength ;
SIZE size ;

iLength = lstrlenA (pString) ;
GetTextExtentPoint32A (hdc, pString, iLength, &size) ;
return TextOutA (hdc,( prc->right - prc->left - size.cx) / 2,
( prc->bottom - prc->top - size.cy) / 2,
pString, iLength) ;
}

EXPORT BOOL CALLBACK EdrCenterTextW (HDC hdc, PRECT prc, PCWSTR pString)
{
int iLength ;
SIZE size ;

iLength = lstrlenW (pString) ;
GetTextExtentPoint32W (hdc, pString, iLength, &size) ;
return TextOutW (hdc, ( prc->right - prc->left - size.cx) / 2,
( prc->bottom - prc->top - size.cy) / 2,
pString, iLength) ;
}


/*---------------main.cpp----------------*/


....


HINSTANCE hLib=NULL;
PFNTEXT fpBeginPath;
PAINTSTRUCT ps ;
HDC hdc;
RECT rcClient;


switch (message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rcClient);
fpBeginPath(hdc,&rcClient,TEXT("Used in the DLL"));
EndPaint(hwnd,&ps);
return 0;
case WM_CREATE:
hLib=LoadLibrary(TEXT("FDLL.DLL"));
if(hLib)
{
fpBeginPath=(PFNTEXT)GetProcAddress(hLib,TEXT("BiginPath"));
}
else
{
MessageBox(hwnd,"Can't Open the DLL\"FDLL.DLL\".","Message",MB_OK|MB_ICONERROR);
DestroyWindow(hwnd);
}


case WM_DESTROY:
if(hLib);
FreeLibrary(hLib);


估计问题 是在获得窗口RECT上 但是不知道怎么搞 高手指教
...全文
378 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxgamexx 2007-04-19
  • 打赏
  • 举报
回复
好了 谢谢
neil_cn 2007-04-18
  • 打赏
  • 举报
回复
很久很久以前就已经确定是 GetProcAddress 返回的句柄值是 0,也告诉你怎么解决了,可是你就不去改

返回0不代表“没有调用成功”,而是调用成功了,但是没找到你给它的函数名入口地址
我再解释一下我在二楼说的吧
fpBeginPath=(PFNTEXT)GetProcAddress(hLib,TEXT("BiginPath"));
("BiginPath") ???

我打问号的意思是问你,你确定这个函数名称是 "BiginPath" 吗?貌似不对吧


fpBeginPath(hdc,&rcClient,TEXT("Used in the DLL"));
==>
if( fpBeginPath) fpBeginPath(hdc,&rcClient,TEXT("Used in the DLL"));

这部分我的意思是把上面的语句修改成下面的语句,原因很简单,在使用 fpBeginPath 这个函数指针前要判断这个指针的值是否合法

另外,调用 GetProcAddress函数时的第二个参数我从来没有用过 TEXT("...") 这种形式,所以不敢妄言,我以前直接用 "..."
xxgamexx 2007-04-18
  • 打赏
  • 举报
回复
已经确定了 是GetProcAddress出错了~ 没有调用成功~
xxgamexx 2007-04-17
  • 打赏
  • 举报
回复
那么我这里 应该怎么设置呢?
neil_cn 2007-04-16
  • 打赏
  • 举报
回复
if( fpBeginPath) fpBeginPath(hdc,&rcClient,TEXT("Used in the DLL"));

你 GetProcAddress 返回的值不判断就用啊?
一次是 “BiginPath”,你根本没导出这个名字的函数
一次是 “EdrCenterText”,这个只是一个宏

我上贴已经说得很明白了
roger_77 2007-04-16
  • 打赏
  • 举报
回复
函数调用约定问题

CALLBACK 与 extern "C"是两种不同的函数调用约定,LZ把他们用在一起了.请只使用其中一种
xxgamexx 2007-04-16
  • 打赏
  • 举报
回复
LPCSTR lpProcName 参数表示什么意思?
txk1452 2007-04-16
  • 打赏
  • 举报
回复
FARPROC GetProcAddress( HMODULE hModule, LPCSTR lpProcName);
其中lpProcName不能是Unicode
uoice 2007-04-16
  • 打赏
  • 举报
回复
ls对的.
xxgamexx 2007-04-15
  • 打赏
  • 举报
回复
fpBeginPath=(PFNTEXT)GetProcAddress(hLib,TEXT("EdrCenterText"));
这样程序也死了~~~
xxgamexx 2007-04-15
  • 打赏
  • 举报
回复
楼上的意思是?
neil_cn 2007-04-14
  • 打赏
  • 举报
回复
fpBeginPath=(PFNTEXT)GetProcAddress(hLib,TEXT("BiginPath"));

("BiginPath") ???

fpBeginPath(hdc,&rcClient,TEXT("Used in the DLL"));
==>
if( fpBeginPath) fpBeginPath(hdc,&rcClient,TEXT("Used in the DLL"));

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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