EXE编译可以通过,连接时出错
error LNK2019: unresolved external symbol "__declspec(dllimport) int __stdcallDLL中代码示例:
//主程序CPP#include "stdafx.h"
#include <afxdllx.h>
#include "YYAPI.h"
#ifdef _MANAGED
#error Please read instructions in Support.cpp to compile with /clr
// If you want to add /clr to your project you must do the following:
// 1. Remove the above include for afxdllx.h
// 2. Add a .cpp file to your project that does not have /clr thrown and has
//    Precompiled headers disabled, with the following text:
// #include <afxwin.h>
// #include <afxdllx.h>
#endif#ifdef _DEBUG
#define new DEBUG_NEW
#endif
static AFX_EXTENSION_MODULE SupportDLL = { NULL, NULL };#ifdef _MANAGED
#pragma managed(push, off)
#endifextern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved); if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("Support.DLL Initializing!\n");

// Extension DLL one-time initialization
if (!AfxInitExtensionModule(SupportDLL, hInstance))
return 0; // Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
//  an MFC Regular DLL (such as an ActiveX Control)
//  instead of an MFC application, then you will want to
//  remove this line from DllMain and put it in a separate
//  function exported from this Extension DLL.  The Regular DLL
//  that uses this Extension DLL should then explicitly call that
//  function to initialize this Extension DLL.  Otherwise,
//  the CDynLinkLibrary object will not be attached to the
//  Regular DLL's resource chain, and serious problems will
//  result. new CDynLinkLibrary(SupportDLL); }
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("Support.DLL Terminating!\n"); // Terminate the library before destructors are called
AfxTermExtensionModule(SupportDLL);
}
return 1;   // ok
}#ifdef _MANAGED
#pragma managed(pop)
#endif
=============================================================================
YYAPI.h
namespace YYAPI{

//创建一个GUID
extern AFX_EXT_API CString WINAPI AfxGetGID();
}=============================================================================在EXE中的调用
CString s=YYAPI:: AfxGetGID();=============================================================================
error LNK2019: unresolved external symbol "__declspec(dllimport) int __stdcall YYAPI:: AfxGetGID()" (__imp_? AfxGetGID@YYAPI@@YGHPBDAAVCStringArray@@@Z) referenced in function "public: void __thiscall CDataTestDlg::OnBnClickedOk(void)" (?OnBnClickedOk@CDataTestDlg@@QAEXXZ)
..\debug\DataTest.exe : fatal error LNK1120: 1 unresolved externals

解决方案 »

  1.   

    用工具查看DLL的导出,根本就没有命名空间中所有函数。有人说在DLL中为了与其他语言的沟通(因为命名空间是C++的),所以根本不允许使用。
    我想问问像这种情况,有什么解决方案。
      

  2.   

    上面只贴出了DLL中AfxGetGID的声明,没有实现。
    另外,导出函数最好不要使用CString作为返回值或参数。
      

  3.   

    to:cnzdgs
    实现很简单,而且我这只是个例子,就算用的不是CString做返回,效果是一样的。
    关键的是我怀疑在DLL编译时,根本就不导出命名空间里的东西。
      

  4.   

    主要就三个文件,support.cpp,supportapi.h,supportapi.cpp请cnzdgs指正// Support.cpp : Defines the initialization routines for the DLL.
    //#include "stdafx.h"
    #include <afxdllx.h>#include "SupportAPI.h"#ifdef _MANAGED
    #error Please read instructions in Support.cpp to compile with /clr
    // If you want to add /clr to your project you must do the following:
    // 1. Remove the above include for afxdllx.h
    // 2. Add a .cpp file to your project that does not have /clr thrown and has
    //    Precompiled headers disabled, with the following text:
    // #include <afxwin.h>
    // #include <afxdllx.h>
    #endif#ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    static AFX_EXTENSION_MODULE SupportDLL = { NULL, NULL };#ifdef _MANAGED
    #pragma managed(push, off)
    #endifextern "C" int APIENTRY
    DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
    {
    // Remove this if you use lpReserved
    UNREFERENCED_PARAMETER(lpReserved); if (dwReason == DLL_PROCESS_ATTACH)
    {
    TRACE0("Support.DLL Initializing!\n");

    // Extension DLL one-time initialization
    if (!AfxInitExtensionModule(SupportDLL, hInstance))
    return 0; // Insert this DLL into the resource chain
    // NOTE: If this Extension DLL is being implicitly linked to by
    //  an MFC Regular DLL (such as an ActiveX Control)
    //  instead of an MFC application, then you will want to
    //  remove this line from DllMain and put it in a separate
    //  function exported from this Extension DLL.  The Regular DLL
    //  that uses this Extension DLL should then explicitly call that
    //  function to initialize this Extension DLL.  Otherwise,
    //  the CDynLinkLibrary object will not be attached to the
    //  Regular DLL's resource chain, and serious problems will
    //  result. new CDynLinkLibrary(SupportDLL); }
    else if (dwReason == DLL_PROCESS_DETACH)
    {
    TRACE0("Support.DLL Terminating!\n"); // Terminate the library before destructors are called
    AfxTermExtensionModule(SupportDLL);
    }
    return 1;   // ok
    }#ifdef _MANAGED
    #pragma managed(pop)
    #endif===========================================================================================================
    //SupportAPI.h
    #pragma once
    namespace YYAPI{

    //创建一个GUID
    extern AFX_EXT_API CString WINAPI AfxGetGID();
    }
    =============================================================================================================
    //SupportAPI.cpp
    #include "stdafx.h"
    #include "SupportAPI.h"
    #include <math.h> 
    #include <float.h>
    #include "io.h"
    CString WINAPI AfxGetGID()
    {
    GUID guid;
    ::CoCreateGuid(&guid);
    WCHAR wcharGUID[40];  
    StringFromGUID2(guid,wcharGUID,40);  //把GUID先转成WCHAR
    BSTR bstrGUID=SysAllocString(wcharGUID);  //再把WCHAR转成BSTR
    int  len=SysStringLen(bstrGUID);
    char* charGUID=(char *)malloc(sizeof(char)*(len*2+2));
    WideCharToMultiByte(CP_ACP,0,(LPCWSTR)bstrGUID, -1,charGUID, int(strlen(charGUID)), NULL, NULL ); //最后把BSTR转成char*
    CString strGUID;
    strGUID.Format(_T("%s"),charGUID);
    free(charGUID);
    ::SysFreeString(bstrGUID); //释放资源
    return strGUID;
    }
      

  5.   

    AfxGetGID的函数实现没有放在名空间里面。
      

  6.   

    怎么放,就象上面这段代码
    namespace YYAPI{ 
    CString WINAPI AfxGetGID() 

    GUID guid; 
    ::CoCreateGuid(&guid); 
    WCHAR wcharGUID[40];  
    StringFromGUID2(guid,wcharGUID,40);  //把GUID先转成WCHAR 
    BSTR bstrGUID=SysAllocString(wcharGUID);  //再把WCHAR转成BSTR 
    int  len=SysStringLen(bstrGUID); 
    char* charGUID=(char *)malloc(sizeof(char)*(len*2+2)); 
    WideCharToMultiByte(CP_ACP,0,(LPCWSTR)bstrGUID, -1,charGUID, int(strlen(charGUID)), NULL, NULL ); //最后把BSTR转成char* 
    CString strGUID; 
    strGUID.Format(_T("%s"),charGUID); 
    free(charGUID); 
    ::SysFreeString(bstrGUID); //释放资源 
    return strGUID; 

    }貌似不行呀