xcdemon05
xcdemon05

Reputation: 1432

Where do I implement my MFC DLL functions?

I have been searching google all morning and all I can't find what I'm looking for. I'm creating a regular DLL in Visual Studio modified for MFC. That is to say that in the project wizard, I selected

Win32 Project -> DLL -> MFC

I did NOT just click MFC DLL from the main list in the wizard, which is what all the tutorials online were describing.

My question is simple. In the .cpp file, I just need to know whether I'm supposed to implement my methods (declared in the .h file) inside or outside of the _tmain function. Inside there is a comment that says

//TODO: code your applications behavior here

but I'm not sure if that's where my implementations go.

For reference, here is the .cpp file:

// testmfcdllblah.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "testmfcdllblah.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    HMODULE hModule = ::GetModuleHandle(NULL);

    if (hModule != NULL)
    {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            _tprintf(_T("Fatal Error: MFC initialization failed\n"));
            nRetCode = 1;
        }
        else
        {
            // TODO: code your application's behavior here.
        }
    }
    else
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
        nRetCode = 1;
    }

    return nRetCode;
}

Upvotes: 1

Views: 592

Answers (2)

harper
harper

Reputation: 13690

In C++ you can't define local functions. You will never implement any function in _tmain.

When you have used the wizard to create a DLL you should add a header file that defines your interface to the DLL. And you should add a .CPP source file where you implement the function(s).

You can call function at that place where you find

// TODO: change error code to suit your needs

BTW: I don't know why the wizard for a Dynamic Link Library creates a main function.

Upvotes: 1

Carsten
Carsten

Reputation: 11606

Since you cannot implement functions/methods inside other functions your method implementation need to go outside the _tmain function.

The comment block you quoted can be replaced to supply an initialisation implementation of your library.

So if you are declaring a function like SayHello this could look like so:

testmfcdllblah.h:

// Declaration
void SayHello(void);

testmfcdllblah.cpp:

void _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    // .. all the other stuff ..

    // TODO: code your application's behavior here.
    SayHello();

    // .. the rest of the other stuff ..
}

void SayHello()
{
    AfxMessageBox("Hello!");
}

Upvotes: 1

Related Questions