Доброго времени суток.
Пытаюсь вынести пару функций в dll библиотеку. Пробовал уже несколько вариантов:
- пример с http://www.mingw.org/wiki/sampleDLL.
- пример из Рихтер "Создание эффективных Win-23 приложений"
- и множество других примеров с просторов сети.
В приложенном коде используется вариант, используемый моим одногруппником, у него все работает корректно. Я использую компилятор MinGW.org GCC-6.3.0-1.
Мог бы кто-то объяснить на пальцах или дать ссылку на реальный пример создания и явного связывания dll, как экспортировать функции и структуры. Спасибо
// testdll.h
#include <windows.h>
#include <tchar.h>
#include <string>
#include <stdio.h>
#include <vector>
using std::vector;
extern "C++"{
struct line{
POINT start;
POINT end;
};
__declspec(dllexport) void reDraw(HWND hWnd, HDC hdc, HPEN pen, bool df, POINT cp, vector< struct line > &lines);
__declspec(dllexport) WNDCLASS InitializeWndClass(HINSTANCE hInstance, WNDPROC WProc, LPCTSTR ClassName,
LPCTSTR MenuName, UINT style, HICON Icon, HCURSOR cursor,
HBRUSH Bground, int ClsExtra, int WndExtra);
}
// testdll.cpp
#include "testdll.h"
extern "C++"
{
extern "C++" __declspec(dllexport) void reDraw(HWND hWnd, HDC hdc, HPEN pen, bool df, POINT cp, vector< struct line > &lines)
{
hdc = GetDC(hWnd);
SelectObject(hdc, pen);
if (df){
InvalidateRect(hWnd, NULL, true);
UpdateWindow(hWnd);
lines.back().end = cp;
for (const auto curLine : lines){
POINT tmp[2] = {curLine.start, curLine.end};
Polyline(hdc, tmp, 2);
}
}
}
extern "C++" __declspec(dllexport) WNDCLASS InitializeWndClass(HINSTANCE hInstance, WNDPROC WProc,
LPCTSTR ClassName, LPCTSTR MenuName,
UINT style, HICON Icon, HCURSOR cursor,
HBRUSH Bground, int ClsExtra, int WndExtra)
{
WNDCLASS *wc = new WNDCLASS;
wc->hInstance = hInstance;
wc->lpszClassName = ClassName;
wc->lpfnWndProc = WProc;
wc->style = style;
wc->hIcon = Icon;
wc->hCursor = cursor;
wc->lpszMenuName = MenuName;
wc->cbClsExtra = ClsExtra;
wc->cbWndExtra = WndExtra;
wc->hbrBackground = Bground;
return *wc;
}
}
// test.cpp
#include <windows.h>
#include <tchar.h>
#include <string>
#include <math.h>
#include <stdio.h>
#include <vector>
using std::vector;
HINSTANCE hDLL;
typedef void(*reDraw_func_type)(HWND hWnd, HDC hdc, HPEN pen, bool df, POINT cp, vector< struct line > &lines);
typedef WNDCLASS(*classInit_func_type)(HINSTANCE hInstance, WNDPROC WProc, LPCTSTR ClassName, LPCTSTR MenuName,
UINT style, HICON Icon, HCURSOR cursor, HBRUSH Bground, int ClsExtra, int WndExtra);
reDraw_func_type updateWnd;
classInit_func_type clsInit;
HDC hdc;
HPEN pen = CreatePen(PS_SOLID, 4, RGB(255, 0, 0));
POINT currentPoint;
vector< struct line > scene;
bool DrawFlag = false;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE This, HINSTANCE Prev, LPSTR cmd, int mode)
{
hDLL = LoadLibrary("testdll.dll");
if (!hDLL){
printf("fail to load dll");
return 0;
}
else{
printf("Dll loaded\n");
updateWnd = (reDraw_func_type)GetProcAddress(hDLL, "reDraw");
if (!updateWnd){
printf("Error to load reDraw func\n");
}
else{
printf("reDraw func had been loaded");
}
clsInit = (classInit_func_type)GetProcAddress(hDLL, "InitializeWndClass");
if (!clsInit){
printf("Error to load clsInit func\n");
}
else{
printf("clsInit func had been loaded");
}
if (!updateWnd || !clsInit){
FreeLibrary(hDLL);
return 0;
}
}
WNDCLASS wc = clsInit(This, WndProc, "MainFrame", NULL, (CS_HREDRAW | CS_VREDRAW), LoadIcon(NULL, IDI_APPLICATION),
LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW + 1), 0, 0);
if (!RegisterClass(&wc)) {
MessageBox(NULL, "register class error", "Error", MB_OK);
return 0;
}
HWND hWnd = CreateWindow("MainFrame", "template", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, HWND_DESKTOP, NULL, This, NULL);
ShowWindow(hWnd, mode);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
FreeLibrary(hDLL);
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:{
struct line newLine = {
currentPoint, // start point
currentPoint // end point
};
if (!DrawFlag) DrawFlag = true;
scene.push_back(newLine);
break;
}
case WM_RBUTTONDOWN: {
DrawFlag = false;
break;
}
case WM_MOUSEMOVE: {
GetCursorPos(¤tPoint);
ScreenToClient(hWnd, ¤tPoint);
updateWnd(hWnd, hdc, pen, DrawFlag, currentPoint, scene);
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
<тип> __declspec(dllexport) <имя функции>
и все extern "c++" в .cpp лишние. И у вас нет самого главного, функции входа в длл. – NewView 1 апр '19 в 20:14