简体   繁体   中英

Passing CStrings between projects in same solution

this is my class

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

        };
};

this is 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

this is the implementation

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

this is the call

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

and this is the error, both projects use unicode, i can get round it by using LPCTSTR instead, but i would love to know what the problem is.

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)  

It seems you are exporting CString at the DLL interface boundaries. If so, make sure that all the projects using that DLL and the DLL itself are dynamically linked to the same flavor of the CRT and MFC libraries.


In addition, in your question code you wrote an explicit fixed __declspec(dllexport) :

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

You should dllexport when building the DLL, but dllimport when using it in client projects.

So, consider using an adaptable dllimport / dllexport pattern, eg:

// 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
  {
...

In the DLL implementation .cpp file:

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

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

// DLL implementation code ...

You declare your isOpenIdAuthentication method in your class, but you probably don't implement it anywhere.

Update: You should include both the namespace and class in the signature. Also, you should not declare your namespace again.

This:

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

Should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM