Creating named Mutex in c# exe and Accessing it a dll in c++

Deadly 提交于 2019-12-12 01:35:10

问题


I have two exe's one in C# and other is a vc++ exe . Both of these exe need to access a file.

So I am planning to create a named mutex in c#. vc++ how can i access this named mutex. Can any one give me sample codes for this


回答1:


Something like this in the C++ code:

HANDLE hMutex = CreateMutex(NULL, FALSE, name);
if (hMutex == NULL) {
  // Handle failure.
}

If you need to know if the mutex already existed, check for hMutex != null && GetLastError() == ERROR_ALREADY_EXISTS.

The default ACL you get should be OK for cases with both processes in the same session, otherwise you will need to set an appropriate ACL.




回答2:


If you have created the mutex in c# , Your C++ code would be something like:

HANDLE mutexHandle = OpenMutex(SYNCHRONIZE,0,"NameOfYourMutex");
if(mutexHandle == 0) {
  //handle error
}


来源:https://stackoverflow.com/questions/2288654/creating-named-mutex-in-c-sharp-exe-and-accessing-it-a-dll-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!