[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4194
  • Last Modified:

Linking Win32 dll using ATL CString into an MFC project

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...
1 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.
squallseekerAuthor Commented:
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now