在托管环境中多线程p调用dll。是可能的吗?

时间:2022-05-05 20:59:10

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).

我有一个供应商。DLL(本机DLL,用c++编写),它公开许多方法。通常供应商。DLL打开供应商专有文件,返回句柄并允许对这些文件进行更多的读/写操作。供应商。DLL支持多线程(当从非托管代码/COM调用时)。

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

如果我从供应商DLL中公开了pinvoke方法,请说。

PinvokedVendor.DLL

PinvokedVendor.DLL

[System.Runtime.InteropServices.DllImportAttribute("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?

如何确保这个包装类是线程安全的?当从ASP.NET调用时,它甚至是线程安全的吗?我的选择是什么?

Thanks in advance.

提前谢谢。

3 个解决方案

#1


4  

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.

一个叫它A的电话。线程安全源于供应商DLL的实现及其接口规范。p调用与它无关。

#2


0  

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

从供应商的角度来看。PInvoke调用与普通的本机方法调用没有任何不同。它不应该对代码的线程安全性产生任何影响。假设PInvoke调用都是静态的,那么可以在代码中从多个线程调用它们。当然,假设您以供应商.dll规定的安全的方式使用结果数据

#3


0  

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...

让代码[MTAThread]而不是[STAThread]在使用pinvoke和线程处理COM的方法中有用吗?只是一个想法…

Hope this helps, Best regards, Tom.

祝你好运,汤姆。

#1


4  

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.

一个叫它A的电话。线程安全源于供应商DLL的实现及其接口规范。p调用与它无关。

#2


0  

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

从供应商的角度来看。PInvoke调用与普通的本机方法调用没有任何不同。它不应该对代码的线程安全性产生任何影响。假设PInvoke调用都是静态的,那么可以在代码中从多个线程调用它们。当然,假设您以供应商.dll规定的安全的方式使用结果数据

#3


0  

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...

让代码[MTAThread]而不是[STAThread]在使用pinvoke和线程处理COM的方法中有用吗?只是一个想法…

Hope this helps, Best regards, Tom.

祝你好运,汤姆。