473,771 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check result of call into Windows API

Lee
(I also posted this query in Microsoft.Publi c.DotNet.Framew ork
yesterday, but since I have received no responses, I am posting it here
too.)

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("use r32.dll", _
EntryPoint:="Me ssageBeep", _
SetLastError:=T rue, _
CallingConventi on:=CallingConv ention.StdCall) _
Public Function MessageBeep(ByV al 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.GetLast Win32Error
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

Jul 29 '06 #1
5 1637

Lee wrote:
(I also posted this query in Microsoft.Publi c.DotNet.Framew ork
yesterday, but since I have received no responses, I am posting it here
too.)

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("use r32.dll", _
EntryPoint:="Me ssageBeep", _
SetLastError:=T rue, _
CallingConventi on:=CallingConv ention.StdCall) _
Public Function MessageBeep(ByV al 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.GetLast Win32Error
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 - MessageBeep is probably not a good choice for this. From the
remarks section of the MSDN help:

If it cannot play the specified alert sound, MessageBeep attempts to
play the system default sound.
What that means is that this function won't really fail for an invalid
value - it will simply play the system default sound. And that is what
it does on my system, it plays the sound even with an invalid value and
the function returns true.

--
Tom Shelton [MVP]

Jul 30 '06 #2
Lee
Tom:

Thanks for the feed-back.

I guess I mis-interpreted the docs and thought that an invalid value
would cause MessageBeep to fail.

I'll try a different API function and post my results here.

--
// Lee Silver
// Information Concepts Inc.
//
// Converting Data into Information since 1981
Tom Shelton wrote:
Lee wrote:
(I also posted this query in Microsoft.Publi c.DotNet.Framew ork
yesterday, but since I have received no responses, I am posting it here
too.)

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("use r32.dll", _
EntryPoint:="Me ssageBeep", _
SetLastError:=T rue, _
CallingConventi on:=CallingConv ention.StdCall) _
Public Function MessageBeep(ByV al 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.GetLast Win32Error
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 - MessageBeep is probably not a good choice for this. From the
remarks section of the MSDN help:

If it cannot play the specified alert sound, MessageBeep attempts to
play the system default sound.

What that means is that this function won't really fail for an invalid
value - it will simply play the system default sound. And that is what
it does on my system, it plays the sound even with an invalid value and
the function returns true.

--
Tom Shelton [MVP]
Jul 30 '06 #3
Lee
Per Tom's suggestion I changed my test to use the GetTempFileName and
got the same result. My changes were as follows:

1)

<DllImport("ker nel32.dll", _
EntryPoint:="Ge tTempFileNameA" , _
CharSet:=CharSe t.Ansi, _
ExactSpelling:= True, _
CallingConventi on:=CallingConv ention.StdCall) _
Public Function GetTempFileName (ByVal Path As String, _
ByVal PrefixString As String, _
ByVal Unique As Integer, _
ByVal TempFileName As
Text.StringBuil der) As Integer
End Function

and 2)

Dim GTFName As New Text.StringBuil der(255)
Dim GTFResult As Integer
Dim GTFErrorCode As Integer

GTFResult = GetTempFileName ("x:\temp" & Chr(0), _
"zzzz" & Chr(0), _
0, _
GTFName)
If GTFResult = 0 Then
GTFErrorCode = Marshal.GetLast Win32Error
End If
GTFResult is 0 (failure) after the call to GetTempFileName , and
GTFErrorCode is 127. Note that I am forcing an error because "x:\temp"
does not exist (I have no X drive), but the error-code seems to be
wrong.

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

Lee wrote:
Tom:

Thanks for the feed-back.

I guess I mis-interpreted the docs and thought that an invalid value
would cause MessageBeep to fail.

I'll try a different API function and post my results here.

--
// Lee Silver
// Information Concepts Inc.
//
// Converting Data into Information since 1981
Tom Shelton wrote:
Lee wrote:
(I also posted this query in Microsoft.Publi c.DotNet.Framew ork
yesterday, but since I have received no responses, I am posting it here
too.)
>
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("use r32.dll", _
EntryPoint:="Me ssageBeep", _
SetLastError:=T rue, _
CallingConventi on:=CallingConv ention.StdCall) _
Public Function MessageBeep(ByV al 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.GetLast Win32Error
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 - MessageBeep is probably not a good choice for this. From the
remarks section of the MSDN help:

If it cannot play the specified alert sound, MessageBeep attempts to
play the system default sound.
What that means is that this function won't really fail for an invalid
value - it will simply play the system default sound. And that is what
it does on my system, it plays the sound even with an invalid value and
the function returns true.

--
Tom Shelton [MVP]
Jul 30 '06 #4

Lee wrote:
Per Tom's suggestion I changed my test to use the GetTempFileName and
got the same result. My changes were as follows:

1)

<DllImport("ker nel32.dll", _
EntryPoint:="Ge tTempFileNameA" , _
CharSet:=CharSe t.Ansi, _
ExactSpelling:= True, _
CallingConventi on:=CallingConv ention.StdCall) _
Public Function GetTempFileName (ByVal Path As String, _
ByVal PrefixString As String, _
ByVal Unique As Integer, _
ByVal TempFileName As
Text.StringBuil der) As Integer
End Function

and 2)

