Using a C# dll inside EXCEL VBA

后端 未结 3 1213
深忆病人
深忆病人 2020-12-05 01:13

I am running into a little problem here and need you guys\' help.

I have a C# DLL exposed through COM interop. It is working alright, but apparently the deployment o

3条回答
  •  没有蜡笔的小新
    2020-12-05 02:00

    i had this issue many times. i ended up registering the com dll from vba using a shell and wait method, on the regasm util to register/unregister the dll before late bound creating the com object via

     CreateObject('yourclasshere')
    

    Its a bit of a hack but it works, heres the shellandwait method and a register and unregister method that follows.

                 Private Declare Function OpenProcess Lib "kernel32" _
                 (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
                 ByVal dwProcessId As Long) As Long
    
                 Private Declare Function GetExitCodeProcess Lib "kernel32" _
                 (ByVal hProcess As Long, lpExitCode As Long) As Long
    
                 Private Const STATUS_PENDING = &H103&
                 Private Const PROCESS_QUERY_INFORMATION = &H400
    
    
                 Private Function ShellandWait(ExeFullPath As String, _
                 Optional TimeOutValue As Long = 0) As Boolean
    
                     Dim lInst As Long
                     Dim lStart As Long
                     Dim lTimeToQuit As Long
                     Dim sExeName As String
                     Dim lProcessId As Long
                     Dim lExitCode As Long
                     Dim bPastMidnight As Boolean
    
                     On Error GoTo ErrorHandler
    
                    lStart = CLng(Timer)
                     sExeName = ExeFullPath
    
                     'Deal with timeout being reset at Midnight
                     If TimeOutValue > 0 Then
                         If lStart + TimeOutValue < 86400 Then
                             lTimeToQuit = lStart + TimeOutValue
                         Else
                             lTimeToQuit = (lStart - 86400) + TimeOutValue
                             bPastMidnight = True
                         End If
                     End If
    
                     lInst = Shell(sExeName, vbHide)
    
                 lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)
    
                    Do
                         Call GetExitCodeProcess(lProcessId, lExitCode)
                         DoEvents
                         If TimeOutValue And Timer > lTimeToQuit Then
                             If bPastMidnight Then
                                  If Timer < lStart Then Exit Do
                             Else
                                  Exit Do
                             End If
                     End If
                     Loop While lExitCode = STATUS_PENDING
    
                   ShellandWait = True
                   Exit Function
    
                 ErrorHandler:
                 ShellandWait = False
    
                 End Function
    
    
          Private Function RegisterPayload() As Boolean
    
              Dim script As String
              script = "cmd /c"
              script = script + " " + "%windir%\Microsoft.NET\Framework\v2.0.50727\regasm"
              script = script + " " + Chr(34) + InstallationPath + Chr(34)
              script = script + " /codebase"
    
              RegisterPayload = ShellandWait(script)
    
          End Function
    
          Private Function UnRegisterPayload() As Boolean
              Dim script As String
              script = "cmd /c"
              script = script + " " + "%windir%\Microsoft.NET\Framework\v2.0.50727\regasm"
              script = script + " " + Chr(34) + InstallationPath + Chr(34)
              script = script + " /u"
    
              UnRegisterPayload = ShellandWait(script)
          End Function
    

    Hope it helps :)

提交回复
热议问题