如何在C#中使用win32 API

A.N*_*R.I -3 c# winapi multithreading mutex semaphore

如何将win32连接到C#并使用win32函数创建信号量,互斥量等.

Pau*_*sik 11

您不需要使用Win32 API来使用互斥锁或信号量.他们已经内置了.NET类,并且自从Mutex版本为1.1,而Semaphore版本为2.0:

一些.NET内部表明Mutex类实际上使用Win32 API:就像一个快速的样品,下面的代码是从.NET中的Threading.Mutex类OpenMutex和ReleaseMutex方法反编译源.它使用DllImport属性和extern关键字来访问kernel32.dll中的函数

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern SafeWaitHandle OpenMutex(int desiredAccess, bool inheritHandle, string name);

[DllImport("kernel32.dll", CharSet=CharSet.None)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
internal static extern bool ReleaseMutex(SafeWaitHandle handle);
Run Code Online (Sandbox Code Playgroud)

你可以使用这些片段来进行我的想法,并且在这个方便的网站上可以获得更多信息:http://www.pinvoke.net/index.aspx