C++ dll编写与加载

2020-06-09  本文已影响0人  炫子_260f

1 环境

windows 10
VisualStudio 2015

2 dll项目

2.1 创建项目

打开VS2005,添加新项目。如图所示

image.png

这里选择“Win32 控制台应用程序”即可。选择工程的位置,以及添加工程的名称“ConsoleApplication3”,然后按“确定”。

接着,出现“Win32 应用程序向导”,按“下一步”即可。如图


image.png

然后,出现下面的对话框。

image.png
这里应用程序类型选择 DLL
附加选项选择 空目录
便创建了简单DLL工程。

解决方案显示框内容如下:

image.png

这是一个空项目

2.2 DLL开发

在创建了DLL工程之后,便可以进行开发。步骤如下:

首先,创建头文件dll.h,如图:


image.png

右击 - 添加 - 新建项

image.png

添加了dll.h 文件,同样方式可添加.cpp文件。

添加后,其解决方案显示框内容如下:

image.png
dll.h代码
#pragma once

extern "C" _declspec(dllexport) int __stdcall add(int a, int b);

dll.cpp代码

int __stdcall add(int a, int b) {
    return a + b;
}
再下面是一些固定写法了
定义 def文件,如下图:
image.png
ConsoleApplication3.def 代码:
LIBRARY

EXPORTS

add @ 1

按照add @ 1这样的格式,修改或添加需要导出的方法

targetver.h
#pragma once

// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。

// 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将
// 将 _WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。

#include <SDKDDKVer.h>
#pragma once

Stdafx.cpp 与 Stdafx.h, 用于定义常用的头文件:

Stdafx.h

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件

#pragma once

#include "targetver.h"

#define WIN32_LEAN_dnfa_MEAN             // 从 Windows 头中排除极少使用的资料
// Windows 头文件: 
#include <windows.h>

Stdafx.cpp

#include "Stdafx.h"
DllMain.cpp,如下图:
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "Stdafx.h"

BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }

    return TRUE;
}

2.4 生成DLL

右击 - 生成 ,就可以生成dll了


image.png
2.5 调试Dll

在同一个解决方案下,添加一个c#项目,添加一个ConsoleApplication3.cs调用文件,项目结构如下:

image.png

修改dll 属性:


image.png

ConsoleApplication3.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public static class ConsoleApplication3
    {
        [DllImport("ConsoleApplication3.dll", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        private static extern int add(int a,int b);

        private static object obj = new object();
        public static int addDLL(int a,int b)
        {
            int res = 0;
            lock (obj)
            {
                try
                {
                    int aptr = add(a, b);
                    res = aptr;
                }
                catch (Exception ex)
                { }
            };
            return res;
        }
    }
}

调用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;
            int r = ConsoleApplication3.addDLL(a, b);
            Console.WriteLine(r);
        }
    }
}

控制台输出:3。

写得比较简单,有部分可能说得不对,欢迎指正

上一篇 下一篇

猜你喜欢

热点阅读