【问题标题】:Passing CStrings between projects in same solution在同一解决方案中的项目之间传递 CStrings
【发布时间】:2017-03-21 10:53:00
【问题描述】:

这是我的课

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)  

【问题讨论】:

    标签: c++ string dll mfc


    【解决方案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 ...
    

    【讨论】:

    • 我如何检查这是 vs2010?
    • @WendyLisaGibbons:选择项目属性,并在“常规”下检查您是否有“在共享 DLL 中使用 MFC”。此外,在“C/C++|代码生成”下检查“运行时库”是否正确设置为多线程 DLL(或用于调试构建的多线程调试 DLL)。
    • 对不起,我以为我这样做是为了简化代码,添加了完整版本和添加的头文件
    • 关于导出...我确实发现了一个问题,即 OPENID_EXPORT 未包含在我的预处理器设置中
    • @WendyLisaGibbons:我看到你修改了代码。您应该在实现 DLL 代码的 .cpp 源文件中#define OPENID_EXPORT之前包括您的 DLL 公共标头。
    【解决方案2】:

    您在类中声明了isOpenIdAuthentication 方法,但您可能没有在任何地方实现它。

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

    这个:

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

    应该是:

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

    【讨论】:

    • 我没有包含实现,因为我觉得它并没有真正添加任何额外的信息,因为签名完全相同。
    • 由于某种原因,您的编译器找不到它。也许你应该分享它,至少是签名。
    • 添加到开篇文章中
    • 她写她的工作是使用原始 C 字符串指针(例如LPCTSTR)而不是CString。这让我觉得她的问题在于在模块边界共享CString,而 CRT/MFC 风格不匹配。总而言之,CString 是基于CStringT 模板,并且有几个参数可以改变(除了char/wchar_t 字符类型),比如特定的字符串特征等。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 2018-11-03
    相关资源
    最近更新 更多