如何在C中编写DLL文件? [英] How to write a DLL file in C?

查看:114
本文介绍了如何在C中编写DLL文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 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, select Win32 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 the Application Settings tab).
      • Check Empty Project and press 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 Item and then select C++ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