Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use MFC DLL in C program?

Status
Not open for further replies.

evilpeer

Programmer
Sep 24, 2001
16
US
I've written an MFC DLL that works fine with another C++ program I wrote, but now I want to use it with a C program (that I didn't write).

Upon compilation I receive several "unresolved external symbol" errors. The appropriate .h and .lib are included in the program, so I'm a little confused as to why its giving me this error. I tried compiling as C++, but man-oh-man, about three or four hundred errors (mostly type conversions).

Is it possible to use an MFC DLL with a C program, or must I drudge through all the extra errors?
 
There's a couple of considerations when exporting functions from an MFC standard DLL. Firstly you need an instance of a class derived from CWinApp:

class CMfcStdDllApp : public CWinApp
{
public:
DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CMfcStdDllApp, CWinApp)
END_MESSAGE_MAP()

CMfcStdDllApp theApp;

Then you need to ensure that module state is correct on entry to each exported function.

extern "C" int __declspec(dllexport) TestFunc(int i)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return 2*i;
}

The you can import your functions into C code:

int __declspec(dllimport) TestFunc(int i);

Anyway this works writing C code with the VC++ compiler. It should work where your functions use standard C calling conventions. Note that Windows DLLs use the __stdcall convention which causes a few extra problems. Also you can only easily export simple functions, no classes.
 
The only error messages I receive (when compiling for C) are several "unresolved external symbol" refering to the DLL functions. I can fix all of the C++ errors (like I said, mostly type conversions) no problem. It'll just take me forever.

This is not a standard MFC DLL I suppose, but rather one that uses MFC classes and functions in its internals. Only the functions are exported (no DLL main entry point).

All of the function prototypes are defined in the main header file, which is included in the C program. Basically, the functions currently look like this:

int __declspec(dllexport) TestFunc(int i);

So should I be using a different header for the C program itself? Switch them all over to dllimport? Should the extern "C" be in the header, or just in the function definitions?
 
extern "C" is used when compiling C++ code that will be exported for C function calls, or is defining C-Style functions to be called from C++. Normally a dual mode header is used with conditional compilation such as

#ifdef __cplusplus
extern "C" {
#endif
...function declarations...
#ifdef __cplusplus
}
#endif

The simplest method of defining DLL imports/exports in VC++ is with __declspec(dll...). You can similarly use a #define to switch the headers:

#ifdef _EXPORTS
#define IMPEXP __declspec(dllexport)
#else
#define IMPEXP __declspec(dllimport)
#endif

and define your functions with
void IMPEXP myfunc(...) {};
for example. When compiling your DLL you can set /D "_EXPORTS" as compiler option.
 
Well, throwing the extern "C"s into the DLL header and main source file worked well enough for one of the DLLs. I'm going to go apply it to the other now. In my previous question, I was really asking more about the placement of the stuff with reguards to the DLL and program more so than how to use it, but thanks all the same.

Now all I have to do is figure out what's wrong with the other DLLs that I didn't write..
 
I ve a MFC Dll which implements some CPropertySheet and CPropertyPage Classes. But I 'm getting assertuin failure when i try to use the exported function(where CPropertySheet etc are used) from main();
Any help?
Thanks and regards

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top