Dim GTFName As New Text.StringBuil der(255)
Dim GTFResult As Integer
Dim GTFErrorCode As Integer

GTFResult = GetTempFileName ("x:\temp" & Chr(0), _
"zzzz" & Chr(0), _
0, _
GTFName)
If GTFResult = 0 Then
GTFErrorCode = Marshal.GetLast Win32Error
End If
GTFResult is 0 (failure) after the call to GetTempFileName , and
GTFErrorCode is 127. Note that I am forcing an error because "x:\temp"
does not exist (I have no X drive), but the error-code seems to be
wrong.
Looking at this and your last example, I realize what the problem is...
You aren't adding the SetLastError:=T rue to your DllImport attribute.
Without that, the api error code is not saved.

Now for a couple of api suggestions...

1) You don't need to null terminate your strings (the Chr(0)), the
marshaller will take care of that for you. That was true in VB6 as
well as .NET. It won't hurt anything really, but it is just extra
overhead that doesn't need to happen.

2) Don't hard code to the A/W versions of your calls. The runtime
marshaller can work that out for you as well. In other words, your
declare would look like:

<DllImport("ker nel32", CharSet:=CharSe t.Auto, SetLastError:=T rue)>
_
Private Function GetTempFileName ( _
ByVal lpPathName As String, _
ByVal lpPrefixString As String, _
ByVal uUnique As Integer, _
ByVal lpTempFileName As StringBuilder) As Integer

End Function

The runtime will call the W function on systems were it is available,
avoiding the extra string conversions required for the A function on NT
boxes.

3) Why don't you just use the VB declare syntax?
Private Declare Auto Function GetTempFileName Lib "kernel" (.....) As
Integer
That will set all the necessary attribute fields (like SetLastError).

Don't take any of the above as critizism, just friendly advice. Anyway
here is my code for your example:

Option Explicit On
Option Strict On

Imports System
Imports System.Text
Imports System.Componen tModel
Imports System.Runtime. InteropServices

Module Module1

<DllImport("ker nel32", CharSet:=CharSe t.Auto, SetLastError:=T rue)>
_
Private Function GetTempFileName ( _
ByVal lpPathName As String, _
ByVal lpPrefixString As String, _
ByVal uUnique As Integer, _
ByVal lpTempFileName As StringBuilder) As Integer
End Function

'Private Declare Auto Function GetTempFileName Lib "kernel32" ( _
' ByVal lpPathName As String, _
' ByVal lpPrefixString As String, _
' ByVal uUnique As Integer, _
' ByVal lpTempFileName As StringBuilder) As Integer

Sub Main()
Dim tempFile As New StringBuilder(2 60)

If GetTempFileName ("x:\temp", "zzzz", 0, tempFile) = 0 Then

Dim exception As New
Win32Exception( Marshal.GetLast Win32Error())
Console.WriteLi ne( _
"The error code was {0} and the message was {1}", _
exception.Error Code, exception.Messa ge)

End If
End Sub
End Module

--
Tom Shelton [MVP]

Jul 31 '06 #5
Lee
Tom:

Much much thanks for the help and advice.

1) The missing "SetLastErr or" was a brain fart on my part :) -- once I
added that, things worked as expected.

2) I wasn't sure about the Chr(0)'s -- but I figured better safe than
sorry. Glad they are not needed.

3) I did decide to change the declaration syntax to the shorter Declare
syntax -- I'm not sure why I was using the empty-function syntax -- but
the Declare syntax works just fine.

Thanks again for the help and advice.

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

Tom Shelton wrote:
Looking at this and your last example, I realize what the problem is...
You aren't adding the SetLastError:=T rue to your DllImport attribute.
Without that, the api error code is not saved.

Now for a couple of api suggestions...

