C语言 如何用C编写DLL文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13218824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a DLL file in C?
提问by user1386966
回答by askmish
Let's get you started on your first DLL:
让我们开始您的第一个 DLL:
- Start Visual Studio .NET.
- Go to menu File-> New-> Project.
- Select Visual C++ Project, and from the
Templates, selectWin32 Project. - Give the name to your project. This will be the name of your final DLL file.
- Press OK.
- Select DLL from
Application Type(In theApplication Settingstab). - Check
Empty Projectand pressFinish.
- 启动 Visual Studio .NET。
- 转到菜单File-> New-> Project。
- 选择 Visual C++ Project,然后从 中
Templates选择Win32 Project. - 为您的项目命名。这将是最终 DLL 文件的名称。
- 按确定。
- 从
Application Type(在Application Settings选项卡中)选择 DLL 。 - 检查
Empty Project并按Finish。
You need to attach an empty source file to the blank project:
您需要将一个空的源文件附加到空白项目中:
- Start Solution Explorer (if it's not displayed).
- Right click to the
Source Files, Add-> Add New Itemand then selectC++ Fileand give the name to it. - Press
Open.
- 启动解决方案资源管理器(如果未显示)。
- 右键单击
Source Files,添加->添加新项目,然后选择C++ File并为其命名。 - 按
Open。
In the opened window, enter the following code:
在打开的窗口中,输入以下代码:
#include <stdio.h>
extern "C"
{
__declspec(dllexport) void DisplayHelloFromMyDLL()
{
printf ("Hello DLL.\n");
}
}
__declspec(dllexport)is an obligatory prefix which makes DLL functions available from an external application.extern “C”(with braces, for scoping) shows that all code within brackets is available from “outside” the file. Although code will compile even without this statement, during runtime, you will get an error. (I leave this as an experiment for you).
__declspec(dllexport)是一个强制性前缀,它使 DLL 函数可从外部应用程序使用。extern “C”(带大括号,用于范围)显示方括号内的所有代码都可以从文件“外部”获得。尽管即使没有这个语句代码也能编译,但在运行时,你会得到一个错误。(我把它留作你的实验)。
Build this application and your DLL file is ready.
构建这个应用程序,你的 DLL 文件就准备好了。
Refer to Walkthrough: Creating and Using a Dynamic Link Libraryfor more information on how to do addition and stuff.
请参阅演练:创建和使用动态链接库以获取有关如何执行添加和内容的更多信息。

