Linking Win32 dll using ATL CString into an MFC project

Posted on 2006-05-30
Last Modified: 2013-11-25
I tested a class I'd written with an MFC wrapper, and then took steps to convert the class to DLL.  Knowing the DLL needed to export to Delphi, I avoided using MFC and tried for Win32, but I like my CString member capability so I am using ATL for that support.  As a dll it compiles fine (some code below), but when I try to write an MFC dialog test wrapper around the dll I get LNK2001 errors.

Portion of the DLL header:
#ifdef DLLDIR_EX
   #define DLLDIR  __declspec(dllexport)
   #define DLLDIR  __declspec(dllimport)
#include <windows.h>
#include <string.h>
#include <atlstr.h>
class DLLDIR CjsrFileMux
      CjsrFileMux(CString path);
      ~CjsrFileMux( );

Portion of MFC test app:
#include "jsrFileMux.h"
   ... then later ...
void CFileMuxDecoderDlg::OnBnClickedDecode()
      // introduce the filemux class from the dll
      CjsrFileMux fmux(m_csDestDirectory);
      CjsrFileMux* pmux = &fmux;
      pmux->ExtractAll(m_csSrcDirectory, m_csDestDirectory);

The error:
FileMuxDecoderDlg.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall CjsrFileMux::CjsrFileMux(class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > >)" (__imp_??0CjsrFileMux@@QAE@V?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@@Z)
FileMuxDecoderDlg.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: bool __thiscall CjsrFileMux::ExtractAll(class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > >,class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > >)" (__imp_?ExtractAll@CjsrFileMux@@QAE_NV?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@0@Z)
C:\IccProj\FileMuxDecoder\Release\FileMuxDecoder.exe : fatal error LNK1120: 2 unresolved externals

I have my release .dll, .lib, .exp, and .h files available in seperate directories and my MFC test project points to them (C/C++ - General - Additional Include; Linker - General - Additional Library Directories; Linker - Input - Additional Dependencies = FileMuxDLL.lib.  The error itself seems to suggest the linker can't convert between ATL and MFC CString forms, so I've tried an explicit cast to that effect and I've tried to add template <class ATL> neither of which help.  I'm generally in hardware design; I do enough VC++ to be quite comfortable with it, but it's possible I may be missing something very simple -- be kind.  Thanks...
Question by:squallseeker
    LVL 48

    Accepted Solution

    Use LPCTSTR instead of CString parameter. Internally you can use CString. CString instance can be created from LPCTSTR using assignment operator. Delphi client has no idea what is CString.

    Author Comment

    Thanks AlexFM.  Not only would this have saved further trouble when trying to tie into Delphi, but it also takes care of the link issue.

    For those that may be interested...
    It was sufficient to just use
    CjsrFileMux fmux( (LPCTSTR) m_csDestDirectory); // on the call side
    and inside the dll...
    CString csvar = the received LPCTSTR parameter
    I didn't have to play with address (&) or pointer-to (*var) to pass anything.
    [Upon reading the response I thought I might need something like:  CjsrFileMux fmux( (LPCTSTR) &m_csDestDirectory);]

    Thanks again

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    760 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

    7 Experts available now in Live!

    Get 1:1 Help Now