To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old May 8th, 2007, 05:38 AM   #1
GrimmReaper
Hyperactive Member
 
Join Date: Sep 05
Posts: 344
GrimmReaper is on a distinguished road (20+)
Resolved [RESOLVED] [2005] VB.NET and APIs

Hello guys!

I just need some clarity on how to properly use APIs in .NET.

In VB 6, we used to declare APIs like this :
Code:
Private Declare Function GetCaretBlinkTime Lib "user32" Alias "GetCaretBlinkTime" () As Long
In VB 2005, I can also declare the same API, almost 100% similar, with the difference being with the function return types, and argument types (Am I correct)

In VB.NET 2005 I declare this API like this:
Code:
    Private Declare Function GetCaretBlinkTime Lib "user32.dll" () As Int32
Which is all fine and dandy, BUT, I have noticed many people opting to use the Sytem.Runtime.InteropServices class / namespace (Which is it ?), and then create the particular API like this :
Code:
    <DllImport("user32.dll", EntryPoint:="FindWindowEx", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
    ByVal childAfter As IntPtr, _
    ByVal lclassName As String, _
    ByVal windowTitle As String) As IntPtr
    End Function
I have found this technique amusing. I went ahead and based on the above (FindWindowEx) example, and did this with GetCaretBlinkTime
Code:
    <DllImport("user32", EntryPoint:="GetCaretBlinkTime", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Function GetCaretBlinkTime() As Int32
    End Function
But, now, I get this error :
Quote:
Originally Posted by Compiler
'System.Runtime.InteropServices.DllImportAttribute' cannot be applied to instance method
Why?
Why can I not follow the same principal as with FindWindowEx?
Can I not use this technique with every API?

Any advice on these will be appreciated.
GrimmReaper is offline   Reply With Quote
Old May 8th, 2007, 06:21 AM   #2
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 54,913
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
Re: [2005] VB.NET and APIs

You can use the Declare key word as you did in VB6, but that's a VB hold-over. You need to use numerical types that are as wide as the function expects. Most API functions expect 32-bit numbers. In VB6 that meant Long, but in VB.NET it means Integer.

The "more .NET" way is to use the DllImportAttribute class, which is a member of the System.Runtime.InteropServices namespace. There are a few things you can do with it that are just not possible with Declare.

The reason that code is not working is exactly what the error message says. Imported functions must be declared Shared. The Declare key word inherently creates a Shared method, but with the DllImport attribute you need to specify it yourself.

By the way, MSDN is just a couple of mouse clicks away so there's really no excuse for not knowing what's a class a what's a namespace.
__________________

2007, 2008, 2009, 2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET
jmcilhinney is offline   Reply With Quote
Old May 8th, 2007, 06:22 AM   #3
GrimmReaper
Hyperactive Member
 
Join Date: Sep 05
Posts: 344
GrimmReaper is on a distinguished road (20+)
Re: [2005] VB.NET and APIs

Voila!
Code:
    <DllImport("user32", EntryPoint:="GetCaretBlinkTime", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function GetCaretBlinkTime() As Int32
    End Function
EDIT: Thanx for the reply jmcilhinney, didn't see it upfront. What you said made perfect sense. Thank you!
GrimmReaper is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 09:54 AM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.