VB6 COM+ calling .Net COM DLL
Asked Answered
P

1

6

Happy 20th Birthday VB6! Yes, VB6 is still alive even though support ran out 13 years ago. Feel free to razz me but please also provide any insight you might have on this issue.

I have a VB6 EXE on a Citrix farm. Data is obtained via COM+ from another server hosting a VB6 COM+ DLL. The COM+ package is exported and installed on each Citrix machine.

The EXE and the COM+ DLL both write to an old logger. I'm trying to get rid of the old logger in favor of a Log4Net version.

I wrote a .NET COM DLL whose sole purpose is to write to the log.

The EXE logs using the .NET DLL (as long as I put the log.config in the same folder as the EXE) but the COM+ component does not. I've tried copying the log.config into various folders hoping that might be the problem. I also wrote another EXE just to test the .NET DLL and it works.

Just for kicks I tried using late binding on the COM+ DLL but that didn't help either. I have the .Net COM DLL registered on the Citrix machine as well as the COM server.

I also attempted to write to the event log from vb6 -- both failed.

I am certain my COM+ DLL's are being implemented as no logs are being written to the old logger. Also, when running from my local machine I get logs from my EXE and the COM+ DLL.

Here's some code...

Here's my VB.NET COM Logging code

Imports System.IO
Imports log4net

<Assembly: Config.XmlConfigurator(ConfigFile:="Log.config", Watch:=True)>

<ComClass(log.ClassId, log.InterfaceId, log.EventsId)>
Public Class log

    Public logger As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)

#Region "COM GUIDs"
    Public Const ClassId As String = "6fae24c7-f86b-4fab-8b49-1d441de5bd18"
    Public Const InterfaceId As String = "223a30c4-ec1e-45a6-82d3-d23cd4c933f9"
    Public Const EventsId As String = "2087d93e-dd38-4cd1-b757-44dee322a8e3"
    Public Property TraceExtension As Object
#End Region

    Public Sub New()
        MyBase.New()

        WriteEvent("New Start")

        If Not log4net.LogManager.GetRepository().Configured Then
            Dim configFileDirectory = (New DirectoryInfo(TraceExtension.AssemblyDirectory)).Parent
            Dim configFile = New FileInfo(configFileDirectory.FullName + "\log.config")

            If configFile.Exists Then
                WriteEvent("New Config Exists")
                log4net.Config.XmlConfigurator.Configure(configFile)
            Else
                WriteEvent(String.Format("The configuration file {0} does not exist", configFile))
                Throw New FileLoadException(String.Format("The configuration file {0} does not exist", configFile))
            End If
        End If

        WriteEvent("New End")

    End Sub

    Private Sub WriteEvent(msg As String, Optional id As Integer = 11)
        Using eventLog As New EventLog("Application")
            eventLog.Source = "CSS"
            eventLog.WriteEntry(msg, EventLogEntryType.Error, id)
        End Using
    End Sub

    Public Sub Debug(msg As String)
        logger.Debug(msg)
    End Sub
    Public Sub Info(msg As String)
        logger.Info(msg)
    End Sub
    Public Sub Warn(msg As String)
        logger.Warn(msg)
    End Sub
    Public Sub Err(msg As String)
        logger.Error(msg)
    End Sub
    Public Sub Fatal(msg As String)
        logger.Fatal(msg)
    End Sub
End Class

and here is the VB6 code being called from both EXE and COM+ DLL.

Public Sub AppLog(ByVal LogType As Integer, ByVal Data As String)
10  On Error Resume Next
20  Dim klog As New KerryLog.Log
30  Data = "EXE > " + Data ' the COM+ DLL has a Prefix of COM instead of EXE

    Select Case LogType
            Case LogTypeNone: klog.Info Data
            Case LogTypeAppStart: klog.Info Data
            Case LogTypeAppEnd: klog.Info Data
            Case LogTypeInfo: klog.Info Data
            Case LogTypeVerbose: klog.Debug Data
            Case LogTypeWarning: klog.Warn Data
            Case LogTypeHandledException: klog.Err Data
            Case LogTypeUnhandledException: klog.Fatal Data
            Case LogTypeAutomated: klog.Info Data
            Case LogTypeUsage: klog.Debug Data
    End Select

40  Set klog = Nothing

Here's method 1 I was using to write to the event log:

App.StartLogging "", vbLogToNT
App.LogEvent "this is the error message", vbLogEventTypeError

and here's method 2 of writing to the event log

Private 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 Long, _
    ByVal wNumStrings As Integer, _
    ByVal dwDataSize As Long, _
    plpStrings As String, _
    lpRawData As Long) As Long

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

Public Sub LogThis(nErrNo As Long, sLogMsg As String, EventType As LogEventTypeConstants)
    Dim hEvent As Log

    hEvent = RegisterEventSource("", "CSS")
    Call ReportEvent(hEvent, EventType, 0, nErrNo, 0, 1, Len(sLogMsg), sLogMsg, 0)
End Sub

The Event log is a side note -- I'm trying to write to something just so I can debug. I guess my next step will be trying to write to a text file.

Maybe this is a permission issue? Both Citrix and the COM+ server is Win Server 2008 R2 Standard SP1 / 64 bit

Any ideas?

Patentor answered 3/4, 2018 at 21:59 Comment(3)
I'm not sure that it will help, but when log4net not working with COM+ they're talking about some kind of repository configuration: #1028875, weblogs.asp.net/george_v_reilly/…Kuster
I don't see any reason why it couldn't work, but many things can fail (security, bitness, file paths, etc.). Difficult to say w/o reproducing code. To debug and test, I suggest you use OutputDebugString or better, ETW. I wrote a tool called TraceSpy that allows to trace ETW (the nice thing with ETW is they work accross Windows Workstations, web, etc.) and I've also provided VBA support code (in the vba directory) for ETW (VBA is as you know the close cousin of ole' VB6) github.com/smourier/TraceSpyAminoplast
What is the hosting process for COM+, and where does that binary live? Perhaps that is the location that the config file is being looked for?Zeena
S
0

COM+ components will have it current working folder in C:\Windows\System32 so it will most likey will fail with path access error or file write error in COM+ untill u set absolute paths in your configuration for COM+ Logging.

My approach was to use out-of-process COM+ server for logging, and i accessed to it through ActiveX "middleware" proxy to be independent of logging mechanizm and do not bounds to the existing logging solution. For the out-of-process COM+ activation, I wrote small module to call directly CoCreateInstance, cause nice and clean VB6 alike approach with New just uses in-process COM+ activation only and there is no way to select out-of process activation.

With this architecture, all duties of finding proper logging mechanism and errror handling goes out of target VB6 project to side VB6 ActiveX, wich uses connection to existing COM+ server (out-of-process, remember?) or falls back to default logger implementation in ActiveX, so any code does not seems to write any logging error handling code whatsoever or write additional things except 'New ILoggerFactory.CreateLogger'. Cause ActiveX component itself has implementation of COM+ interface for logging support.

Until i have my COM+ server (.NET 5 application) up and running, i can get additional advanced sturctured logging using NLog format including extensions (DB, etc). The same way, u can use your own log4net .NET executable with configuration to use it to as a default implementation of your COM+ logging ingerface, so u do not need to provide any configuration information to COM+ as long as it has limited rights with the system, in worst scenario, u can embed you default logger implementation as EventLog logger into your ActiveX library, so it will never fails on file/access exceptions, keeping primary COM+ server logger from safe writable locations

Selfservice answered 2/8, 2021 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.