Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Check result of call into Windows API

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lee - 28 Jul 2006 16:44 GMT
Using Windows XP with all updates applied and Visual Studio 2.0.

I am trying to develop some common error-handling of Windows API
invocations that fail and am using MessageBeep as the API to test with.

Given 1) the following Imports statement:

  Imports System.Runtime.InteropServices

2) the following declaration of MessageBeep:

  <DllImport("user32.dll", _
             EntryPoint:="MessageBeep", _
             SetLastError:=True, _
             CallingConvention:=CallingConvention.StdCall)> _
  Public Function MessageBeep(ByVal wType As Int32) As Boolean
  End Function

3) the following code to test passing an invalid value to MessageBeep

        Dim MBResult As Boolean
        Dim MBErrorCode As Integer

        MBResult = MessageBeep(-2)

        If MBResult Then
           MBErrorCode = Marshal.GetLastWin32Error
        End If

When I set a break-point after the assignment to MBErrorCode, I see
that it has a value of 127 -- "The specified procedure could not be
found". I was expecting a value of 87 -- "The parameter is incorrect".

Note that when I pass MessageBeep a value of 0 -- a presumably valid
value -- I get the same result.

--
// Lee Silver
// Information Concepts Inc.
//
// Converting data into information since 1981
Greg Young - 31 Jul 2006 19:47 GMT
Try user32 instead of user32.dll in your dllimport (it should work like a
charm).

>   <DllImport("user32", _
>              EntryPoint:="MessageBeep", _
>              SetLastError:=True, _
>              CallingConvention:=CallingConvention.StdCall)> _
>   Public Function MessageBeep(ByVal wType As Int32) As Boolean
>   End Function

Also you might want to take a look at this
http://www.codeproject.com/vb/net/apiexception.asp as it seems to be
discussing exactly what you are trying to do.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

> Using Windows XP with all updates applied and Visual Studio 2.0.
>
[quoted text clipped - 37 lines]
> //
> // Converting data into information since 1981
Lee - 31 Jul 2006 22:00 GMT
Greg:

Thanks for the response.

I found the answer(s) I needed in the .VB newsgroup...

It looks like MessageBeep will always succeed -- or at least not fail
for an invalid parameter.

I modified my test to use GetTempFileName specifying an invalid path
and the error detection worked just as it should.

--
// Lee Silver
// Information Concepts Inc.
//
// Converting Data into Information since 1981

> Try user32 instead of user32.dll in your dllimport (it should work like a
> charm).
[quoted text clipped - 15 lines]
> MVP - C#
> http://codebetter.com/blogs/gregyoung

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.