如何用C来写一个DLL文件? [英] How to write a DLL file in C?

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

问题描述

我怎样写一个中的 HTTP:/ /en.wikipedia.org/wiki/C_%28programming_language%29相对=nofollow>ç

How do I write a DLL file in C?

我一直在寻找在网上,但我总是得到的 C ++ ,不是C.我想用C编写我的第一个DLL文件,我怎么能这样做?将一个简单的例子是什么?

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?

推荐答案

让你开始你的第一个DLL:

Let's get you started on your first DLL:


  • 启动Visual Studio .NET。

  • 转到菜单​​中的文件的 - > 的 - > 项目

  • 选择Visual C ++项目,并从模板,选择 Win32项目

  • 给的名称到您的项目。这将是你最后的DLL文件的名称。

  • preSS确定。

  • 应用类型选择DLL(在应用程序设置设置页)。

  • 检查空项目和preSS 完成

  • 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:


  • 启动解决方案管理器(如果它不显示)。

  • 右键单击该源文件添加的 - > 添加新项的选择 C ++文件,并给予姓名。

  • preSS 打开

  • 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.

在打开的窗口中,输入以下code:

In the opened window, enter the following code:

#include <stdio.h>
extern "C"
{
    __declspec(dllexport) void DisplayHelloFromMyDLL()
    {
        printf ("Hello DLL.\n");
    }
}


  • __ declspec(dllexport)的是一个强制性的preFIX,这将使得DLL功能从外部应用程序。

    • __declspec(dllexport) is an obligatory prefix which makes DLL functions available from an external application.

      的externC(带牙套,为作用域)显示,括号内的所有code可从外部的文件。虽然code甚至会编译没有这个说法,在运行时,你会得到一个错误。 (我离开这个作为一个实验你)。

      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).

      建立这个应用程序和DLL文件已准备就绪。

      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天全站免登陆