Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Create COM interface using ATL dll that wraps existing MFC dll using VC2003

Posted on 2008-10-25
Medium Priority
Last Modified: 2013-11-20
I have created a ATL dll that must wrap an existing MFC dll so that I create a resultant COM interface.
The mfc dll's lib has been properly referenced etc. etc.
The error I get on linking is 2019 which means my call does not match the original.
Here is a typical ATL dll method with its details:
I need to know what I need to do to correct the problem. I want to use implicit linking.

STDMETHODIMP CWrap::CloseDevice(LONG* lngRetVal)
//	[DllImport("Device.dll", EntryPoint = "dllCloseDevice", SetLastError = false,
//		CharSet = CharSet.Ansi,
//		CallingConvention=CallingConvention.Cdecl)]
//		static extern string functionname( string key );
	int iRet = dllCloseDevice();
	*lngRetVal = (long)iRet;
	return S_OK;

Open in new window

Question by:softbee
  • 4
  • 3
LVL 19

Expert Comment

ID: 22805217
> The mfc dll's lib has been properly referenced etc. etc.
Symptoms would say they are not correctly referenced.  You have them listed as additional dependencies in the project properties, and the dll(s) are known to have the desired functions?  Perhaps n 'extern "C"' issue?

Are the MFC dll's built in the same version of Visual Studio as is the new ATL wrapper?  Shouldn't matter, but just trying to eliminate variables here.

What exactly have you done to set up the references to the MFC dll's?
LVL 11

Expert Comment

ID: 22806855
LNK2019 message says which external symbol is unresolved. Could you post the message in full?

Author Comment

ID: 22810494
Thanks for both comments:
I wrote this particular dll myself in 2005.
I have checked using 'dumpbin' and the undecorated name I am looking for corresponds to message 2019 which says:
Wrap.obj : error LNK2019: unresolved external symbol _dllCloseDevice referenced in function "public: virtual long __stdcall CWrap::CloseDevice(long *)" (?CloseDevice@CWrap@@UAGJPAJ@Z).
The original was done using VS6 and I am now working with VS2003.
Is my header prototype correct?
Note the underscore in the error message. That same underscore exists when I examine the original lib using dumpbin. I must also admit that I have never had a clear understanding as to why the underscore should exist.
Does the ATL project require MFC support? Personally I did not think so.

extern "C" { int __declspec(dllexport)dllCloseDevice( void ); }

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 22810516
More explanation:
The setup for the MFC data is done as follows:
I added a lib folder to the project referenced as ..\lib.
I added a include folder referenced as ..\include
The lib file is included in the Linker/Input property under 'Additional Dependencies'
I checked if all this is ok by deleting the lib from the lib directory and making sure I pick up the error that it cant be found.
LVL 11

Expert Comment

ID: 22810681
You should declare the function as dllimport in the header file. The accepted way is to use conditional compilation, as displayed in the attached snippet. The DLL project will have DEVICE_EXPORTS in its preprocessor definitions list.
extern "C" { int __declspec(dllimport) dllCloseDevice( void ); }
    #define DEVICE_API _declspec(dllexport)
    #define DEVICE_API _declspec(dllimport)
extern "C" { int DEVICE_API dllCloseDevice( void ); }

Open in new window


Author Comment

ID: 22811219
Thanks for re-teaching me to look at my problem properly before going public:
I have made the change from export to import and now get the following error:
Wrap.obj : error LNK2019: unresolved external symbol __imp__dllCloseDevice referenced in function "public: virtual long __stdcall CWrap::CloseDevice(long *)" (?CloseDevice@CWrap@@UAGJPAJ@Z)

Since I seem to have lost my common-sense may I ask for one more comment although this has nothing to do with the original subject anymore.

In the meantime I am going to build a one call sample in VC6 just in case.
LVL 11

Accepted Solution

alexcohn earned 1500 total points
ID: 22811412
Do you supply the import library (Device.lib) to the linker? If you haven't got an import library, follow the Microsoft instructions here: http://support.microsoft.com/kb/131313

Author Closing Comment

ID: 31509926
Thanks for pointing out my obvious boo-boo. After 35 years of programming I should now better. Thanks

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

564 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