LWChris
LWChris

Reputation: 4221

What is the difference between Declare and System.Runtime.InteropServices.DllImportAttribute?

I'm using VB.NET and obtain an icon from a System-DLL. Therefore, I use ExtractIconEx. As mentioned in the remarks, I'm using DestroyIcon to free the resources again.

So far, I used the line

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

to declare that method.

From this example for Icon.FromHandle, I see they use

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

What is the difference?

I'm especially riddled by that DllImportAttribute doesn't seem to work with the same ease as Declare. I made the following test:

I use GetPrivateProfileString to obtain a String from an ini file.

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

works; it writes the entry in the lpReturnedString buffer and returns 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

leaves lpReturnedString untouched, but returns the correct string length of 41.

Upvotes: 1

Views: 178

Answers (0)

Related Questions