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

Posted on 2008-10-25
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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:

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

690 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