-
DLL with resources
Okay before you go thinking this is a repost of the same question, know it's not. I would like to know if you can tell me how to go about creating a DLL in MSVC that contains resources and CDialog derived classes.
If you know anything on the topic, let me know. Thanks Charlie.
-
-
VC++ 6.0:
- File->New, "Projects" tab, "Win32 Dynamic-Link Library"
- select "A simple DLL project"
- File->New, "Files" tab, "Resource Script"
- Insert->Resource..., select Icon or Dialog
Use LoadLibrary() to map the DLL into your address space.
Use LoadIcon() to access icons from the DLL.
Use LoadResource() to bring a dialog template into global memory, so you can use it with CreateDialogIndirect() or DialogBoxIndirect()
gg
-
Thank you for the suggestions.
Kuphryn, that was a good link, but the info is no good. The version of VC that fellow uses is completely different from ver6. I cannot create a dll the way he illustrates. Aside from that I can't get his sample app to compile.
Codeplug, I'm sure that would work. Thanks for explaining however I would like to have a dlg that I can create simply by calling a function in the dll. For example, in the dll's .h:
void initDlg(HWND parent);
Then in the app:
initDlg(GetSafeHwnd());
I am successful with doing all this but only until I try to add a CPropertySheet to my dlg then it crashes... There must be something I'm missing, but I don't know what.
-
I missed the "using MFC" part....
Try putting "_AFXDLL" in
Project -> Settings (All Configurations) -> "C/C++" tab -> Category = "Preprocessor" -> "Preprocessor Definitions:"
This is a comma seperated list.
This will tell MFC to look for resources in all modules loaded in your address space, and not just the resources in the EXE that's running.
If that doesn't work, then find out where/why you're crashing using the debugger.
[EDIT] assuming you are linking with MFC DLL dynamically[/EDIT]
gg
-
I want to kill myself trying to make this thing work.
Adding _AFXDLL required me to add the switch /MD. Done. Then it compiles but running the app with the dll doesn't work (ordinal 5076 in MFC42D.dll is missing)... So I gave up on trying that. I've trimmed the whole thing down into a very simple DLL and app and it runs and the dll dialog opens as it should, but then it crashes citing user.exe.
I'm attaching the simplified version of this crap. Please take a look at it and tell me if you can figure out what the deal is. It is too simple, so it's got to be something minor (I hope). Two projects are included: theDLL (which creates the .lib and .dll) and theAPP (which uses them). Just compile theDLL then compile+run theAPP...
Muchas gracias amigos.
[edit]removed attachment[/edit]
-
AHA! I've FINALLY figured it out (thanks to google of course)...
In case you're wondering:
file:new:projects-MFC AppWizard (dll):finish(accept defaults)
do normal dialog stuff. put any free function implementations in the dll's .cpp file and the function name in the .def file. Most importantly the first line of any free function implementation must be: AFX_MANAGE_STATE(AfxGetStaticModuleState());
ie, in MainDllFile.cpp append:
Code:
void doDialog() {
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CTheDialog dlg;
dlg.DoModal();
}
in MainDllFile.def:
Code:
; Explicit exports can go here
doDialog
then in the app using the dll, #include a file that contains the following or just type it into the files that use the func:
extern void doDialog();
==or==
__declspec(dllimport) void doDialog();
Then you can call it. Alternately you can exlude the .def entry and just use a header that both the dll and app can include which contains something like:
Code:
#ifndef _EXPORT_H_
#define _EXPORT_H_
#ifdef THE_DLL// the dll should define this before inclusion
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
DLLEXPORT void doDialog();
#ifdef __cplusplus
}
#endif
#endif //_EXPORT_H_
That's it (yeah, after 4 days of trying)!!!
-
Wow, 3 posts in a row ;P
I just wanted to include a simple sample app. It will be helpful if anyone else is ever looking for a solution to an MFC DLL with AFX or ordinal errors with MFC42d.dll (just throwin in some keywords).
Just compile AfxWinDll then compile+run test.
-
Here's a technical article that will tie up any loose ends on using MFC in DLL's.
Yeah, you should use the appwizards whenever possible.
I downloaded you're code before you removed it - this is what you would have needed to get that code to work:
"theAPP" Project Settings: (Release)
>> C/C++ tab -> Preprocessor -> Remove "_AFXDLL" from Preprocessor definitions (I think you added it from my previous post but that was meant for the DLL project, however when you select Static or Dynamic MFC linkage, it's smart enough to add/remove _AFXDLL from the preprocessor definitions for DLL projects)
"theDLL" Project Settings: (all configurations)
>> General tab -> "Use MFC in a Static Library"
>> Added the following code to MainDlg.cpp
Code:
// This should look familiar now that you've done the MFC Appwizard (DLL) :)
class CTestDLLApp : public CWinApp
{
public:
CTestDLLApp() {}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CTestDLLApp, CWinApp)
END_MESSAGE_MAP()
CTestDLLApp theApp; // absence of this was the root cause of your exceptions
In this example, since "theDLL" is linking with MFC statically, you don't need to call AFX_MANAGE_STATE().
If you link to MFC dynamically, then you do need to call it.
Also, it's a good idea not to pass any MFC objects in or out of the DLL since you can't gaurantee that the version of the MFC on both ends with be the same.
Change this:
>> THEDLL_API void doDialog(CWnd*);
to
>> THEDLL_API void doDialog(HWND);
or
>> THEDLL_API void doDialog(void);
gg