1) You don't need to null terminate your strings (the Chr(0)), the
marshaller will take care of that for you. That was true in VB6 as
well as .NET. It won't hurt anything really, but it is just extra
overhead that doesn't need to happen.

2) Don't hard code to the A/W versions of your calls. The runtime
marshaller can work that out for you as well. In other words, your
declare would look like:

<DllImport("ker nel32", CharSet:=CharSe t.Auto, SetLastError:=T rue)>
_
Private Function GetTempFileName ( _
ByVal lpPathName As String, _
ByVal lpPrefixString As String, _
ByVal uUnique As Integer, _
ByVal lpTempFileName As StringBuilder) As Integer

End Function

The runtime will call the W function on systems were it is available,
avoiding the extra string conversions required for the A function on NT
boxes.

3) Why don't you just use the VB declare syntax?
Private Declare Auto Function GetTempFileName Lib "kernel" (.....) As
Integer
That will set all the necessary attribute fields (like SetLastError).

Don't take any of the above as critizism, just friendly advice. Anyway
here is my code for your example:

Option Explicit On
Option Strict On

Imports System
Imports System.Text
Imports System.Componen tModel
Imports System.Runtime. InteropServices

Module Module1

<DllImport("ker nel32", CharSet:=CharSe t.Auto, SetLastError:=T rue)>
_
Private Function GetTempFileName ( _
ByVal lpPathName As String, _
ByVal lpPrefixString As String, _
ByVal uUnique As Integer, _
ByVal lpTempFileName As StringBuilder) As Integer
End Function

'Private Declare Auto Function GetTempFileName Lib "kernel32" ( _
' ByVal lpPathName As String, _
' ByVal lpPrefixString As String, _
' ByVal uUnique As Integer, _
' ByVal lpTempFileName As StringBuilder) As Integer

Sub Main()
Dim tempFile As New StringBuilder(2 60)

If GetTempFileName ("x:\temp", "zzzz", 0, tempFile) = 0 Then

Dim exception As New
Win32Exception( Marshal.GetLast Win32Error())
Console.WriteLi ne( _
"The error code was {0} and the message was {1}", _
exception.Error Code, exception.Messa ge)

End If
End Sub
End Module

--
Tom Shelton [MVP]
Jul 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6860
by: build | last post by:
G'day All, I'm using code below to check a path selected by a user. Can anyone pick holes in it please? The more critical you are the better. tia build 'GET SOURCE PATH -------------------------------------- inPath = Me.txtPath.Text 'CHECK PATH ----------------------------
11
3764
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows 2003 Server or ADO or ODBC issue, I am posting this on all of the three newsgroups. That's the setup: Windows 2003 Server with IIS and ASP.NET actiavted Access 2002 mdb file (and yes, proper rights are set on TMP paths and path,
2
1606
by: Skybuck Flying | last post by:
Hi, This is a somewhat cleaned up version 0.02 of the nrand48() routine. There are still a few issues: This code needs brackets but where ? X = (uint64) xsubi << 32 | (uint32) xsubi << 16 | xsubi;
11
8003
by: zhong | last post by:
Error Message: Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention. There are several answers to this error, but my situation is different. The place I get this error is at
3
2121
by: Caspy | last post by:
I just get stuck on how to check if a user is a member of network (domain). I am building an internal tracking system with ASP.Net with Form authentication. When an user is added into the system, it check if the user is a member of the domain account against Global Catalog. If not, the user is not allowed to added in. If is, get the user's first name and last name and insert into the database. Because the system need access to other...
1
2359
by: Dave Brown | last post by:
I am attempting to post to a url (https://FakeURL/logon.asp) using the HttpWebRequest class. The response for a succesful post will contain the html for the logon user's default page. We've accomplished this in the past utilizing the ServerXMLHTTP object. When I try an equivalent? post utilizing the HttpWebRequest class, the response contains the html for logon.asp (the same page that was posted to), which indicates to me that in some...
2
6385
by: Steve JORDI | last post by:
Hi, I'm having a little trouble trying to update a page content when the user clicks a check box. Basically, I need to reload a page to update its content depending on that checkbox status. I have added a checkbox that has an onClick event that calls a javascript function that loads the form itself and adds a GET variable in the url so that I can check which data I have to display.
17
2101
by: Klaas Vantournhout | last post by:
Hi all, I was wondering if it is possible if you can check in a function if one of the arguments is temporary. What I mean is the following. A is a class, foo is a function returning a class and bar is a function using A is an argument returning something else class A;
173
8184
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc. OTOH, if you're allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.