Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148654
  • 博文数量: 33
  • 博客积分: 1494
  • 博客等级: 上尉
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-24 21:50
文章分类

全部博文(33)

文章存档

2014年(1)

2013年(1)

2012年(2)

2011年(3)

2010年(26)

我的朋友

分类:

2010-08-04 21:06:31

一、选择win32 dynamic library,输入project name(此处为example),点击ok

二、选择an empty dll project,点击finish

三、添加c++ source file------- Math.c

//Math.c----实现大数相乘

#include

#define  N 100

void exchange(int *a,  char *s)  

{

       int i;

       char digit;

       int len = strlen(s);

       for(i = 0; i < N; i ++)

              *(a + i) = 0;

       for(i = 0; i < len; i ++)

       {

        digit = *(s + i);

        *(a + len - 1 - i) = digit - '0';

       }

}

__declspec (dllexport) char* multiply(char *str1,char *str2) //此函数为要导出的函数

{

      

       int a[N],b[N],ix;

       int d[2*N];

       int *c =d;

       int i,j,len;

       char s[2*N];

       char *str =s;

             

       exchange(a,str1);

       exchange(b,str2);

      

       for (ix=0;ix<2*N;ix++)

       {

              d[ix]=0;

       }

 

 

       for(i=0;i

              *(c+i)=0;

      

       for(i=0;i

              for(j=0;j

                     *(c+i+j)+=*(a+i) * *(b+j);

             

 

              for(i=0;i

              {

                     *(c+i+1)+=*(c+i)/10;   

                     *(c+i)=*(c+i)%10;         

              }

             

      

             

              for (i=2*N-1;i>=0;i--)

              {

                     if (c[i]!=0)

                     {

                            break;

                     }

              }

             

              for (len=i;len>=0;len--)

              {

                     str[i-len]=c[len]+48;

              }

              str[i+1] = '\0';

             

             

              return str;

}

编译链接后再Debug目录下生成example.dll,即是我们所要的动态链接库。

四、调用

Debug目录下的example.dll拷贝到新建工程目录下(亦可使用绝对路径)

#include

#include

using namespace std;

 

typedef char* (*largeNum)(char *,char *);

int main()

{

       HINSTANCE hInstance = LoadLibrary("example.dll");

       if (hInstance==NULL)

       {

              cout<<"Can not find dll !"<

       }

       largeNum lgn = (largeNum)GetProcAddress(hInstance,"multiply");

       /*

       使用下面这句在创建dll文件时还要加入下面这个文件

Math.def

LIBRARY "example"

EXPORTS

multiply @2

*/

       //largeNum lgn = (largeNum)GetProcAddress(hInstance,(LPTSTR)2);

       if (lgn==NULL)

       {

 

              cout<<"Can not get the function !"<

       }

 

       cout<

       return 0;

}

运行结果如下所示:

阅读(1160) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~