动态链接库 初识dll

来源:互联网 发布:java vs python 编辑:程序博客网 时间:2024/06/01 10:40

一、创建dll文件

1.vs2013新建项目,模版Visual C++下的win32 控制台应用中的Dll类型,创建空项目。

2.头文件添加新建项 .h文件,源文件添加 .cpp文件。

3.头文件:

<span style="font-size:24px;">extern "C" int _declspec (dllexport) add(int x, int y, int z);</span>

源文件:

<pre name="code" class="html">
<span style="font-size:24px;">#include "abc.h"int _declspec (dllexport) add(int x, int y, int z){<span style="white-space:pre"></span>return x + y + z;}</span>

4.解决方案配置为Release,再编译,输出成品可以在其他计算机直接运行使用。

二、在unity中引用

1.微软自带的dllC:\Windows\System32中能被Unity调用,加.dll后缀。

2.自己生成的dll文件C:\Windows\System32没调用成功,须在Unity中Assets下创建Plugins文件夹下才能被Unity调用,不加.dll后缀。

<span style="font-size:24px;">using UnityEngine;using System.Collections;using System;using System.Runtime.InteropServices;//嵌入调用操作系统功能//U3D可以控制整个计算机public class TestDll : MonoBehaviour {////指明调用的系统链接库//[DllImport("user32.dll")]////调用win操作系统,消息窗//public extern static int MessageBox (int Hwnd, string text, string Caption, int iType);//自建的abc.dll[DllImport("abc")]extern static int add (int x, int y, int z);// Use this for initializationvoid Start () {//MessageBox(0, "Hellow World", "One", 0);print (add (2,4,6).ToString());}// Update is called once per framevoid Update () {}}</span>


三、调用动态链接库可与操作系统文件、硬件接口控制交互。

软件开发时,要将自己的文件(如 .dll)放在用户系统中,可以将安装程序打包,安装向导时放到系统指定目录。

0 0