当前位置:首页 » 编程语言 » c语言dll怎么写
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言dll怎么写

发布时间: 2022-09-24 06:05:14

c语言写的程序怎么样生成.dll文件

这个过程和C语言怎么写没什么大关联,主要是编译器的编译设置,IDE对内置的编译核心进行了相应的设置,才将代码转换成DLL而不是EXE,这个问题我纠结了好久,最终还是妥协了IDE,在IDE里选择建立DLL工程,IDE在编译这个工程的时候就会自动进行相应的设置(准确的说是应用了DLL相关的命令)这样,编译出来的就是DLL了.

❷ dll文件如何用C语言生成

用 vc 6.0 下的cl.exe 和 link.exe工具,请读下文:
声明:下面这篇文章不是我写的,源自:一个叫,有容乃大 的博客

如何手工编写动态链接库(windows dll)

1.本例介绍在命令行(Console)环境下制作dll的方法
2.读者动手前,请确保在windows中安装有编译、链接工具和必要的函数库文件。
3.本例使用C语言实现.
4.本例中使用路径均为我机器上的绝对路径,读者需根据实际情况调整。

工具要求:

Microsoft的编译器cl.exe
MIcrosoft链接器link.exe

dll制作步骤:
1.编写dll函数实现源代码hello.c

#include

int say_hello(char* name)
{
printf( "hello %s\n ", name);
return 1;
}

2.编写dll函数输出定义文件hello.def.

LIBRARY hello
EXPORTS
say_hello @1

3.编译dll源码,生成dll,lib文件.

3.1 新建命令行窗口
3.2 设置PATH �0�7 INCLUDE �0�7 LIB 3个环境变量.

SET PATH=K:\vcnet\vc7\bin;%PATH%
SET INCLUDE=K:\vcnet\vc7\include;%INCLUDE%
SET LIB=K:\vsnet\Vc7\lib;%LIB%

3.3 编译hello.c

cd K:\Source\dllsample (hello.c和hello.def所在目录)
cl /c hello.c

3.4 链接hello.obj,生成hello.dll,hello.lib两个文件.

link /def:hello.def /dll hello.obj

4.测试dll函数.

4.1 编写测试代码 test.c

extern int say_hello(char* name);
int main(int argc,char** argv)
{
say_hello( "robbie ");
return 0;
}

4.2 编译测试代码test.c

cl /c test.c

4.3 链接test.obj和 hello.lib,生成可执行文件test.exe

link test.obj hello.lib

4.4 运行test.exe,屏幕输出:

hello robbie

至此,一个dll构造完毕.

下面是我自己的一点补充:
如果要在c++下,或者win32 mfc下使用标准c写的dll,必须把上面的声明
extern int say_hello(char* name);改成:extern "C " int say_hello(char* name);

❸ c怎么生成dll文件

在VC++中选择新建一个Win32 Dynamic-Link Library。需要建立一个c/c++ head file和一个c/c++ source file并加入工程。头文件中内容为输出函数的声明,源文件中内容为DllMain函数和输出函数的定义。下面是一个最简单的例子。

//dlldemo.h
#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif

EXPORT void CALLBACK DllFoo(void) ;

//dlldemo.c
#include <windows.h>
#include "dlldemo.h"

int WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
return TRUE ;
}

EXPORT void CALLBACK DllFoo(void)
{
MessageBox(NULL,TEXT("This function is exported from a DLL"),TEXT("DllFoo"),MB_OK) ;
return ;
}
头文件预处理中的__declspec是微软增加的“C扩展类存储属性”(C Extended Storage-Class Attributes),它指明一个给出的实例被存储为一种微软特定的类存储属性,可以为thread,naked,dllimport或dllexport. [MSDN原文:The extended attribute syntax for specifying storage-class information uses the __declspec keyword, which specifies that an instance of a given type is to be stored with a Microsoft-specific storage-class attribute (thread, naked, dllimport, or dllexport).] 输出函数必须指明为CALLBACK。 DllMain是dll的入口点函数。也可以不写它。DllMain必须返回TRUE,否则系统将终止程序并弹出一个“启动程序时出错”对话框。 编译链接后,得到动态链接库文件dlldemo.dll和输入库文件dlldemo.lib。

❹ 用C语言可以写dll文件吗应该怎么写呢

dll文件?

把C写在TXT(记事本里)然后把后缀TXT改成DLL,嘿嘿

不知道对不对,楼下的你来回答!

❺ C语言程序怎么编译成dll文件供其他语言调用

C程序编译成dll文件只不过是在要公开的接口函数声明前面加上几个特定的修饰符而已。
下面是个例子,用dev-cpp建了个dll的默认文档
/*dll.h文件*/
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

DLLIMPORT void HelloWorld (void);

#endif /* _DLL_H_ */
/*dllmain.c文件*/
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
DLLIMPORT void HelloWorld ()
{
MessageBox (0, "Hello World from DLL!/n", "Hi", MB_ICONINFORMATION);
}

BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}

具体请参考jilei08124的CSDN博客

❻ c语言写dll动态链接 简单例子

先用vc6建立一dll工程"dlltest"

// dlltest.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "dlltest.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticMoleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//

/////////////////////////////////////////////////////////////////////////////
// CDlltestApp

BEGIN_MESSAGE_MAP(CDlltestApp, CWinApp)
//{{AFX_MSG_MAP(CDlltestApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlltestApp construction

CDlltestApp::CDlltestApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDlltestApp object

CDlltestApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDlltestApp initialization

BOOL CDlltestApp::InitInstance()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}

return TRUE;
}

//myfunc.cpp
#include "stdafx.h"

extern "C" __declspec(dllexport)
int WINAPI func_test1(int a,int b)
{
int c;

c=a+b;

return c;
}

; dlltest.def : Declares the mole parameters for the DLL.

LIBRARY "dlltest"
DESCRIPTION 'dlltest Windows Dynamic Link Library'

EXPORTS
; Explicit exports can go here
func_test1 @1

❼ c语言怎么调用dll文件

1、新建DLLTest文件夹,在该文件夹中新建source文件夹。

注意事项:

C语言能以简易的方式编译、处理低级存储器。C语言是仅产生少量的机器语言以及不需要任何运行环境支持便能运行的高效率程序设计语言。

❽ 如何用VC编写dll文件

VC编写dll文件

1、VC++->New->Project->Win32 Dynamic_Link Library;

2、选择创建一个空工程;

首先DLL需要一个头文件,所以新建一个CC++ Header File

这个头文件中必须包含想要输出的变量和函数,头文件必须定义用于输出的任何符号和数据结构。

例如 :

证明调用动态链接库里的 Add函数成功。

❾ c语言写的程序怎么样生成.dll文件

创建一个vc项目,同时创建关联一个动态链接库项目最后生成的就是一个dll与exe文件,运行时缺一不可

❿ 用C语言编写DLL

使用微软的vc或者vs,用c语言编程就是了。创建工程的时候会问你是创建什么样的工程,比如控制台程序,mfc程序,动态库(dll)等。
c和c++只是编程语言,动态库依赖于操作系统,在哪个平台下就依赖于哪个平台的动态库生成工具。linux下是so文件,用gcc编译器就可以生成。