Solved

calling formatMessage() from VB5

Posted on 1998-06-21
6
446 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now