none
VB.Net & API Declarations

    General discussion

  • It seems to me the documentation on using API declarations in VB.Net is all over the place. If I'm lucky I'll find a VB focused example, but usually it's C# or C++. Sometimes it's using the Declare keyword, other times it's DLLImport. Sometimes MS has complete examples, sometimes it doesn't. Sometimes pinvoke,net has examples, but sometimes even those are all over the place.

    Why, after so many years, is this such a black art? Is there one clearing house of complete and mostly accurate info? It doesn't feel right lifting pieces of code here and there and hobbling a program together. Ugh.
    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, July 22, 2011 5:30 PM

All replies

  • I always turn to pinvoke.net when I need to get started using some Win API.

    http://pinvoke.net/

    Friday, July 22, 2011 5:33 PM
    Moderator
  • I think I mentioned pinvoke.net. Matter of fact, I know I did. But even their examples are incomplete.

    Sometimes Declare is used, sometimes DLLImport is used. Is this by design?
    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, July 22, 2011 5:36 PM
  • The Windows API function libraries and documentation were written for C or C++, back when there was no Visual Basic or .NET. Fortunately, .NET is not about "black art". That doesn't mean you can't be adventurous, but if you choose to do so then expect to work harder at it.

    There is a utility called P/Invoke Interop Assistant that may be of some help:

    http://blogs.msdn.com/b/bclteam/archive/2008/06/23/p-invoke-interop-assistant-justin-van-patten.aspx

     


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Friday, July 22, 2011 5:42 PM
  • Thank you Paul.

    Now this program hasn't been updated for almost 4 years, has no documentation, and - wait for it - offers up yet another variation on Declare/DLLImport.

    Amazing.


    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, July 22, 2011 5:49 PM
  •   Public Shared Function GetWindow(<System.Runtime.InteropServices.InAttribute()> ByVal hWnd As System.IntPtr, ByVal uCmd As UInteger) As System.IntPtr
      End Function

    <DllImport("user32", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
    Public Shared Function GetWindow(ByVal hwnd As IntPtr, ByVal uCmd As Integer) As IntPtr
    End Function

    Declare Auto Function GetWindow Lib "user32.dll" ( _
      ByVal hWnd As IntPtr, ByVal uCmd As UInt32) As IntPtr
    


    Three (seemingly) different ways to call the same GetWindow API function.

    Which would you use?


    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, July 22, 2011 5:57 PM
  • I typically use the Declare method, since I come from the Classic Visual Basic world, but I'm not fussy. Use whichever is easier for you to understand.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Friday, July 22, 2011 6:15 PM
  • This is a cluster.

    I'm going to go take a stroll across a bed of hot coals.


    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, July 22, 2011 6:43 PM
  • The 'Declare' approach is an older VB style that was available before .NET (it uses 'DllImport' behind the scenes, though).

    Unfortunately, the desire for compatibility with VB6 has resulted in many ways of doing many things in VB.

     


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Friday, July 22, 2011 8:06 PM
  • The 'Declare' approach is an older VB style that was available before .NET (it uses 'DllImport' behind the scenes, though).

    Unfortunately, the desire for compatibility with VB6 has resulted in many ways of doing many things in VB.

     


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Indeed. I've used a number of declared API functions going back to VB6.

    Since I'm writing something in VB.Net from the ground up, ideally I'd like to use the native or best practices means to instantiate a function.

    Any way I can find the DLLImport styled method for any given declaration styled VB6 call? Sure, I found some here, and there, but there doesn't seem to be any one fool-proof source. Not even MS.

    Thanks

    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, July 22, 2011 8:15 PM
  • Hi Zamdrist,

    The 2nd one either requires the Imports statement

    Imports System.Runtime.InteropServices

    or to be fully qualified as seen below.

     

     

    However you have uCmd declared as Integer in the 2nd one.

     

    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
     Public Shared Function Get_Window(<System.Runtime.InteropServices.InAttribute()> _
             ByVal hWnd As System.IntPtr, ByVal uCmd As UInteger) As System.IntPtr
     End Function
    
     <System.Runtime.InteropServices.DllImport("user32.dll", CharSet:=System.Runtime.InteropServices.CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
     Public Shared Function GetWindow(ByVal hwnd As IntPtr, ByVal uCmd As Integer) As IntPtr
     End Function
    
     Public Declare Auto Function GetWindow Lib "user32.dll" ( _
      ByVal hWnd As IntPtr, ByVal uCmd As UInt32) As IntPtr
    
    End Class
    




    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net

    App Hub forums



    Friday, July 22, 2011 8:26 PM
  • Reference vjslib and import the com.ms.win32 namespace.  Don't need PInvode, DLLImport or Declare.  Call the API methods as you would any .NET method.
    Friday, July 22, 2011 8:30 PM
  • Reference vjslib and import the com.ms.win32 namespace.  Don't need PInvode, DLLImport or Declare.  Call the API methods as you would any .NET method.
    Serious? Why would anyone bother with PInvoke, DLLImport or Declare then, if ONLY for migration of a VB6 application?

    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, July 22, 2011 8:34 PM
  • Reference vjslib and import the com.ms.win32 namespace.  Don't need PInvode, DLLImport or Declare.  Call the API methods as you would any .NET method.

    Hi JohnWein,

    I guess that is only available with the full VISUAL STUDIO product?

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net

    App Hub forums



    Friday, July 22, 2011 8:42 PM
  • Wasn't vjslib created for J# applications?  And wasn't J# abandoned in 2005?

    I wouldn't rely on anything related to J#.  Far better to use DllImport.

     

     


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Friday, July 22, 2011 8:47 PM
  • vjslib runtime was updated in 2007 for 32 and 64 bit.  As such Microsoft implies that it will support it until 2017.  Removing the vjslib dependency after you get an application that interops with the WinAPI working correctly is far easier than mucking through the quadmire the OP cites in this thread.
    Friday, July 22, 2011 9:14 PM
  • vjslib runtime was updated in 2007 for 32 and 64 bit.  As such Microsoft implies that it will support it until 2017.  Removing the vjslib dependency after you get an application that interops with the WinAPI working correctly is far easier than mucking through the quadmire the OP cites in this thread.
    And begs the question if such functionality is available to J# (in that manner), why isn't with the other languages? Again, cluster.

    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, July 22, 2011 9:23 PM
  • I would ask myself why do I need an API call? because whatever you needed one for in VB6 or C++ there is prob a .Net method to do the same thing without one.

     

     

    Friday, July 22, 2011 9:34 PM
  • I would ask myself why do I need an API call? because whatever you needed one for in VB6 or C++ there is prob a .Net method to do the same thing without one.

     

     

    A fair question perhaps, though I already asked it in another thread, same board. http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/20711185-7ea3-4fd4-a275-ff99282104b0

    I need to enumerate user windows, get their handle, and selective reset their screen position.

    I have something that works now, but is hobbled from various sample code snippets from around the net, I don't honestly understand half of it, and would like to rewrite it, make it cleaner and more straight forward.

    If you know of a way to enumerate windows (and child windows), move their window position, and possibly return the window application's small icon for an imagelist, without using the Win32 API, I'd love to see it.

    Thanks


    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, July 22, 2011 9:41 PM
  • "And begs the question if such functionality is available to J# (in that manner), why isn't with the other languages? Again, cluster."  Any .Net assembly can be used by any .Net language.  vjslib isn't a J# library, it's a .NET library usuable by any .NET language, for example C# or VB.

    Friday, July 22, 2011 9:45 PM
  • When you say User Windows are you talking all windows or all of your apps windows?
    Friday, July 22, 2011 10:15 PM
  • When you say User Windows are you talking all windows or all of your apps windows?
    App windows, and any associated child windows, i.e Outlook, IE, Word, etc... 
    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, July 22, 2011 10:51 PM
  • Understanding how the APIs function is more of a documentation issue, in which case you should be consulting the API function doc. You say your code is hobbled but works. I have no idea what that means. If it's working then perhaps there is no need to re-write.

    It would probably help us address your questions better if you were more specific.


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Saturday, July 23, 2011 3:48 AM