怎样在动态链接库中输出模板函数

//怎样在动态链接库中输出模板函数?

//VC6.0

//mydll.h

#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif

 

template <class T>
extern MYDLL_API void __stdcall swap(T &t1,T &t2);

 

extern "C"
{
MYDLL_API int __stdcall avg(int x,int y);
}

 

// MYDll.cpp : Defines the entry point for the DLL application.
 

#include "stdafx.h"
#include "MyDll.h"

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    switch (ul_reason_for_call)
 {
  case DLL_PROCESS_ATTACH:
  case DLL_THREAD_ATTACH:
  case DLL_THREAD_DETACH:
  case DLL_PROCESS_DETACH:
   break;
    }
    return TRUE;
}

 

//必须将模板函数实例化再输出
extern MYDLL_API void __stdcall swap(int &t1,int &t2)
{ int t;
 t=t1;
 t1=t2;
 t2=t;
};

extern MYDLL_API void __stdcall swap(double &t1,double &t2)
{ double t;
 t=t1;
 t1=t2;
 t2=t;
};

 

int __stdcall avg(int x,int y)
{ return (x+y)/2;
};

 

 

 

//测试程序

#include "stdafx.h"
#include <stdio.h>

#include "mydll.h"//请指明具体路径
#pragma comment(lib,"mydll.lib")//请指明具体路径

int main(int argc, char* argv[])
{
 int a=100;
 int b=20;
 int c;
 c=avg(a,b);

double x=5.5;

double y=3.2;

swap(a,b);//void swap(int &,int &)

swap(x,y);//void swap(double &,double &)

 printf("a=%d,b=%d,c=%d/n",a,b,c);

printf("x=%g,y=%g/n",x,y);

 

 return 0;
}

 

//注意:静态链接的话c++容易

//其他语言可用动态链接,参照以下:

//typedef void (__stdcall *SWAP)(int&,int&);
//HMODULE hlib=LoadLibrary("d://My Documents//MyProjects//DllTest//Debug//dlltest.dll");
//SWAP swap=(SWAP)GetProcAddress(hlib,"?swap@@YGXAAH0@Z");//函数名C++包装

//int a=1,b=2;
//swap(a,b);

//FreeLibrary(hlib);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值