Solved

want resource templates export from dll

Posted on 2011-02-17
2
609 Views
Last Modified: 2013-11-20
I have an MFC dll where I comfortably use the AFX_CLASS_EXPORT to export complete classes.
on of them is a CDialog derived class which has some resource template.

The point is, when I call dlg.DoModal() in my app, it returns -1 because it searches the Resource in the app , not the dll. How do I call doModal on the exported instance, and load the resource from the dll ?

class AFX_CLASS_EXPORT CMyDllDlg : public CDialog
{...
}

app.exe:

dlg CMyDllDlg; // OK
dlg.DoModal() // resource problem!

Open in new window

0
Comment
Question by:stev75
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
JohnCz earned 500 total points
ID: 34916036
From your descrition of the problem it looks like you are using regular MFC dll.
It requires switching resource handle to tell the app what resource to use.
Your code should look like this:

	HMODULE hLib = LoadLibrary(_T("YourResDllName.dll"));

	ASSERT(hLib != NULL);

	AfxSetResourceHandle(hLib);	// switch to dll resource


	CDllDlg dlg;

	int iret = dlg.DoModal();

	AfxSetResourceHandle(AfxGetInstanceHandle());   // switch back to app resource

Open in new window


Another thing is a macro that you are using to export class. In general it is designed to use in MFC extension dll. It may create a confusion if you are using MFC extension DLL. Besides, in the main project, both AFX_CLASS_EXPORT and AFX_CLASS_IMPORT are defined.
Consider using different, safer approach:

#ifdef DLG_FROM_DLL
#define DLG_FROM_DLL_EXPORT __declspec(dllexport)
#else
#define DLG_FROM_DLL_EXPORT __declspec(dllimport)
#endif
class DLG_FROM_DLL_EXPORT CMyDllDlg : public CDialog
{...
};

Open in new window


In dll project properties, for a C++ in Preprocessor Definitions add DLG_FROM_DLL.
This way when file is included in a dll project, class is exported; in an app project (since DLG_FROM_DLL is not defined, class is impoerted.
0
 

Author Closing Comment

by:stev75
ID: 34940906
i had to switch to the "extension dll". Then with your additions, it works well.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question