在同一解决方案中的项目之间传递CStrings

时间:2017-03-21 10:53:01

标签: c++ string dll mfc

这是我的班级

namespace AuthenticationAdapter
{
    class  OPENID_EXPORT_API AuthenticationHelper
        {
            public:
                static CString isOpenIdAuthentication( CString hostUrl, bool &isOpenId );
                static CString Authenticate( CString hostUrl, CString& tokenId);

        };
};

这是openidExport.h

#ifndef OPENIDEXPORT_H
#define OPENIDEXPORT_H

#ifdef OPENID_EXPORT
    #define OPENID_EXPORT_API __declspec(dllexport)
#else
    #define OPENID_EXPORT_API __declspec(dllimport)
#endif

#endif

这是实施

namespace AuthenticationAdapter
{
    CString AuthenticationHelper::isOpenIdAuthentication( CString hostUrl,bool &isOpenId )
    {
        ...
    }
}

这是电话

CString error = AuthenticationAdapter::AuthenticationHelper::isOpenIdAuthentication( 
            url,
            connection->m_isOpenId);

这是错误,两个项目都使用unicode,我可以通过使用LPCTSTR来绕过它,但我很想知道问题是什么。

error LNK2019: unresolved external symbol "public: static class 
ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class 
ATL::ChTraitsCRT<wchar_t> > > __cdecl 
AuthenticationAdapter::AuthenticationHelper::isOpenIdAuthentication(class 
ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class 
ATL::ChTraitsCRT<wchar_t> > >,bool &)" 
(?isOpenIdAuthentication@AuthenticationHelper@AuthenticationAdapter@@SA?AV?
$CStringT@_WV?$StrTraitMFC_DLL@_WV?$ChTraitsCRT@_W@ATL@@@@@ATL@@V34@AA_N@Z) 
referenced in function "public: class CServerConnection * __thiscall 
CGlobalData::getAuthDetails(wchar_t const *,wchar_t const *,int)" (?
getAuthDetails@CGlobalData@@QAEPAVCServerConnection@@PB_W0H@Z)  

2 个答案:

答案 0 :(得分:1)

您似乎在DLL接口边界处导出CString。如果是这样,请确保使用该DLL和DLL本身的所有项目动态链接 CRT 相同风格 MFC 库。

此外,在您的问题代码中,您编写了一个明确的固定__declspec(dllexport)

namespace AuthenticationAdapter
{
  class  __declspec(dllexport) AuthenticationHelper
  {
   public:
    static CString isOpenIdAuthentication( CString hostUrl, bool &isOpenId );
    static CString Authenticate( CString hostUrl, CString& tokenId);
  };
}

构建DLL时应该dllexport,而在客户端项目中使用dllimport

因此,请考虑使用适应性dllimport / dllexport模式,例如:

// In DLL header

// Client code hasn't defined MYDLL_API,
// so dllimport in clients.
#ifndef MYDLL_API
#define MYDLL_API __declspec(dllimport)
#endif

namespace AuthenticationAdapter
{
  class MYDLL_API AuthenticationHelper
  {
...

在DLL实现.cpp文件中:

// Define *before* including DLL header,
// to export DLL stuff
#define MYDLL_API __declspec(dllexport)

#include "MyDll.h" // Include DLL header

// DLL implementation code ...

答案 1 :(得分:0)

您在课堂上声明了isOpenIdAuthentication方法,但您可能无法在任何地方实施该方法。

更新: 您应该在签名中包含命名空间和类。此外,您不应再次声明您的命名空间。

此:

namespace AuthenticationAdapter
{
CString AuthenticationHelper::isOpenIdAuthentication( CString hostUrl,bool &isOpenId )
    {
    ...
    }
}

应该是:

CString AuthenticationHelper::AuthenticationHelper::isOpenIdAuthentication( CString hostUrl,bool &isOpenId )
{
    ...
}