Semaphore or SemmaphoreSlim not working in Linux with pinvoke

Copper Contributor

Hi,

I am trying to create a .NET standard wrapper which calls the originally implemented API methods from C++ library. So, in my C++ library there is a method which takes one of the parameter is Unix type Semaphore which is sem_t * and I need to call that API method from .NET standard wrapper code which I am using initially "Semaphore" which is giving Exception like System.InvalidOperationException: Type 'System.Threading.Semaphore has a ComVisible(false) parent 'System.threading.WaitHandle' in its hierarchy, therefore Query Interface calls for I Dispatch or class interfaces are disallowed."

 

I found some where in documentation that "Semaphore" uses COM Win32 object, so I used passed "SemmaphoreSlim" as an argument to API method instead of "Semaphore", this time it worked fine in Windows, but giving an exception in Linux "System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #2': Invalid managed/unmanaged type combination (Interfaces must be paired with Interface)."

 

So is there any way to pass to pinvoke by marshaling it?

please let me know.

 

Environment is .NET  Standard 2.0 and .NET core 3.1

C++ method

void myAsyccSemaphore(sem_t * event, int eventType)

2 Replies

Hi @mahesha10,

Thanks for posting your issue here.

However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
Best Regards,
Lan Huang

Hi @LanHuang 

 

Thanks for your reply. I have posted in Microsoft Q&A.