Declare和System.Runtime.InteropServices.DllImportAttribute有什么区别?

时间:2014-12-27 13:15:49

标签: vb.net declare compiler-services

我正在使用VB.NET并从System-DLL获取一个图标。因此,我使用ExtractIconEx。正如在评论中所提到的,我正在使用DestroyIcon来再次释放资源。

到目前为止,我使用了

Private Declare Auto Function DestroyIcon Lib "user32.dll" (
  ByVal hIcon As IntPtr) As Boolean

声明该方法。

Icon.FromHandle的{​​{3}}示例,我看到他们使用

<System.Runtime.InteropServices.DllImportAttribute("user32.dll")> _
Private Shared Function DestroyIcon(
  ByVal hIcon As IntPtr) As Boolean
End Function

有什么区别?

我特别想到DllImportAttribute似乎与Declare同样轻松。我做了以下测试:

我使用this从ini文件中获取字符串。

Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (
  ByVal lpApplicationName As String, 
  ByVal lpKeyName As String, 
  ByVal lpDefault As String, 
  ByVal lpReturnedString As String, 
  ByVal nSize As Integer, 
  ByVal lpFileName As String) As Integer

工程;它将条目写入lpReturnedString缓冲区并返回41.

<System.Runtime.InteropServices.DllImportAttribute("kernel32.dll")> _
Private Shared Function GetPrivateProfileString(
  ByVal lpApplicationName As String,
  ByVal lpKeyName As String,
  ByVal lpDefault As String,
  ByVal lpReturnedString As String,
  ByVal nSize As Integer,
  ByVal lpFileName As String) As Integer
End Function

保持lpReturnedString不变,但返回正确的字符串长度41。

0 个答案:

没有答案