Solved

calling formatMessage() from VB5

Posted on 1998-06-21
6
457 Views
Last Modified: 2008-02-01
Hi There !

I using wininet.dll to connect to ftp server, and i want to handle errors, I found that i have to use kernel32.dll formatMessage() (info from MSDN appendix c: handling errors).

That problem is I get "application error" or "mismach type" when I call this func, I belive it's becasue the the last parameter :va_list *args , and in the declaration (from API viewer) it's defined as long.

My questions are :
1. if parameter defined as va_list *Args in the dll - how to I declare it in Vb?
2. how do i send C++'s NULL ? - vbNullString doesn't work

thanks
0
Comment
Question by:avior
  • 3
  • 3
6 Comments
 

Author Comment

by:avior
ID: 1463888
Edited text of question
0
 
LVL 4

Expert Comment

by:yowkee
ID: 1463889
avior,

  1. You declare the parameter with type va_list as "Long", since what the API required in this parameter is a pointer.
  2. To pass Null to Windows API, pass vbNullString to paramter declared as string or any, and pass 0 to parameter declared as Long or Integer.

Try following code and you could get the error message to related error no which return by GetLastError.

---' The code is in a BAS module
Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _
                            (ByVal dwFlags As Long, lpSource As Any, _
                            ByVal dwMessageId As Long, ByVal dwLanguageId As Long, _
                            ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
 
Public Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Public Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Public Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Public Const FORMAT_MESSAGE_FROM_STRING = &H400
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Public Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF
Public Const LANG_USER_DEFAULT = &H400&
 

Function GetLastErrorStr(dwErrCode As Long) As String

  ' Let VB alloc the buffer. If FORMAT_MESSAGE_ALLOCATE_BUFFER
  ' was used, the lpBuffer param (sMsgBuf) would have to be a
  ' long pointer
  ' to the buffer & would then have to be freed when we're done.
  ' 256 chars is the maximun length for a resource string (+ 1     ' for the null char)
  Static sMsgBuf As String * 257, dwLen As Long

  dwLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM _
                        Or FORMAT_MESSAGE_IGNORE_INSERTS _
                        Or FORMAT_MESSAGE_MAX_WIDTH_MASK, _
                        ByVal 0&, _
                        dwErrCode, LANG_USER_DEFAULT, _
                        ByVal sMsgBuf, 256&, 0&)

  If dwLen Then
      GetLastErrorStr = Trim$(Left$(sMsgBuf, dwLen))
  Else
     GetLastErrorStr = "Unknown error."
  End If

End Function
--------

Regards.


0
 

Author Comment

by:avior
ID: 1463890
hi again !

first, thanks for the good answers, but as I said before when i call the function I get error message "MAILMAN caused an invalid page fault in module KERNEL32.DLL at 0137:bff7be3f.",
The call & the declaration are listed below , hope you can solve my problem.



' declarations

Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Declare Function GetLastError Lib "kernel32" () As Long

Public Const FORMAT_MESSAGE_FROM_HMODULE = &H800


Private Sub showError(dError As Long, funcName$)    
    Dim Buffer As String * 257
    Dim hWininetdll As Long, size As Integer
   
   
    ' get error description from wininet.dll
    hWininetdll = GetModuleHandle("wininet.dll")
    size = FormatMessage(FORMAT_MESSAGE_FROM_HMODULE, hWininetdll, dError, 0&, ByVal Buffer, 256&, 0&)
    msgbox "error in func : " & funcName & ",desciription is :" & Trim$(Left$(Buffer, size))
end Sub

thanks, Avior
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 4

Expert Comment

by:yowkee
ID: 1463891
avior,

  For page fault issue, pass hWininetdll byval. Eg.
size = FormatMessage(FORMAT_MESSAGE_FROM_HMODULE, Byval hWininetdll, dError, 0&, ByVal Buffer, 256&, 0&)

  However, I do a simple test but didn't not get the error message from wininet.dll. I am not sure since I simply pass a error number to it. I am wondering is there anyway to find out a dll got message table or not.

  Regards.
0
 

Author Comment

by:avior
ID: 1463892
yowkee !

Thanks very much it did solve the problem, I realy have to get more expirence with calling dlls.

I dont have option to grade your answer, (and it's A for sure) since you added a comment ,please answer this so i can grade it.


Thanks , Avior  
0
 
LVL 4

Accepted Solution

by:
yowkee earned 100 total points
ID: 1463893
avior,

  You're welcome.
  Answer as my comment.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question