Answered by:
Win32 DLL which have a Window

Question
-
I'd like to make a Win32 DLL.
This DLL will have only one function of which name is DS_Entry().
Dll's DS_Entry() function should create a window which have only [Close] button.
If I create Win32 DLL project, DllMain() exists.
In case of Win32 Application, there's WinMain and WndProc.
I have no idea where do I add this code in DLL.
Can anybody give some idea?
- Edited by Jeff0803 Wednesday, September 9, 2015 5:19 PM typo
Friday, September 4, 2015 5:33 PM
Answers
-
The following is some eample code that creates a DLL. The DLL exports a function that can be used to create a window. I hope you find it informative.
MyDLL.h :
// The following ifdef block is the standard way of creating macros which make exporting // from a DLL simpler. All files within this DLL are compiled with the MYDLL_EXPORTS // symbol defined on the command line. This symbol should not be defined on any project // that uses this DLL. This way any other project whose source files include this file see // MYDLL_API functions as being imported from a DLL, whereas this DLL sees symbols // defined with this macro as being exported. #ifdef MYDLL_EXPORTS #define MYDLL_API __declspec(dllexport) #else #define MYDLL_API __declspec(dllimport) #endif // Functions exported from the MyDLL.dll MYDLL_API int CALLBACK plus(int x, int y); MYDLL_API HWND CALLBACK ShowDLLWindow(HWND hParent, LPCWSTR lpszWindowCaption);
MyDLL.cpp
// MyDLL.cpp : Defines the exported functions for the DLL application. // #include "stdafx.h" #include "resource.h" #include "MyDLL.h" HMODULE hDLLInstance; // set in DLLMain LRESULT CALLBACK DLLWndProc(HWND, UINT, WPARAM, LPARAM); // DLLMain defines the entry point for the DLL. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: hDLLInstance = hModule; break; } return TRUE; } // Register the window class used in the ShowDLLWindow function to create a window. ATOM RegisterDLLWndClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = DLLWndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszMenuName = NULL; wcex.lpszClassName = L"MyDLLWindow"; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_ICON2)); return RegisterClassEx(&wcex); } //This is the WndProc for the window created by the exported DLL ShowDLLWindow function LRESULT CALLBACK DLLWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... EndPaint(hWnd, &ps); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } //This function is exported and creates a window MYDLL_API HWND CALLBACK ShowDLLWindow(HWND hParent, LPCWSTR lpszWindowCaption) { RegisterDLLWndClass(hDLLInstance); HWND hWnd = CreateWindow(L"MyDLLWindow", lpszWindowCaption, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, hParent, NULL, hDLLInstance, NULL); if (hWnd != NULL) { ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); } return hWnd; } // This is an example of an exported function. MYDLL_API int CALLBACK plus(int x, int y) { return (x + y); }
Sunday, September 6, 2015 6:53 PM -
WinMain is the entry point for a windows application (.exe). The entry point for a DLL is DLLMain.
WndProc is a function known as a window procedure. Every window has a window procedure that handles window messages that are dispatched to the related window or passes them to the default window procedure for processing. The RegisterClass/RegisterClassEx functions associate a WndProc with the window created by CreateWindow/CreateWindowEx.
If you use the Visual Studio new project wizard to create a Win32 application project and a Win32 DLL project you will be able to see these items in the source code that is generated by the wizard.
You can include all the code necessary within a DLL to create a window. You will have to properly export a function from the DLL so that it is accessible to an application that uses it. If you use the Win32 DLL project wizard to create a DLL project make sure to specify that it should create exports. The generated code will then include examples of exported functions.
Friday, September 4, 2015 5:59 PM
All replies
-
For a quick start, just call MessageBox from your function. The button will be labeled OK, with some other options.
By the way a DLL can have only one function only if its name is DllMain. Otherwise, it wold have two or more functions ;)
Friday, September 4, 2015 5:57 PM -
WinMain is the entry point for a windows application (.exe). The entry point for a DLL is DLLMain.
WndProc is a function known as a window procedure. Every window has a window procedure that handles window messages that are dispatched to the related window or passes them to the default window procedure for processing. The RegisterClass/RegisterClassEx functions associate a WndProc with the window created by CreateWindow/CreateWindowEx.
If you use the Visual Studio new project wizard to create a Win32 application project and a Win32 DLL project you will be able to see these items in the source code that is generated by the wizard.
You can include all the code necessary within a DLL to create a window. You will have to properly export a function from the DLL so that it is accessible to an application that uses it. If you use the Win32 DLL project wizard to create a DLL project make sure to specify that it should create exports. The generated code will then include examples of exported functions.
Friday, September 4, 2015 5:59 PM -
The following is some eample code that creates a DLL. The DLL exports a function that can be used to create a window. I hope you find it informative.
MyDLL.h :
// The following ifdef block is the standard way of creating macros which make exporting // from a DLL simpler. All files within this DLL are compiled with the MYDLL_EXPORTS // symbol defined on the command line. This symbol should not be defined on any project // that uses this DLL. This way any other project whose source files include this file see // MYDLL_API functions as being imported from a DLL, whereas this DLL sees symbols // defined with this macro as being exported. #ifdef MYDLL_EXPORTS #define MYDLL_API __declspec(dllexport) #else #define MYDLL_API __declspec(dllimport) #endif // Functions exported from the MyDLL.dll MYDLL_API int CALLBACK plus(int x, int y); MYDLL_API HWND CALLBACK ShowDLLWindow(HWND hParent, LPCWSTR lpszWindowCaption);
MyDLL.cpp
// MyDLL.cpp : Defines the exported functions for the DLL application. // #include "stdafx.h" #include "resource.h" #include "MyDLL.h" HMODULE hDLLInstance; // set in DLLMain LRESULT CALLBACK DLLWndProc(HWND, UINT, WPARAM, LPARAM); // DLLMain defines the entry point for the DLL. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: hDLLInstance = hModule; break; } return TRUE; } // Register the window class used in the ShowDLLWindow function to create a window. ATOM RegisterDLLWndClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = DLLWndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszMenuName = NULL; wcex.lpszClassName = L"MyDLLWindow"; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_ICON2)); return RegisterClassEx(&wcex); } //This is the WndProc for the window created by the exported DLL ShowDLLWindow function LRESULT CALLBACK DLLWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... EndPaint(hWnd, &ps); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } //This function is exported and creates a window MYDLL_API HWND CALLBACK ShowDLLWindow(HWND hParent, LPCWSTR lpszWindowCaption) { RegisterDLLWndClass(hDLLInstance); HWND hWnd = CreateWindow(L"MyDLLWindow", lpszWindowCaption, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, hParent, NULL, hDLLInstance, NULL); if (hWnd != NULL) { ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); } return hWnd; } // This is an example of an exported function. MYDLL_API int CALLBACK plus(int x, int y) { return (x + y); }
Sunday, September 6, 2015 6:53 PM