如何在C中编写DLL文件? [英] How to write a DLL file in C?
问题描述
如何在 DLL文件 /en.wikipedia.org/wiki/C_%28programming_language%29rel =noreferrer> C ?
我在网上看,但我总是获取 C ++ 的教程,而不是C.我想写我的第一个C中的DLL文件我该怎么做?一个简单的例子是什么?
让我们开始你的第一个DLL:
- 启动Visual Studio .NET。
- 转到菜单文件 - > 新建
- 选择Visual C ++ Project,从
模板
中选择Win32项目
。 - 给你的项目名称。这将是您最终DLL文件的名称。
- 按OK。
- 从
应用程序类型选择DLL
(在应用程序设置
选项卡中) - 检查
空项目
并按完成
。
您需要附加一个空的源文件到空白的项目:
- 启动解决方案资源管理器(如果没有显示)。
- 右键单击到
源文件
,添加 - > 添加新项目,然后选择C ++ File
并给它名称。 - 按
打开
。
在打开的窗口中,输入以下代码:
#include< ; stdio.h中>
externC
{
__declspec(dllexport)void DisplayHelloFromMyDLL()
{
printf(Hello DLL.\\\
);
}
}
-
code> __ declspec(dllexport)是一个强制性的前缀,使外部应用程序可以使用DLL功能。
-
externC
(带大括号,用于范围界定)显示括号内的所有代码均可从外部文件获取。虽然代码将编译即使没有这个语句,在运行时,你会得到一个错误。 (我将此作为您的实验)。
构建此应用程序,您的DLL文件已准备就绪。 p>
请参阅 演练:创建和使用动态链接库 有关如何添加和填充的更多信息。
How do I write a DLL file in C?
I was looking online, but I always get tutorials for C++, not C. I want to write my first DLL file in C. How can I do that? What would a short example be?
Let's get you started on your first 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 Settings
tab). - Check
Empty Project
and pressFinish
.
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 Item and then selectC++ File
and give the name to it. - Press
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).
Build this application and your DLL file is ready.
Refer to Walkthrough: Creating and Using a Dynamic Link Library for more information on how to do addition and stuff.
这篇关于如何在C中编写DLL文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!