4 Replies - 1455 Views - Last Post: 03 March 2009 - 11:09 PM Rate Topic: -----

#1 Tim A  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-April 08

Question on API Declaration Styles

Post icon  Posted 03 March 2009 - 09:58 AM

I've noticed several ways to declare the API functions in VB.Net. Below are two that I have seen. What is the difference, what is preferred and why do some functions work either way and others do not?

<DllImport("shell32.dll")> _
Public Shared Function SHBrowseForFolder(ByRef lpbi As BROWSEINFO) As Integer
End Function
<DllImport("shell32.dll")> _
Public Shared Function SHGetPathFromIDList(ByVal pidList As Integer, ByVal lpBuffer As String) As Integer
End Function
<DllImport("shell32.dll")> _
Public Shared Function SHGetSpecialFolderLocation(ByVal hWndOwner As Integer, ByVal nFolder As Integer, ByRef ListId As Integer) As Integer
End Function

Public Declare Function SHBrowseForFolder Lib "shell32" (ByRef lpbi As BROWSEINFO) As Integer
Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Integer, ByVal lpBuffer As String) As Integer
Public Declare Function SHGetSpecialFolderLocation Lib "shell32" (ByVal hWndOwner As Integer, ByVal nFolder As Integer, ByRef ListId As Integer) As Integer



In the above example, the second function 'SHGetPathFromIDList' failed to execute correctly when using the "DllImport" style of declaration.

Is This A Good Question/Topic? 0
  • +

Replies To: Question on API Declaration Styles

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question on API Declaration Styles

Posted 03 March 2009 - 10:21 AM

DllImport is actually importing the DLL file, allowing access to properties of the Dynamic Link Library (DLL) that Public Declare doesn't offer.

The major headache with Public Declare would be the data types. Sometime an API would be declared using Integers and it will not work properly.
Was This Post Helpful? 0
  • +
  • -

#3 Tim A  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-April 08

Re: Question on API Declaration Styles

Posted 03 March 2009 - 01:49 PM

Like the "DllImport" way of declaring. Ended up being parameter types for the strings and integers. Works great now!

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question on API Declaration Styles

Posted 03 March 2009 - 02:42 PM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Question on API Declaration Styles

Posted 03 March 2009 - 11:09 PM

Public Declares are used in VB6 to call API Functions.
.NET supports it as well to allow developers to have a smooth transition from VB6 to .NET.

DllImport allows you to provide more options, so I would recommend it over Public Declare.

(Topic Title edited to be more descriptive)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1