开发者

Multi-Threading Pinvoked DLLs in managed environment. Is it possible?

开发者 https://www.devze.com 2022-12-18 17:52 出处:网络
I have a Vendor.DLL (Native DLL, written in C++) that exposes many methods. Typically Vendor.DLL opens Vendor proprietary files, returns handles and allows more Read/Write operation on those files. Ve

I have a Vendor.DLL (Native DLL, written in C++) that exposes many methods. Typically Vendor.DLL opens Vendor proprietary files, returns handles and allows more Read/Write operation on those files. Vendor.DLL supports multi-threading (when called from unmanaged code/COM).

If I expose Pinvoked method(s) from Vendor DLL, say

PinvokedVendor.DLL

[System.Runtime.InteropServices.DllImp开发者_StackOverflow中文版ortAttribute("Vendor.dll", EntryPoint = "SomeVendorMethod")]
            public static extern int SomeVendorMethod(uint param1, ref SomeVendorDataStruct pData);

How to ensure that this wrapper class is thread safe? Is it even thread safe when called from ASP.NET? What are my options?

Thanks in advance.


A call its a call its a call. Thread safety derives from the implementation of the vendor DLL and it's interface specifications. P-Invoke has nothing to do with it.


From the perspective of the vendor.dll, the PInvoke call won't look any different than a normal native method call. It shouldn't have any effect on the thread safety of their code. Given that the PInvoke calls are all static it is fine to call them from multiple threads within your code. Assuming of course you use the resulting data in a manner that is safe as prescribed by vendor.dll


Would making your code [MTAThread] instead of [STAThread] be useful in the approach to using pinvoke and threading in relation to COM? Just a thought...

Hope this helps, Best regards, Tom.

0

精彩评论

暂无评论...
验证码 换一张
取 消