是否必须处理本机方法引用 - 如果是的话,是否有任何推荐的做法可以这样做

发布于 2024-09-25 16:10:55 字数 290 浏览 4 评论 0原文

我有一个使用 user32.dll 中的方法的类:

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr windowHandlerPtr);

根据有效的 C#,所有使用非托管代码的类都应该实现 IDisposable 和终结器。在不讨论该讨论的细节的情况下,是否需要处理这种外部方法声明?如果是这样 - 该处理方法会是什么样子?

I have a class which uses a method in user32.dll:

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr windowHandlerPtr);

According to Effective C#, should all classes which uses unmanaged code implement both IDisposable and a finalizer. Without going into the details in that discussion, is there a need to dispose this kind of extern method declaration? If so - how would that dispose method look like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梓梦 2024-10-02 16:10:55

这并不是真正的“参考”,而是一个声明。它实际上并没有创建任何东西,所以不需要处置(事实上,没有什么可以处置的)。

不过,使用来自非托管代码的返回值是您应该注意的事情 - 在这种情况下,它只是一个您不需要担心的布尔值。但在大多数情况下,您应该查看非托管 API 的文档,看看是否应该以某种方式发布它。如果是,那么将其包装在一个可以正确处理的类中。这是一篇关于 IDisposable 的不错的文章

经验法则是:如果它始终实现 IDisposable处置它,如果它是非托管资源,请确保它被正确释放。

That's not really a "reference", but instead a declaration. It doesn't actually create anything, so no need to dispose (nothing to dispose, in fact).

Using a return value from unmanaged code is something you should watch, though - in this case it's just a bool that you don't need to worry about. But in most cases you should look at the unmanaged API's documentation to see if it should be released somehow. If it is, then wrap that up in a class that you can dispose of properly. Here's a decent article on IDisposable

A rule of thumb is: If it implements IDisposable always dispose of it, and if it's an unmanaged resource, make sure it is properly released.

妄断弥空 2024-10-02 16:10:55

在这种情况下,IntPtr 没有要处置的非托管资源。返回值也是 bool ,因此以同样的方式无需执行任何操作。但是,如何获取 IntPtr 可能很重要,如果您创建了它(例如 createDC),则需要释放它(通过调用另一个非托管方法),但在这种情况下您只是获取值然后通过它。

In this case, IntPtr has no unmanaged resource to be disposed of. Return value is bool also so in the same way nothing to do. However, it could be important how you get hold of IntPtr, if you create it (e.g. createDC), you need to release it (by calling another unmanaged method) but in this case you just get the value and then pass it.

终遇你 2024-10-02 16:10:55

根据Effective C#,所有使用非托管代码的类都应该实现 IDisposable 和终结器。

不,这是关于使用非托管资源,而不是代码。在 Windows 中,资源几乎总是由“句柄”表示。非托管代码本身不是资源,除了 DLL 的句柄(您无权访问的句柄)之外,没有与其关联的内核对象。

您传递给 SetForegroundWindow 的参数确实是这些资源之一。它是一个窗口的句柄。但是您自己没有创建该窗口,您试图将焦点设置到某个已创建并由其他代码管理的窗口。您不应该处置不是您创建的对象。

值得注意的是,该建议已经过时了。 Windows 句柄应由 SafeHandle 派生类之一包装。他们已经提供了一个终结器,您不应该实现自己的。只需实现 Dispose() 并调用 SafeHandle.Dispose() 方法即可。

According to Effective C#, should all classes which uses unmanaged code implement both IDisposable and a finalizer.

No, this is about using unmanaged resources, not code. In Windows, resources are almost invariably represented by 'handles'. Unmanaged code in itself is not a resource, there is no kernel object associated with it beyond the handle for the DLL, a handle you don't have access to.

The argument you pass to SetForegroundWindow is indeed one of those resources. It is a handle to a window. But you didn't create that window yourself, you're trying to set the focus to some window that was already created and is managed by other code. You should not dispose objects that you didn't create.

Notable too is that the advice is outdated. Windows handles should be wrapped by one of the SafeHandle derived classes. They already provide a finalizer, you shouldn't implement your own. Just implement Dispose() and call the SafeHandle.Dispose() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文