Hi all,
I have a problem with this DLL... I am trying to load the DLL at runtime but it is causing an error when the function pointer pfnEdrCent is called.
This is driving me mad because it looks very similar to the code that I have seen on the net and in the Petzold book and I am sure that the DLL is loading correctly. The only thing that I can assume is that the function pointer is pointing nowhere, but I can't see why.
Thanks.
FB
The windows proc:
Code:
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
// Typedef - create a typedef for the pointer to a function
typedef BOOL (WINAPI * EDRCENT) (HDC, PRECT, PCSTR);
static HINSTANCE hLibrary;
EDRCENT pfnEdrCent;
switch(message)
{
case WM_CREATE:
{
if ((hLibrary = LoadLibrary(TEXT("edrlib.dll"))) == NULL)
{
MessageBox (hwnd, TEXT("Cant load edrlib.dll"), TEXT("Error"), 0);
return -1;
}
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect (hwnd, &rect);
pfnEdrCent = (EDRCENT) GetProcAddress(hLibrary, TEXT("EdrCenterText"));
((pfnEdrCent)(hdc, &rect, TEXT("This is a string")));
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
if (hLibrary)
FreeLibrary(hLibrary);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
The DLL header:
Code:
//
//EDRLIB.H Header file
//
#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#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
and the DLL source:
Code:
//
// EDRLIB.C -- Easy drawing routines
//
#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);
}