VC2010 編寫DLL並調用;


一  、 新建DLL項目

文件- 新項目-win32控制台

應用類型選擇DLL,附件選項 選擇導出符號 建立了 dllapp項目。

在DLLapp.h  添加DLLAPP_API int Max(int a ,int b );

//  下列 ifdef 塊是創建使從 DLL 導出更簡單的
//  宏的標准方法。此 DLL 中的所有文件都是用命令行上定義的 DLLAPP_EXPORTS
//  符號編譯的。在使用此 DLL 的
//  任何其他項目上不應定義此符號。這樣,源文件中包含此文件的任何其他項目都會將
//  DLLAPP_API 函數視為是從 DLL 導入的,而此 DLL 則將用此宏定義的
//  符號視為是被導出的。
#ifdef DLLAPP_EXPORTS
#define DLLAPP_API __declspec(dllexport)
#else
#define DLLAPP_API __declspec(dllimport)
#endif

//  此類是從 dllapp.dll 導出的
class DLLAPP_API Cdllapp {
public:
    Cdllapp( void);
     //  TODO: 在此添加您的方法。
     int Max( int a , int b );
};

extern DLLAPP_API  int ndllapp;

DLLAPP_API  int fndllapp( void);
DLLAPP_API  int Max( int a , int b );

在dllapp中添加函數

DLLAPP_API int Max(int a ,int b )
{
 if (a>b)return a;
 else return b;
   
}

 

//  dllapp.cpp : 定義 DLL 應用程序的導出函數。
//

#include  " stdafx.h "
#include  " dllapp.h "


//  這是導出變量的一個示例
DLLAPP_API  int ndllapp= 0;

//  這是導出函數的一個示例。
DLLAPP_API  int fndllapp( void)
{
     return  42;
}
DLLAPP_API  int Max( int a , int b )
{
     if (a>b) return a;
     else  return b;
    
}
//  這是已導出類的構造函數。
//  有關類定義的信息,請參閱 dllapp.h
Cdllapp::Cdllapp()
{
     return;
}

 

編譯通過后找到生成的 lib 和 dll文件

新建一個項目來使用這個dllapp.MAX(int ,int )函數

LINKER 輸入中添加 dllapp.lib

VC++目錄添加 include 、lib等路徑和調用其他DLL一樣,系統PATH 添加dllapp.dll 路徑

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM