【C# C++】C#中调用msvcr100.dll中的_beginthreadex函数

 

msvcr100.dll是VS2010的C运行时库DLL, _beginthreadex开启子线程的函数就在这个DLL里面实现

unsigned long _beginthreadex(
    void *security, //安全属性
    unsigned stack_size, //线程栈大小
    unsigned ( __stdcall *start_address )( void * ), //线程函数
    void *arglist, //线程函数的参数 */
    unsigned ini tflag, //标志:0或CREATE_SUSPEND
    unsigned *thrdaddr ); //线程ID
要想在C#中使用这个函数,必须:引入他所在的C运行时库DLL, 即msvcrXX.dll, XX代表版本,VS1020为100,
还要知道C++中的数据类型和C#中数据类型的对照,可以百度:C# C++数据类型对照表

void* == IntPtr
unsigned == uint
unsigned * == ref uint
函数指针 == 委托
所以, 如下:

复制代码
 1 private   delegate  uint  StartAddress(IntPtr  param);
 2 
 3 [DllImport("msvcr100.dll")]  //vs2010的版本
 4 static  extern  uint  _beginthreadex(
 5     IntPtr           psa,         //安全属性
 6     uint             stacksize,   //栈大小
 7     StartAddress     start,       //线程函数
 8     IntPtr           param,       //线程函数参数
 9     uint             flags,       //编辑,0或CREATE_SUSPEND(C++)
10     ref      uint    threadID     //线程ID
11 );
12 
13 private  void button_new_thread_Click(object  sender, EventArgs e)
14 {
15     uint   unThreadID = 0;
16     StartAddress   start = new StartAddress(show);  //委托对象,函数指针
17     _beginthreadex((IntPtr)0, 0, start, (IntPtr)0, 0, ref unThreadID);
18 }
19 
20 //委托函数
21 private  uint  show(IntPtr  param)
22 {
23     MessageBox.Show("这是子线程");
24     return 0;
25 }







复制代码

 

















posted on   崔好好  阅读(749)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示