如何书写动态连接库

来源:互联网 发布:电脑软件停止工作 编辑:程序博客网 时间:2024/04/30 20:20
1.DIL文件代码如下
// TestDll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

extern "C" __declspec( dllexport ) int  AddFunction(int x, int y);
extern "C" __declspec( dllexport ) int  JanFunction(int x, int y);
BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    return TRUE;
}

int AddFunction(int x, int y)
{
   return x+y;
}
int JanFunction(int x,int y)
{
    return x-y;
}
2.如何调用
#include <iostream>
#include <conio.h>

using namespace  std;
extern "C" __declspec(dllimport) int  AddFunction(int x, int y);
extern "C" __declspec(dllimport) int  JanFunction(int x, int y);

int main(void)
{
   int x;
    int y;
    cout<<"请输入X的值:";
    cin>>x;
    cout<<"请输入Y的值:";
    cin>>y;
    int sum=AddFunction(x,y);
    cout<<"x+y="<<sum<<endl;
    int dum=JanFunction(x,y);
    cout<<"x-y="<<dum<<endl;
    getch();
    return 0;
}
这样就OK了
原创粉丝点击