Passing an array of strings from VB to Windows API 
Author Message
 Passing an array of strings from VB to Windows API

Does anyone know how to pass an array of strings from VB to the WindowsAPI
function ReportEvent (or any other API function).

If you could provde me with a sample VB function prototype (Declare Function
...) and a sample function call I'd appreciate it very much.



Fri, 28 Nov 2003 00:34:27 GMT  
 Passing an array of strings from VB to Windows API
I was finally able to make my code work.  I'm posting what I used here in case
others have the same problem.

I tested my code and it works okay but I've had to hack it up a bit to post it
here to get rid of all global variables, user-defined types, and other
extraneous data.  This should work fine, though.

Public Const EVENTLOG_SUCCESS = 0
Public Const EVENTLOG_ERROR_TYPE = 1
Public Const EVENTLOG_WARNING_TYPE = 2
Public Const EVENTLOG_INFORMATION_TYPE = 4
Public Const EVENTLOG_AUDIT_SUCCESS = 8
Public Const EVENTLOG_AUDIT_FAILURE = 10

Public Const MAX_NUMBER_SM_PARAMETERS = 5

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
              (hpvDest As Any, _
               hpvSource As Any, _
               ByVal cbCopy As Long)

Declare Function GetLastError Lib "kernel32" () As Long

Declare Function GlobalAlloc Lib "kernel32" _
                   (ByVal wFlags As Long, _
                    ByVal dwBytes As Long) As Long

Declare Function GlobalFree Lib "kernel32" _
                   (ByVal hMem As Long) As Long

Declare Function DeregisterEventSource Lib "advapi32.dll" _
                   (ByVal hEventLog As Long) As Long

Declare Function RegisterEventSource Lib "advapi32.dll" Alias
"RegisterEventSourceA" _
                   (ByVal lpUNCServerName As String, _
                    ByVal lpSourceName As String) As Long

Declare Function ReportEvent Lib "advapi32.dll" Alias "ReportEventA" _
                   (ByVal hEventLog As Long, _
                    ByVal wType As Integer, _
                    ByVal wCategory As Integer, _
                    ByVal dwEventID As Long, _
                    ByVal lpUserSid As Any, _
                    ByVal wNumStrings As Integer, _
                    ByVal dwDataSize As Long, _
                    plpStrings As Long, _
                    lpRawData As Any) As Boolean

Public Sub procLogEvent()
  Dim intCategory As Integer
  Dim intCounter As Integer
  Dim intLogType As Integer
  Dim intNumParams As Integer
  Dim lgEventID As Long
  Dim lgHandle As Long
  Dim lgReturnValue As Long
  Dim lgStringBufferPointer(MAX_NUMBER_SM_PARAMETERS) As Long
  Dim lgStringSize As Long
  Dim strLogEntry As String
  Dim strParams(MAX_NUMBER_SM_PARAMETERS) As String
  Dim strSource As String

  On Error GoTo ErrorLogEvent

  'these are just example values  
  intCategory = 0
  intLogType = EVENTLOG_ERROR_TYPE
  lgEventID = 100
  intNumParams = 3
  strParams(0) = "My Program Name"
  strParams(1) = "Some Error String"
  strParams(2) = "Another Error String"

  For intCounter = 0 To intNumParams - 1
    strLogEntry = strParams(intCounter)
    lgStringSize = Len(strLogEntry) + 1
    lgStringBufferPointer(intCounter) = GlobalAlloc(&H40, lgStringSize)
    CopyMemory ByVal lgStringBufferPointer(intCounter), ByVal strLogEntry,
lgStringSize
  Next intCounter

  strSource = App.Title
  lgHandle = RegisterEventSource(vbNullString, strSource)

  lgReturnValue = ReportEvent(lgHandle, _
                              intLogType, _
                              intCategory, _
                              lgEventID, _
                              0&, _
                              intNumParams, _
                              0, _
                              lgStringBufferPointer(0), _
                              vbNullString)

  For intCounter = 0 To intNumParams - 1
    Call GlobalFree(lgStringBufferPointer(intCounter))
  Next intCounter

  lgReturnValue = DeregisterEventSource(lgHandle)

  Exit Sub

ErrorLogEvent:
  'handle error here
  Exit Sub
End Sub



Fri, 28 Nov 2003 01:27:47 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Passing String Arrays to API DLLs

2. passing a string array to Win32 API

3. passing a string array to Win32 API

4. passing a string array to Win32 API

5. Passing String Arrays between VB and VC

6. passing (string) arrays from VB to C-DLL and vice versa

7. Passing array of strings to a dll in VB

8. Passing an array of Strings to VB via ActiveX

9. Passing VB String array to DLL???

10. Passing String Arrays between VB and VC

11. passing (string) arrays from VB to C-DLL and vice versa

12. passing (string) arrays from VB to C-DLL and vice versa

 

 
Powered by phpBB® Forum Software