教你用Windows API 写一个Thread类(不使用static哦)------(4)

本文将介绍如何利用Windows API编写一个Thread类,不使用static成员函数。重点在于如何在类的成员函数中设置ecx寄存器,以便调用成员函数。作者建议通过插入机器代码来实现这一目标,并提供了测试用例。由于涉及让数据执行的潜在风险,需要关闭数据执行保护(DEP)。关闭DEP的具体步骤也在文中提及。
摘要由CSDN通过智能技术生成

这一篇文章中主要是讲解怎么写代码把以上3篇文章的理论用起来,写出一个能用的Thread类(只是模型)

我在第3篇中提到了,要想把类的成员函数做Thread函数,一定要在一个时候把this指针赋值给ecx,问题是在类的成员函数里面是不太可能做这种事情的,因为如果你要让fun成为线程函数,你就需要在别的函数里面初始化ecx,这样感觉有点多余。我用的最简单的一个方法是写一段机器代码,在这个机器代码里面完成所有的工作:初始化ecx,跳转到fun函数………至于那段机器代码怎么来的我想,我们用Vs调试的时候有个显示机器代码的选项,可以用这个看到吧,呵呵。不说了,下面直接放代码。

 

//core.h
#ifndef __ZX_CORE_H__
#define __ZX_CORE_H__
#include <windows.h>

#ifndef interface
#define interface struct
#endif

#ifndef implement
#define implement :public
#endif

const static unsigned char	g_thread_proc[]=
{
	//------------parameter-----------------
	0x8B,0x44,0x24,0x04,				// mov         eax,dword ptr [esp+10h] 
	0x50,						// push        eax  

	//-----------this pointer-------------
	0xB9,0x00,0x00,0x00,0x00,   			// mov 	      ecx,0x12FF5C 

	//-----------call back function-------------
	
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在.NET 6.0中,可以使用Windows API来调用摄像头进行拍照和录制视频。具体步骤如下: 1. 引用相关命名空间:需要使用Windows API进行摄像头的调用,需要在代码文件中添加以下命名空间: ```csharp using System.Runtime.InteropServices; using System.Diagnostics; using System.Windows.Forms; ``` 2. 调用摄像头进行拍照:使用Windows API中的SendMessage方法指定命令来调用摄像头,如下所示: ```csharp [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam); const uint WM_CAP_START = 0x400; const uint WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; const uint WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; const uint WM_CAP_SAVEDIB = WM_CAP_START + 25; const int IDOK = 1; public void TakePhoto() { ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Windows\\System32\\amcap.exe"); // 打开摄像头软件 Process.Start(startInfo); IntPtr hWnd = IntPtr.Zero; while (hWnd.ToInt32() == 0) { hWnd = FindWindow(null, "AMCap"); // 查找摄像头软件的窗口句柄 Thread.Sleep(100); } SendMessage(hWnd.ToInt32(), WM_CAP_DRIVER_CONNECT, 0, 0); // 连接摄像头 SendMessage(hWnd.ToInt32(), WM_CAP_SAVEDIB, 0, 0); // 拍照 SendMessage(hWnd.ToInt32(), WM_CAP_DRIVER_DISCONNECT, 0, 0); // 断开连接摄像头 } ``` 3. 调用摄像头进行录制视频:同样是使用Windows API中的SendMessage方法指定命令来调用摄像头,如下所示: ```csharp [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam); const uint WM_CAP_START = 0x400; const uint WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; const uint WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; const uint WM_CAP_SET_PREVIEW = WM_CAP_START + 50; const uint WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52; const uint WM_CAP_SAVEDIB = WM_CAP_START + 25; const int IDOK = 1; public void RecordVideo() { ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Windows\\System32\\amcap.exe"); // 打开摄像头软件 Process.Start(startInfo); IntPtr hWnd = IntPtr.Zero; while (hWnd.ToInt32() == 0) { hWnd = FindWindow(null, "AMCap"); // 查找摄像头软件的窗口句柄 Thread.Sleep(100); } SendMessage(hWnd.ToInt32(), WM_CAP_DRIVER_CONNECT, 0, 0); // 连接摄像头 SendMessage(hWnd.ToInt32(), WM_CAP_SET_PREVIEW, 0, 0); // 设置预览 SendMessage(hWnd.ToInt32(), WM_CAP_SET_PREVIEWRATE, 66, 0); // 设置预览帧率 string fileName = "C:\\video.avi"; if (File.Exists(fileName)) File.Delete(fileName); SendMessage(hWnd.ToInt32(), WM_CAP_SAVEDIB, 0, fileName); // 开始录制 MessageBox.Show("按OK停止录制", "提示", MessageBoxButtons.OK); SendMessage(hWnd.ToInt32(), WM_CAP_SAVEDIB, 0, 0); // 停止录制 SendMessage(hWnd.ToInt32(), WM_CAP_DRIVER_DISCONNECT, 0, 0); // 断开连接摄像头 } ``` 需要注意的是,这两个方法都是在Windows环境下调用摄像头软件进行实现的,因此需要保证在调用的计算机上已经安装了能够调用摄像头的摄像头软件。如果想要在跨平台的环境中使用摄像头,可能需要使用其他的第三方库或者API进行实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值