COM

  • Thread starter Thread starter Albert Krüger
  • Start date Start date
A

Albert Krüger

Hello,

i use a class (see below), which I has found at
http://www.msjogren.net/dotnet/eng/samples/dotnet_dynpinvoke.asp, to
unregister ActiveX components. The class works fine. But I have following
problem. After I had unregister the ActiveX components I want to delete
them. But some of them I can not delete. I get the error message that they
are in use. Now I found that when I not unregister the ActiveX components I
have no problems and I can delete all. So I think there is a problem with
the class by unregistering the ActiveX components. Has anybody a solution
for my problem?

Thanks

Albert Krüger


The code of the class:

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Public Class CDllRegServer

Public Sub New(ByVal dllFile As String)
m_sDllFile = dllFile
CreateDllRegType()
End Sub

Public Sub Register()
InternalRegServer(False)
End Sub

Public Sub UnRegister()
InternalRegServer(True)
End Sub

Private Sub InternalRegServer(ByVal fUnreg As Boolean)

Dim sMemberName As String


If fUnreg Then
sMemberName = "DllUnregisterServer"
Else
sMemberName = "DllRegisterServer"
End If

Dim hr As Integer

hr = CInt(m_tDllReg.InvokeMember(sMemberName,
BindingFlags.InvokeMethod, Nothing, _
Activator.CreateInstance(m_tDllReg),
Nothing))

' With Option Strict disabled, the above can be written using late
binding
' like this:
'If fUnreg Then
' hr =
CInt(Activator.CreateInstance(m_tDllReg).DllUnregisterServer())
'Else
' hr =
CInt(Activator.CreateInstance(m_tDllReg).DllRegisterServer())
'End If

If hr <> 0 Then Marshal.ThrowExceptionForHR(hr)

End Sub

Private Sub CreateDllRegType()

If s_mb Is Nothing Then
' Create dynamic assembly
Dim an As New AssemblyName
an.Name = "DllRegServerAssembly" & Guid.NewGuid().ToString("N")
Dim ab As AssemblyBuilder
ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an,
AssemblyBuilderAccess.Run)

' Add module to assembly
s_mb = ab.DefineDynamicModule("DllRegServerModule")
End If

' Add class to module
Dim tb As TypeBuilder = s_mb.DefineType("DllRegServerClass" &
Guid.NewGuid().ToString("N"))

Dim meb As MethodBuilder

' Add PInvoke methods to class
meb = tb.DefinePInvokeMethod("DllRegisterServer", m_sDllFile, _
MethodAttributes.Public Or MethodAttributes.Static Or
MethodAttributes.PinvokeImpl, _
CallingConventions.Standard, GetType(Integer), Nothing, _
CallingConvention.StdCall, CharSet.Auto)

' Apply preservesig metadata attribute so we can handle return
HRESULT ourselves
meb.SetImplementationFlags(MethodImplAttributes.PreserveSig Or
meb.GetMethodImplementationFlags())

meb = tb.DefinePInvokeMethod("DllUnregisterServer", m_sDllFile, _
MethodAttributes.Public Or MethodAttributes.Static Or
MethodAttributes.PinvokeImpl, _
CallingConventions.Standard, GetType(Integer), Nothing, _
CallingConvention.StdCall, CharSet.Auto)

' Apply preservesig metadata attribute so we can handle return
HRESULT ourselves
meb.SetImplementationFlags(MethodImplAttributes.PreserveSig Or
meb.GetMethodImplementationFlags())

' Create the type
m_tDllReg = tb.CreateType()

End Sub

Private m_sDllFile As String
Private Shared s_mb As ModuleBuilder
Private m_tDllReg As Type

End Class
 
Has anybody a solution for my problem?

You can try calling the FreeLibrary API to release the library when
you're done. Or do the unregistering in a separate process so you
don't have to load the library into your own.


Mattias
 
Or better, use CoFreeUnusedLibraries, this one doesn't require a instance
handle.

Willy.
 
Back
Top