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
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

765 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