托管环境中的多线程 Pinvoked DLL。是否可以?
我有一个 Vendor.DLL(本机 DLL,用 C++ 编写),它公开了许多方法。通常,Vendor.DLL 打开供应商专有文件,返回句柄并允许对这些文件进行更多读/写操作。 Vendor.DLL 支持多线程(当从非托管代码/COM 调用时)。
如果我从供应商 DLL 公开 Pinvoked 方法,例如
PinvokedVendor.DLL
[System.Runtime.InteropServices.DllImportAttribute("Vendor.dll", EntryPoint = "SomeVendorMethod")]
public static extern int SomeVendorMethod(uint param1, ref SomeVendorDataStruct pData);
如何确保此包装类是线程安全的?从 ASP.NET 调用时它是否是线程安全的?我有什么选择?
提前致谢。
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.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?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个电话,一个电话,一个电话。线程安全源自供应商 DLL 的实现及其接口规范。 P-Invoke 与此无关。
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.
从vendor.dll 的角度来看,PInvoke 调用看起来与普通的本机方法调用没有任何不同。它不应该对代码的线程安全产生任何影响。鉴于 PInvoke 调用都是静态的,因此可以从代码中的多个线程调用它们。当然,假设您按照供应商.dll 规定的安全方式使用结果数据
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
在使用与 COM 相关的 pinvoke 和线程的方法中,将代码设置为 [MTAThread] 而不是 [STAThread] 是否有用?只是一个想法...
希望这有帮助,
此致,
汤姆.
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.