首页 > 编程知识 正文

vs2015如何生成dll文件,vs2015怎么生成dll

时间:2023-05-05 18:21:22 阅读:242793 作者:2432

近来学习制作Dll文件,看了几个视频教程,看了网上的例子,看了msdn上的例子。现在做个总结,以便来日回顾,同时也希望以大家相互交流学习。

注意1:用 method 1 named "Using Load - Time Dynamic Linking" 调用dll文件,需要将相应的.lib添加到资源文件中,另外dll文件的源文件(two kinds method of create Dll.cpp和dllmain.cpp)需要添加到现在调用文件的源文件中,感觉比较麻烦。注意2:用method 2 named "Using Run - Time Dynamic Linking"调用dll文件,需要用到“two kinds method of create Dll.dll“的绝对路径,即:"G:\软件学习练习空间\windows program\two kinds method of create Dll\Debug\two kinds method of create Dll.dll"。

1.调用程序如下。

// two kinds method of call Dll file.cpp: 定义控制台应用程序的入口点。//#include "stdafx.h"#include <windows.h>#include<iostream>#include <stdio.h>//method 1 named "Using Load - Time Dynamic Linking"//Because this example calls the DLL function explicitly, //the module for the application must be linked with//the import library Myputs.lib.using std::cout;using std::endl;extern "C" int __cdecl mySub1(int x,int y); // a function from a DLLextern "C" int __cdecl mySub2(int x,int y); // a function from a DLLint main2(void);int main(VOID){std::cout << "1. call " << "extern "C"int __cdecl mySub1(int x,int y) ="<< mySub1(5,3) << std::endl;std::cout << "2. call " << "extern "C" int __cdecl mySub2(int x,int y) =" << mySub2(5, 3) << std::endl;main2();return 1;}//method 2 named "Using Run - Time Dynamic Linking"//Because the program uses run - time dynamic linking, //it is not necessary to link the module with an import//library for the DLL.// A simple program that uses LoadLibrary and// GetProcAddress to access myPuts from Myputs.dll.typedef int(__cdecl *mySub)(int x,int y);//typedef int(__cdecl *mySub2)(int x, int y);int main2(void){HINSTANCE hinstLib;mySub ProcAdd1, ProcAdd2;BOOL fFreeResult, fRunTimeLinkSuccess1, fRunTimeLinkSuccess2= FALSE;// Get a handle to the DLL module.hinstLib = LoadLibrary(L"G:\软件学习练习空间\windows program\two kinds method of create Dll\Debug\two kinds method of create Dll.dll");// If the handle is valid, try to get the function address.if (hinstLib != NULL){ProcAdd1 = (mySub)GetProcAddress(hinstLib, "mySub1");ProcAdd2 = (mySub)GetProcAddress(hinstLib, "mySub2");// If the function address is valid, call the function.if (NULL != ProcAdd1){fRunTimeLinkSuccess1 = TRUE;cout << "typedef int(__cdecl *mySub)(int x,int y) ,mySub1=" << ProcAdd1(7, 3)<<endl;}if (NULL != ProcAdd2){fRunTimeLinkSuccess2 = TRUE;cout<<"typedef int(__cdecl *mySub)(int x,int y) ,mySub2="<<ProcAdd1(7, 2)<<endl;}// Free the DLL module.fFreeResult = FreeLibrary(hinstLib);}// If unable to call the DLL function, use an alternative.if (!fRunTimeLinkSuccess1)printf("mySub1 failedn");if (!fRunTimeLinkSuccess2)printf("mySub2 failedn");return 0;}

2. 在vs2017创建中创建dll文件“two kinds method of create Dll”后,生成了两个可以写源代码的.ccp文件。它 们  是 “ dllmain.cpp”和“two kinds method of create Dll.cpp ”,下面分别在这两个.ccp文件中写方法。

2.1 dll文件,在“dllmain.cpp ”写程序。

// dllmain.cpp : 定义 DLL 应用程序的入口点。#include "stdafx.h"#include <windows.h>#define EOF (-1)#ifdef __cplusplus // If used by C++ code,extern "C" { // we need to export the C interface#endif//__________method 1,create dll file in dllmain.cpp.write code as follow:__declspec(dllexport) int __cdecl mySub1(int a,int b){int c;c = a + b;return c;}#ifdef __cplusplus}#endif//__________method 1,create dll file in dllmain.cpp.write code as above:BOOL APIENTRY DllMain( HMODULE 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;}

2.2 Dll程序,在“ two kinds method of create Dll.cpp”中写Dll程序。

// two kinds method of create Dll.cpp: 定义 DLL 应用程序的导出函数。//#include "stdafx.h"#include <windows.h>#define EOF (-1)#ifdef __cplusplus // If used by C++ code,extern "C" { // we need to export the C interface#endif //__________method 2,create dll file in dllmain.cpp.write code as follow:__declspec(dllexport) int __cdecl mySub2(int a, int b){int c;c = a + b;return c;}#ifdef __cplusplus}#endif//__________method 2,create dll file in dllmain.cpp.write code as above:

3. 输出结果如下:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。