?
Solved

want resource templates export from dll

Posted on 2011-02-17
2
Medium Priority
?
611 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:
Janusz Czopowik earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

765 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