Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying add some menu & dialog box include button & edit control to my win32 DLL.
I found I must add resource files to my code to generate box.
In MFC we can do very easy by Class wizard and add menu & dialog box code to our main code. In win32 DLL how I can add this codes? We must do that manually or system generate for us automatically.
For example code same below:
C++
INT_PTR CALLBACK DialogFunc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	HMENU hMenu;
	switch(uMsg)
	{
	case WM_INITDIALOG:
		hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_MENU1));
		SetMenu(hDlg, hMenu);
		break;
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDC_BUTTON1:
		case ID_FILE_MESSAGEBOX:
			MessageBox(hDlg, "This is sample for Reza", "Test", MB_OK);
			break;
		case IDC_BUTTON2:
		case ID_FILE_EXIT:
			SendMessage(hDlg, WM_CLOSE, 0, 0);
			break;
		

		}
		break;
	case WM_CLOSE:
		DestroyWindow(hDlg);
		return TRUE;
		break;
	case WM_DESTROY: 
		PostQuitMessage (0); 
		return TRUE;
		break; 
	}
	return FALSE;
}


Is there any article or manual for this ?
Regards,
Posted
Comments
Sergey Alexandrovich Kryukov 12-Oct-12 15:28pm    
Well, I cannot deny this is a legitimate question, I just wonder why working at this level?
--SA
Rezame 12-Oct-12 15:30pm    
What do you mean be "level"? You mean better work with MFC or level means my C++ knowledge?
Richard MacCutchan 12-Oct-12 16:07pm    
Where does DialogFunc() get called from? This looks like a normal windows proc handler, rather than a dialog procedure.
Rezame 12-Oct-12 16:09pm    
I found this code in internet. I want know how to create this function? Must copy/paste manually or VS2010 has ready this scrip ready?

1 solution

The way to it is quite different than MFC. Here is an example I have written for creating a simple DLL with a dialog and two buttons "Exit" and "Say Hi", It may looks complex but it's really simple,:

C++
// DllDialog.cpp
#define _WIN32_WINNT 0x0501
#define WIN32_LEAN_AND_MEAN

#include "stdafx.h"
#include <windows.h>

#define MESSAGE_MAP(MessageParam, MessageCallback)\
    if(message == MessageParam)\
        return MessageCallback(hwnd, message, wParam, lParam)

#define MESSAGE_MAP_COMMAND(Identifier, MessageCallback)\
    if((message == WM_COMMAND) && (LOWORD(wParam) == Identifier))\
    return MessageCallback(hwnd, message, wParam, lParam);

#define IDD_DIALOGBOX    101
#define IDD_BUTTON_EXIT  1001
#define IDD_BUTTON_SAYHI 1002

#define DllExport  __declspec(dllexport)
#define STDCALL    __cdecl

#if __cplusplus
extern "C" {
#endif

DllExport INT_PTR STDCALL StartDialog(VOID);

#if __cplusplus
};
#endif

// FIXME: There is a better way by passing the hInstance as a parameter to StartDialog
HINSTANCE hInstance = 0;

BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

BOOL OnDialogInit(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL OnDialogDestroy(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL OnDialogClose(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

BOOL OnDialogButtonExitClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL OnDialogButtonSayHiClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    if(ul_reason_for_call == DLL_PROCESS_ATTACH)
        hInstance = (HINSTANCE) hModule;
    return TRUE;
}

INT_PTR StartDialog(VOID)
{
    return DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOGBOX), NULL, DialogProc);
}

BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    MESSAGE_MAP(WM_INITDIALOG, OnDialogInit);
    MESSAGE_MAP(WM_DESTROY, OnDialogDestroy);
    MESSAGE_MAP(WM_CLOSE, OnDialogClose);

    MESSAGE_MAP_COMMAND(IDD_BUTTON_EXIT, OnDialogButtonExitClicked);
    MESSAGE_MAP_COMMAND(IDD_BUTTON_SAYHI, OnDialogButtonSayHiClicked);

    return FALSE;
}

BOOL OnDialogInit(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    return TRUE;
}

BOOL OnDialogDestroy(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PostQuitMessage(0);
    return TRUE;
}

BOOL OnDialogClose(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    DestroyWindow(hwnd);
    return TRUE;
}

BOOL OnDialogButtonExitClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    OnDialogClose(hwnd, message, wParam, lParam);
    return TRUE;
}

BOOL OnDialogButtonSayHiClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    MessageBox(hwnd, TEXT("Hi :D!"), TEXT("DLL"), MB_OK);
    return TRUE;
}



  • Open Visual Stdio, I'm using VS2005, and create a new C++ DLL project named, for example, DllDialog.
  • Add a new resource file by right clicking on the Resource Files and then
  • Add > New Item. Choose from Resource the Resource Files and name it what ever you like, for example, resource.rc.
  • Go to the resource view and right click on the resource file you've just created and choose Add Resource ... and click on the dialog icon and click new.
  • You should see a dialog with two buttons, "OK" and "Cancel"; delete them and add two new buttons, "Exit" and "Say Hi". From the propitiates change the ID of the button "Exit" to IDD_BUTTON_EXIT and IDD_BUTTON_SAYHI for "Say Hi".
  • Find out the value of this IDs, just right click on resource.rc and choose Resource Symbols.


Make sure you change these definitions so they match yours:

C++
#define IDD_DIALOGBOX    101
#define IDD_BUTTON_EXIT  1001
#define IDD_BUTTON_SAYHI 1002


That code is a DLL that exports a function named StartDialog, you could test it by including this header file in your application:

C++
// header.h
#ifndef __HEADER_H__
#define __HEADER_H__

#include <windows.h>

#define STDCALL    __cdecl
typedef INT_PTR (STDCALL *StartDialogFunc)(VOID);

#endif /* __HEADER_H__ */


and call that using dynamic loading for example:

C++
// foo.cpp
#define _WIN32_WINNT 0x0501
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include "header.h"

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    HMODULE hDll;
    StartDialogFunc StartDialog;

    hDll = LoadLibrary(TEXT("DllDialog.dll"));
    if(!hDll) {
        MessageBox(NULL, TEXT("Cannot load DllDialog.dll"), NULL, MB_OK);
        return -1;
    }

    StartDialog = (StartDialogFunc) GetProcAddress(hDll, "StartDialog");

    StartDialog();

    return 0;
}


I hope this is helpful.

Good luck.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900