四个步骤
1.创建C++ Win32项目动态库dll
2.在Win32项目动态库中添加 外部依赖项 lib头文件和lib库
3.导出C接口
4.c#调用c++动态库
开始你的表演...
①创建一个空白的解决方案,在解决方案中添加 Visual C++ , Win32 项目
空白解决方案的创建:
添加Visual C++ , Win32 项目
这样一个c++的动态库dll就创建好了,它最终会生成一个dll文件,供c#调用的。
②c++动态库dll引用静态库lib
先将lib头文件和lib库文件复制粘贴到项目目录下
和硬件相关的lib文件一般是分x86 和 x64的
添加外部依赖项 头文件.h :工程---属性---配置属性---c/c++---常规---附加包含目录:加上头文件存放目录。
注意:把配置的Debug和Release 平台的Win32(x86) 和 x64都记得选上路径。
添加引用lib库:工程---属性---配置属性---链接器---常规---附加库目录:加上lib文件存放目录。
注意:把配置的Debug和Release 平台的Win32(x86) 和 x64 都对应选上,一般lib库会有x86 和 x64 ,需要对应选择路径。
添加附加依赖项 lib文件名
:工程---属性---配置属性---链接器---输入---附加依赖项:加上lib文件名。
注意:把配置的Debug和Release 平台的Win32(x86) 和 x64 都对应加上lib文件名
这样算是把lib库引进来了。
添加Win32Project1.h头文件。
③导出C接口
在源文件Win32Project1.cpp中引入lib头文件并继承lib库,实现里面的方法函数。
#include "stdafx.h"
#include "IntelSRUtility.h"
#include "Win32Project1.h"
class IntelSR : SRUtility
{
public:
IntelSR() {};
~IntelSR() {};
BOOL ToggleSRRegkey(DWORD enable) {
return toggleSRRegkey(enable);
};
BOOL ConfigureSuperResolutionMode(DWORD mode) {
return configureSuperResolutionMode(mode);
};
};
BOOL SetSuperRStatus(DWORD enable) {
IntelSR *s_IntelAPI = new IntelSR();
BOOL rm = s_IntelAPI->ToggleSRRegkey(enable);
delete s_IntelAPI;
return rm;
}
BOOL SetSuperRMode(DWORD mode) {
IntelSR *s_IntelAPI = new IntelSR();
BOOL rm = s_IntelAPI->ToggleSRRegkey(mode);
delete s_IntelAPI;
return rm;
}
在头文件Win32Project1.h中导出c接口
#pragma once
#define PLUGIN_API __declspec(dllexport)
typedef int BOOL;
typedef unsigned long DWORD;
extern "C" PLUGIN_API BOOL SetSuperRStatus(DWORD enable);
extern "C" PLUGIN_API BOOL SetSuperRMode(DWORD mode);
重新生成解决方案,在项目根目录找到dll文件
这样你拿到C++的动态库就可以用c#调用了
④c# 调用c++动态库。
把c++动态库dll拷贝到c#的项目bin目录下面,在c#中调用。
方法一:
///
/// SR开关
///
/// 1将在驱动程序中启用SR, 0将禁用SR
/// 成功时返回0
[DllImport("Win32Project1.dll", EntryPoint = "SetSuperRStatus", CallingConvention = CallingConvention.Cdecl)]
public static extern int SetSuperRStatus(int enable);
///
/// SR模式
///
/// 0表示默认模式,1表示质量模式(低性能),2表示混合模式,3表示性能模式(低质量)
/// 成功时返回0
[DllImport("Win32Project1.dll", EntryPoint = "SetSuperRMode", CallingConvention = CallingConvention.Cdecl)]
public static extern int SetSuperRMode(int mode);
方法二:
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace InvokeIntel
{
public class InvokeIntelAPI
{
private DllInvoke dll;
public DllInvoke DLL
{
get
{
if (dll == null)
{
dll = new DllInvoke(Path.Combine(Environment.CurrentDirectory, "Win32Project1.dll"));
}
return dll;
}
set
{
value = dll;
}
}
public delegate int SetSuperRStatus(int enable);
public delegate int SetSuperRMode(int mode);
public InvokeIntelAPI()
{
try
{
SetSuperRMode mode = (SetSuperRMode)DLL.Invoke("SetSuperRMode", typeof(SetSuperRMode));
mode(1);
SetSuperRStatus status = (SetSuperRStatus)DLL.Invoke("SetSuperRStatus", typeof(SetSuperRStatus));
status(1);
}
catch (Exception)
{
throw;
}
}
}
public class DllInvoke
{
[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(String path);
[DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);
[DllImport("kernel32.dll")]
private extern static bool FreeLibrary(IntPtr lib);
private IntPtr hLib;
public DllInvoke(String DLLPath)
{
hLib = LoadLibrary(DLLPath);
}
~DllInvoke()
{
FreeLibrary(hLib);
}
//将要执行的函数转换为委托
public Delegate Invoke(String APIName, Type t)
{
IntPtr api = GetProcAddress(hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
}
}
}
方法三:指定环境变量。
///
/// SR开关
///
/// 1将在驱动程序中启用SR, 0将禁用SR
/// 成功时返回0
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
[DllImport("Win32Project1.dll", EntryPoint = "SetSuperRStatus", CallingConvention = CallingConvention.Cdecl)]
public static extern int SetSuperRStatus(int enable);
///
/// SR模式
///
/// 0表示默认模式,1表示质量模式(低性能),2表示混合模式,3表示性能模式(低质量)
/// 成功时返回0
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
[DllImport("Win32Project1.dll", EntryPoint = "SetSuperRMode", CallingConvention = CallingConvention.Cdecl)]
public static extern int SetSuperRMode(int mode);