WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

DllLastError GetLastError Differences


Question:

Trying to get lasterror values when using DllCalls I get different results between DllLastError() and a direct call to the GetLastError function using DllCall. Am I missing something obvious, did I do something wrong or is there another explanation?
; Example code
; First try

dllname = StrCat(DirWindows(1),"secur32.dll")
NameFormat = 0 ; Force an error 
lpNameBufferSize = 1024
lpNameBuffer = BinaryAlloc(lpNameBufferSize)
nSizeBufferSize = 4
nSizeBuffer = BinaryAlloc(nSizeBufferSize)
errorvalue = 0

BinaryEodSet(lpNameBuffer,lpNameBufferSize)
BinaryEodSet(nSizeBuffer,nSizeBufferSize)
BinaryPoke4(nSizeBuffer,0,lpNameBufferSize)

result = DllCall(dllname,long:"GetUserNameExA",long:NameFormat,lpbinary:lpNameBuffer,lpbinary:nSizeBuffer)

If !result
errorvalue = DllLastError()
Endif

; Read info from buffers here...
; Code removed, I'm interested in the lasterror value

BinaryFree(lpNameBuffer)
BinaryFree(nSizeBuffer)

Message("DllLastError value",errorvalue)



; Second try
; Same code, DllLastError() replaced with a DllCall

dllname = StrCat(DirWindows(1),"secur32.dll")
NameFormat = 0 ; Force an error
lpNameBufferSize = 1024
lpNameBuffer = BinaryAlloc(lpNameBufferSize)
nSizeBufferSize = 4
nSizeBuffer = BinaryAlloc(nSizeBufferSize)
errorvalue = 0

BinaryEodSet(lpNameBuffer,lpNameBufferSize)
BinaryEodSet(nSizeBuffer,nSizeBufferSize)
BinaryPoke4(nSizeBuffer,0,lpNameBufferSize)

result = DllCall(dllname,long:"GetUserNameExA",long:NameFormat,lpbinary:lpNameBuffer,lpbinary:nSizeBuffer)

If !result
; GetLastError function: DWORD GetLastError(void);
dllname = StrCat(DirWindows(1),"kernel32.dll")
errorvalue = DllCall(dllname,long:"GetLastError")
Endif

; Read info from buffers here...
; Code removed, I'm interested in the lasterror value

BinaryFree(lpNameBuffer)
BinaryFree(nSizeBuffer)

Message("GetLastError value",errorvalue)


Exit

Using Winbatch 2004D on Windows 2000 SP4

Answer:

If you were writing your C-language program, you would use GetLastError() directly after making a function call in the Win32 API library that is capable of setting a system error code.

When you are using DllCall() or DllCallCDecl() in WinBatch, a lot of other Win32 API functions can be called [that will disturb the system error code] between when DllCall*() is called and when you'd be all to use DllCall() again to call GetLastError(). What WinBatch is doing is that immediately after it calls a function using DllCall*(), it then calls GetLastError() and stores the result so that DllLastError() can return it when called.

User Reply:

OK, I see. So the only way to go here is to use DllLastError() and *not* use the DllCall to get the 'GetLastError' value at all. Thanks for the info.
Article ID:   W16431
File Created: 2005:02:18:12:20:30
Last Updated: 2005:02:18:12:20:30