Solved

calling formatMessage() from VB5

Posted on 1998-06-21
6
455 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
to transfer string from C lanaguage to VBA 4 66
VB6 - Compare and highlight cell not the same 3 51
how to know if my Checkbox is True in VB6.0? 9 55
Problem to With line 4 57
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

773 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