《Programming Windows》中关于DLL的例子/*----------------------
EDRLIB.H header file ----------------------*/#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endifEXPORT BOOL CALLBACK EdrCenterTextA (HDC, PRECT, PCSTR) ;
EXPORT BOOL CALLBACK EdrCenterTextW (HDC, PRECT, PCWSTR) ;#ifdef UNICODE
#define EdrCenterText EdrCenterTextW
#else
#define EdrCenterText EdrCenterTextA
#endif
EDRLIB.C/*-------------------------------------------------
EDRLIB.C -- Easy Drawing Routine Library module (c) Charles Petzold, 1998
-------------------------------------------------*/#include <windows.h>
#include "edrlib.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) ;
}编译后生成EDRLIB.dll一个VB测试程序 ,如下Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate Declare Function EdrCenterText Lib "EDRLIB.dll" Alias "EdrCenterTextA" (ByVal hdc As Long, prc As RECT, ByVal str As String) As Boolean
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPrivate Sub Command1_Click()
Dim rc As RECT
GetClientRect Me.hwnd, rc
EdrCenterText GetDC(Me.hwnd), rc, "Hello World"
End Sub调试出现Can't find DLL entry point EdrCenterTextA in EDRLIB.dll
执行出现Can't find DLL entry point EdrCenterTextA in EDRLIB.dll尝试过如下办法:
1.删除EDRLIB.H和EDRLIB.C中所有CALLBACK
调试出现Bad DLL calling convention
执行没有问题请问这是为什么?
EDRLIB.H header file ----------------------*/#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endifEXPORT BOOL CALLBACK EdrCenterTextA (HDC, PRECT, PCSTR) ;
EXPORT BOOL CALLBACK EdrCenterTextW (HDC, PRECT, PCWSTR) ;#ifdef UNICODE
#define EdrCenterText EdrCenterTextW
#else
#define EdrCenterText EdrCenterTextA
#endif
EDRLIB.C/*-------------------------------------------------
EDRLIB.C -- Easy Drawing Routine Library module (c) Charles Petzold, 1998
-------------------------------------------------*/#include <windows.h>
#include "edrlib.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) ;
}编译后生成EDRLIB.dll一个VB测试程序 ,如下Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate Declare Function EdrCenterText Lib "EDRLIB.dll" Alias "EdrCenterTextA" (ByVal hdc As Long, prc As RECT, ByVal str As String) As Boolean
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPrivate Sub Command1_Click()
Dim rc As RECT
GetClientRect Me.hwnd, rc
EdrCenterText GetDC(Me.hwnd), rc, "Hello World"
End Sub调试出现Can't find DLL entry point EdrCenterTextA in EDRLIB.dll
执行出现Can't find DLL entry point EdrCenterTextA in EDRLIB.dll尝试过如下办法:
1.删除EDRLIB.H和EDRLIB.C中所有CALLBACK
调试出现Bad DLL calling convention
执行没有问题请问这是为什么?
Bad DLL calling convention的问题,但是还是会出现找不到函数的问题,
修改调用方式后还要写一个def文件加入工程,名字为"工程名.def"
def文件内容LIBRARY "test"
DESCRIPTION 'test Dynamic Link Library'EXPORTS
EdrCenterTextW
EdrCenterTextA这样可以解决函数名找不到的问题