导出和导入c ++中的dll接口无法正常工作

时间:2012-09-17 12:33:18

标签: c++ winapi visual-c++ dll

  

可能重复:
  Export entire class through c++ interface

this教程开始,我已经尝试用2个项目做一个自己的VS解决方案.dll构建器和.exe构建器,这里是.dll项目头: libheader.h

#pragma once

#ifdef SHREDLIB_EXPORTS
#define SHREDAPI __declspec(dllexport)

#else
#define SHREDAPI __declspec(dllimport)

#endif

struct IShred
{
    virtual int GetStringSize(char*) = 0;
    virtual void Release() = 0;
};

// Handle type. In C++ language the iterface type is used.
typedef IShred* SHREDHANDLE;

#ifdef __cplusplus
#   define EXTERN_C     extern "C"
#else
#   define EXTERN_C
#endif // __cplusplus


EXTERN_C
SHREDAPI 
SHREDHANDLE 
WINAPI 
GetShred(void);

libheader.cpp

#include libheader.h
class IShredLib: public IShred {
    int GetStringSize(char*);
    void Release();
};

int IShredLib::GetStringSize(char* s)
{
    return strlen(s);
}
void IShredLib::Release()
{
    delete this;
}
#pragma comment(linker, "/export:GetShred=_GetShred@0")
SHREDAPI SHREDHANDLE APIENTRY
GetShred()
{
    return new IShredLib;
}

并在main.cpp exe项目中:

#include "libheader.h"

#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    IShred* itf = ::GetShred();


    _getch();
    return 0;
}

现在我为什么要这个? 错误1错误LNK2001:未解析的外部符号__imp__GetShred @ 0 我还将SHREDLIB_EXPORTS定义为VS .dll项目属性中的c ++预处理器定义。

0 个答案:

没有答案