Err.LastDllError - what's the .Net equivalnet?

Merrion

Well-known member
Joined
Sep 29, 2001
Messages
265
Location
Dublin, Ireland
User Rank
*Experts*
How can I get similar functionality to Err.LastDllError (from VB5/6) in VB.Net? I presume GetLastError api call is still suspect because of API functions that may trample on the error code being called internally by the programming language?
 
The Err object still exists in VB.NET, along with LastDllError, but Im
not sure of the programming ethics that go along with it, since its in the
Microsoft.VisualBasic namespace, which as I understand is the compatability
class. It should make a fine placeholder until you find a better way, however.
 
Originally posted by VolteFace
...its in the
Microsoft.VisualBasic namespace, which as I understand is the compatability
class...

Only if you believe CLs lies!
 
Microsoft can call it whatever they want. Thats partly what its there for (it obviously also serves as the runtime).
 
It appears that you need to set the "SetLastError" attribute to make use of this e.g.:

Code:
<System.Runtime.InteropServices.DLLImport("winspool.drv",EntryPoint:="GetJob",SetLastError:=True, _ 
  CallingConvention:=CallingConvention.StdCall>_
Private Function GetJob(ByVal mhPrinter As Int32,  _ 
                                      ByVal dwJobId As Int32, _ 
                                      ByVal Level As Print_Job_Command_Levels, _ 
                                      ByVal lpJob As Int32, _ 
                                      ByVal cbBuf As Int32, _ 
                                      ByRef lpSizeNeeded As Int32) As Boolean

End Function

Incidentally - Im not sure about "ByVal lpJob As Int32,". How should I pass a pointer to a byte buffer in?

Thanks in advance,
Duncan
 
Make a call to Marshal.AllocHGlobal which will return an IntPtr to a memory location. Call Marshal.Copy to transfer the managed byte array to unmanaged memory. Then call .ToInt32 on the IntPtr to convert the pointer to an integer that GetJob will recognize. Finally unallocate the memory using Marshal.FreeHGlobal. Catch all that? ;)
 
Back
Top