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

22 篇文章 0 订阅
14 篇文章 0 订阅

    关于Windows API 的线程函数CreateThread的使用MSDN有如下说法:

The CreateThread function creates a thread to execute within the address space of the calling process.

HANDLE CreateThread(
  LPSECURITY_ATTRIBUTES lpThreadAttributes,  // pointer to security attributes
  DWORD dwStackSize,                         // initial thread stack size
  LPTHREAD_START_ROUTINE lpStartAddress,     // pointer to thread function
  LPVOID lpParameter,                        // argument for new thread
  DWORD dwCreationFlags,                     // creation flags
  LPDWORD lpThreadId                         // pointer to receive thread ID
);

      

    关于lpStartAddress这个函数的原型MSDN又有如下说法:

The ThreadProc function is an application-defined function that serves as the starting address for a thread. Specify this address when calling theCreateThread or CreateRemoteThread function. TheLPTHREAD_START_ROUTINE type defines a pointer to this callback function.ThreadProc is a placeholder for the application-defined function name.

DWORD WINAPI ThreadProc(
  LPVOID lpParameter   // thread data
);

    其最重要的核心意思是说CreateThread是Windows创建线程的方式,并且里面需要一个全局的或者静态的函数做为线程的执行函数(不能为class的非静态成员函数),但是,我们回想一下Java里面的Thread类的使用,你就会发现,Java里面只要实现runable接口,就可以创建线程了,自然地有多态,如果使用全局函数或者static函数,自然失去面向对象的多态性质。

    我想,大家看到这里面肯定会问,为什么CreateThread里面不可以使用类的成员函数作为线程函数呢?学过C++的同学都知道,C++的成员函数至少背负了一个this指针,也就是说下面的代码:

class	CBase{
public:
	void	Hello();
};

void CBase::Hello(){
	cout<<"Hello,World!"<<endl;
}

int main(){
	CBase	base;
	base.Hello();
}

这个CBase::Hello()实现上在会被编译编译成语意等价的CBase::Hello(CBase* this);当然,不同的编译器会有不同的处理方式,比如GCC和VS对这个Hello的处理方式是不同的(但是,在语意上是等价的)。

    但是,这里只是说明了原理,原理的东西是为了便于理解,但是,难以转化成真正的代码,真正的code还关系到更多的细节,为了便 于大家理解,我将会在下一节中说明VS平台对于this指针的处理方式。

在.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进行实现。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值