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("kernel32.dll", _
EntryPoint:="GetTempFileNameA", _
CharSet:=CharSet.Ansi, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)_
Public Function GetTempFileName(ByVal Path As String, _
ByVal PrefixString As String, _
ByVal Unique As Integer, _
ByVal TempFileName As
Text.StringBuilder) As Integer
End Function
and 2)
Dim GTFName As New Text.StringBuilder(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.GetLastWin32Error
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:=True 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("kernel32", CharSet:=CharSet.Auto, SetLastError:=True)>
_
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.ComponentModel
Imports System.Runtime.InteropServices
Module Module1
<DllImport("kernel32", CharSet:=CharSet.Auto, SetLastError:=True)>
_
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(260)
If GetTempFileName("x:\temp", "zzzz", 0, tempFile) = 0 Then
Dim exception As New
Win32Exception(Marshal.GetLastWin32Error())
Console.WriteLine( _
"The error code was {0} and the message was {1}", _
exception.ErrorCode, exception.Message)
End If
End Sub
End Module
--
Tom Shelton [MVP]