#### Archived

This topic is now archived and is closed to further replies.

# DLL Fundamentals :: MFC

This topic is 5881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I am learning DLL creation and implemention from Programming Applications for Microsoft Windows by Jeffrey Richter. Richter demonstrations are straightforward and appear simple. However, I cannot implement a working at all using Visual C++ .NET. I tried using the DLL Wizard and hardcoding a .cpp and .h file. Neither method worked. I would like to create a simple dll with functions I can use in any C++ programs I work on including MFC. In fact, all my Windows program are MFC. Here is an example of the code. ----- // myDLL.h #ifndef myDLL #define myDLL extern "C" __declspec(dllimport) #endif class myDLL MyClass { MyClass(); void FunctionOne(); int FunctionTwo(); ~MyClass(); ----- ----- // myDLL.cpp #define myDLL extern "C" __declspect(dllexport) #include "stdafx.h" #include "myDLL.h" MyClass::MyClass() { } ... ----- The code above uses the exact sample algorithm Richter presents in his book. It does not work. I would like a good solid working example of creating a dll that works in both MFC and non-MFC Windows programs. Thanks, Kuphryn

##### Share on other sites
Um, try starting here first

  #include <windows.h>BOOL WINAPI DllMain(    HINSTANCE hinstDLL,  // handle to DLL module    DWORD fdwReason,     // reason for calling function    LPVOID lpReserved )  // reserved{    // Perform actions based on the reason for calling.    switch( fdwReason )     {         case DLL_PROCESS_ATTACH:         // Initialize once for each new process.         // Return FALSE to fail DLL load.            break;        case DLL_THREAD_ATTACH:         // Do thread-specific initialization.            break;        case DLL_THREAD_DETACH:         // Do thread-specific cleanup.            break;        case DLL_PROCESS_DETACH:         // Perform any necessary cleanup.            break;    }    return TRUE;  // Successful DLL_PROCESS_ATTACH.}

##### Share on other sites
Why doesn''t it work? What error message, if any, do you get? What does happen? The code you''ve posted looks OK, but without knowing what''s happening, I can''t tell you how to fix it...

If I had my way, I''d have all of you shot!

codeka.com - Just click it.

##### Share on other sites
Okay. Thanks.

CheckAbdoul posted a link at MSDN Newsgroups.

http://www.flipcode.com/tutorials/tut_dll01.shtml

That tutorial helped me.

The tutorial he gives helps me considerably. I am confused at the point where the compiler produces a .dll, .lib, and .h file.

For implicit implementation, you have to including the .h file in the source code and import the .lib file. I do not quite understand that process. I believe the compiler only produce a .dll file and you import that file in your source code only. However, I learn that is true for explicit implementation.

Well, explicit implementation is quicker because you do not have to search for the .h and .lib as well as .dll file. Nonetheless, both implicit and explicit implementations are essential.

Kuphryn

##### Share on other sites
Okay. I just cannot get the compiler to compile the dll without errors.

I have uploaded the entire dll project to my website. Please check it out and see if you can determine the error. I use the exact same technique Jeffrey Richter shows in his book. However, the compiler saying a .pch file cannot be found. Second, I do not understand why Visual C++ includes the stdafx.h file in all projects.

http://www.dslextreme.com/users/kuphryn/software/FirstDLL.zip

Please post if you find an unconditional working solution.

Thanks,
Kuphryn

##### Share on other sites
There's a couple of problems.

1. You're missing the semi-colon on the end of the class declaration. Put a ';' on the end of line 15 in firstdll.h.
2. Change the code:

#ifndef FIRSTDLL#define FIRSTDLL extern "C" __declspec(dllimport)#endif

to:

#if defined(FIRSTDLL_DLL)# define FIRSTDLL extern "C" __declspec(dllexport)#else# define FIRSTDLL extern "C" __declspec(dllimport)#endif

And remove the #define FIRSTDLL line from firstdll.cpp
3. Finally, put a ';' after using std::string in usingdll.cpp.

You've really got to learn how to read error messages, since the errors that VC++ produced was enough for me to work out what was wrong...

If I had my way, I'd have all of you shot!

codeka.com - Just click it.

[edited by - Dean Harding on August 13, 2002 5:54:31 PM]

##### Share on other sites
Okay. Thanks.

Everything works. Well, it compiled okay. Hey, that is a huge step!

Kuphryn

##### Share on other sites
One more questions.

You used this:

-----
#if defined(FIRSTDLL_DLL)
# define FIRSTDLL extern "C" __declspec(dllexport)
#else
# define FIRSTDLL extern "C" __declspec(dllimport)
#endif
-----

I am able to compiler the DLL using this code too.

-----
// FirstDLL.h
#ifndef FIRSTDLL_H
#define FIRSTDLL_H

#define IMPORT __declspec(dllimport)
#define EXPORT __declspec(dllexport)

#ifdef __cplusplus
extern "C"
{
#endif

class EXPORT myClass
{
...
};

#ifdef __cplusplus
}
#endif

#endif
-----

Is there an advantage one declaration has over the other?

Thanks,
Kuphryn

##### Share on other sites
My version will automatically use the correct __declspec, depending on if the header file is included in the DLL project or not. The compiler will automatically define the FIRSTDLL_DLL definition in the DLL project, so it''ll select the __declspec(dllexport) version, and in any project that uses your DLL, it''ll pick the __declspec(dllimport) version, because the FIRSTDLL_DLL preprocessor difinition is not defined.

If I had my way, I''d have all of you shot!

codeka.com - Just click it.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007207

×