Resources dans DLL

cs_jpeg Messages postés 40 Date d'inscription lundi 17 décembre 2001 Statut Membre Dernière intervention 25 février 2004 - 8 juil. 2002 à 11:54
frederic_bourgouin Messages postés 2 Date d'inscription jeudi 13 novembre 2003 Statut Membre Dernière intervention 25 février 2008 - 14 avril 2004 à 10:14
J'ai un problème concernant des ressources contenues dans une DLL MFC. Par exemple, lorsque je fais appel à une boite de dialogue contenue dans cette DLL, il arrive que ce soit une autre resource contenue dans l'éxécutable qui s'affiche. Le problème vient du fait que les deux resources (celle de l'EXE et celle de la DLL) possède le même ID (voir resource.h). La seule solution que j'ai trouvé consiste à faire attention que les deux resources.h possèdent des ID différents, ce qui n'est vraiment pas intéressant et ne fait que déplacer le pb.

Autre question : quelle est exactement, la différence entre les projets "Regular MFC DLL" et "MFC extension DLL" ?
Dans quel cas utiliser l'un plutôt que l'autre

Merci d'avance ...

1 réponse

frederic_bourgouin Messages postés 2 Date d'inscription jeudi 13 novembre 2003 Statut Membre Dernière intervention 25 février 2008
14 avril 2004 à 10:14
Bonjour,
moi aussi j'ai un problème avec les resources dans une dll:
Je ne sais pas comment utiliser une dialogbox(non modale) dans une dll Win32. La dll en elle même fonctionne puisque le debugger entre dans les fonctions exportées:
TEST_DLL1_API int fnTest_Dll1(void);
TEST_DLL1_API BOOL AboutBox (HINSTANCE hInstanceL, HWND hWndParentL);
(pour cela, j'ai ajouté le .lib, la dll et les headers des fonctions exportées dans le projet win32 utilisant la dll)
mais je souhaiterais que la fonction AboutBox(...) créé une boite de dialogue. J'utilise la fonction createdialog avec le nom de la resource dans la dll mais le getlastError récupère toujours l'erreur 0x0716=>code 1814=>ERROR_RESOURCE_NAME_NOT_FOUND.
ce code source n'est probablement pas adapté à ce que je veux faire mais si quelqu'un peut m'aider ....
merci d'avance
fred

=======================================================================
Sources de la dll
test_Dll1.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 TEST_DLL1_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
// TEST_DLL1_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef TEST_DLL1_EXPORTS
#define TEST_DLL1_API __declspec(dllexport)
#else
#define TEST_DLL1_API __declspec(dllimport)
#endif

// This class is exported from the test_Dll1.dll
class TEST_DLL1_API CTest_Dll1 {
public:
CTest_Dll1(void);
// TODO: add your methods here.
};

TEST_DLL1_API HINSTANCE hInstance;
TEST_DLL1_API HWND hWndParent;

extern TEST_DLL1_API int nTest_Dll1;

TEST_DLL1_API int fnTest_Dll1(void);
TEST_DLL1_API BOOL AboutBox (HINSTANCE hInstanceL, HWND hWndParentL);
TEST_DLL1_API LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

test_Dll1.cpp

// test_Dll1.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "test_Dll1.h"
#include "resource.h"

HWND hwndInterf;

BOOL APIENTRY DllMain( HANDLE 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:
break;
}
return TRUE;
}

// This is an example of an exported variable
TEST_DLL1_API int nTest_Dll1=0;

// This is an example of an exported function.
TEST_DLL1_API int fnTest_Dll1(void)
{
return 42;//test de valeur renvoyee
}

// This is the constructor of a class that has been exported.
// see test_Dll1.h for the class definition
CTest_Dll1::CTest_Dll1()
{
return;
}

/*~--------------------------------------------------------------------------------|
| Nom de la fonction : |
| LRESULT CALLBACK test1(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) |
| |
|----------------------------------------------------------------------------------|
| Description : |
|----------------------------------------------------------------------------------|
| Date | Version | Intitule | Nom de l'Auteur | Date qualif.|
|----------|---------|-------------------------|---------------------|-------------|
| 05/03/04 | 1.0 | | BOURGOUIN | |
|----------------------------------------------------------------------------------|
| Visa de qualification: |
| |
| |
| Commentaire : |
| Message handler for dialog box. |
-----------------------------------------------------------------------------------*/
LRESULT CALLBACK test1(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
int result=0;

switch (message)
{
case WM_INITDIALOG:
return TRUE;
break;

case WM_COMMAND:
if (LOWORD(wParam) == IDCANCEL)
{ EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}

if (LOWORD(wParam) == IDOK)
{
}
break;
}
return FALSE;
}

TEST_DLL1_API BOOL AboutBox (
HINSTANCE hInstanceL, HWND hWndParentL
)
{ int erreur=0;
hInstance=hInstanceL;
hWndParent=hWndParentL;

MessageBox(NULL,"Coucou","About Box",MB_OK); //ça c'est OK
hwndInterf= CreateDialog(hInstance,/*(LPCTSTR)IDD_ABOUTBOX*/MAKEINTRESOURCE(101) //ici ya un soucis
,0,(DLGPROC)About);
erreur=GetLastError();
ShowWindow(hwndInterf,SW_SHOW );
return 1;
}

// Mesage handler for about box.
TEST_DLL1_API LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:if (LOWORD(wParam) IDOK || LOWORD(wParam) IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}

====================================================================================
Projet win32 utilisant la dll:

// Test_UtiliseDll1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "Test_UtiliseDll1.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

HINSTANCE hinstLib;

HWND hwndx;

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TEST_UTILISEDLL1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TEST_UTILISEDLL1);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}

// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TEST_UTILISEDLL1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_TEST_UTILISEDLL1;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;

int itest;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_CREATE:
itest=fnTest_Dll1();
AboutBox(hInst, hWnd);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{ case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
FreeLibrary(hinstLib);
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
// Free the DLL module.
FreeLibrary(hinstLib);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:if (LOWORD(wParam) IDOK || LOWORD(wParam) IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
0
Rejoignez-nous