首页 技术 正文
技术 2022年11月10日
0 收藏 328 点赞 5,097 浏览 1233 个字

Windows下的多线程

http://blog.csdn.net/ganpengjin1/article/category/2541791

使用C/C++建立DLL,环境VS2013

新建Win32工程,下一步,类型选择DLL

选中导出符号。不选空项目,让系统生成所需文件。

为了生成和使用的项目使用同一组头文件,一般使用这样的:

#ifdef MYLIB_EXPORTS
#define MYLIB_API __declspec(dllexport)
#else
#define MYLIB_API __declspec(dllimport)
#endif

MYLIB_API的位置:在class 后面,类名的前面

对于函数,申明和实现,在返回类型前面

对于全局变量,在extern后面,在变量类型前面。

DLL中使用回调函数。

在头文件中申明,比如 typedef  void (CALLBACK *yourfn)(int para);

在CPP中,一个初始化函数接受yourfn类型的函数指针

比如 :init(yourfn fn)

{

……

fn();//调用fn

}

外部模块调用DLL的时候,需要写一个类似yourfn的函数,

void myfn(int hi){……}

然后调用init(myfn);

C#调用DLL中的C函数

建立一个static类,

using System.Runtime.InteropServices;

调用形式

C#Stimulator项目>>>C/C++ DLL的生成和调用,Windows下的多线程

或者这样的:

 [DllImport("dlldemo.dll", EntryPoint = "PassString", CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
public static extern int PassString(string msg); [DllImport("dlldemo.dll", EntryPoint = "Power", CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
public static extern double Power(double x, int y);

在C#修饰符 ref 即表示“传址”,所以C++中的 int*对应于 C#中的ref int

传递数组:

在C++中添加函数

C#Stimulator项目>>>C/C++ DLL的生成和调用,Windows下的多线程

C#Stimulator项目>>>C/C++ DLL的生成和调用,Windows下的多线程

这样调用

C#Stimulator项目>>>C/C++ DLL的生成和调用,Windows下的多线程

C#调用DLL中C 回调函数

如果dll需要一个函数指针作为参数,你可以把一个c#的委托传给它,注意参数返回值对应就行

C#Stimulator项目>>>C/C++ DLL的生成和调用,Windows下的多线程

C#在调用时需要将委托声明为静态的类成员变量。因为委托是托管代码,会被垃圾回收机制回收,

如果该回调需要数组传参数,还需注意:

public delegate void CallbackDelegate([marshalAs(UnmanagedType.LPArray,SizeConst=8010)]byte[] buffer,int count); 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,906
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727