Solved

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

Posted on 2008-10-25
8
591 Views
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

0
Comment
Question by:softbee
  • 4
  • 3
8 Comments
 
LVL 19

Expert Comment

by:drichards
Comment Utility
> 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?
0
 
LVL 11

Expert Comment

by:alexcohn
Comment Utility
LNK2019 message says which external symbol is unresolved. Could you post the message in full?
0
 
LVL 2

Author Comment

by:softbee
Comment Utility
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.
Question:
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

0
 
LVL 2

Author Comment

by:softbee
Comment Utility
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Expert Comment

by:alexcohn
Comment Utility
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 ); }
 

#ifdef DEVICE_EXPORTS

    #define DEVICE_API _declspec(dllexport)

#else

    #define DEVICE_API _declspec(dllimport)

#endif
 

extern "C" { int DEVICE_API dllCloseDevice( void ); }

Open in new window

0
 
LVL 2

Author Comment

by:softbee
Comment Utility
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.
0
 
LVL 11

Accepted Solution

by:
alexcohn earned 500 total points
Comment Utility
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
0
 
LVL 2

Author Closing Comment

by:softbee
Comment Utility
Thanks for pointing out my obvious boo-boo. After 35 years of programming I should now better. Thanks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now