references in runtime

  • Thread starter Thread starter Mattias Sjögren
  • Start date Start date
Miguel Atilio Godoy Gadea said:
Can I add reference of a dll in runtime ?? how?

Here is some code I found on the web for registering a VB6 dll. It works
well. Perhaps it may give you some leads for .net:

Public Function RegisterComponent(ByVal FileName$, _
ByVal RegFunction As REGISTER_FUNCTIONS) As STATUS

'**********************************************************************************
'Author: Vasudevan S
'Helena, MT
'Function: RegisterComponent
'Purpose: Registers/Unregisters any ActiveX DLL/EXE/OCX component
'Entry Points in ActiveX DLL/EXE/OCX are DllRegisterServer and
DllUnRegisterServer
'Input: FileName: Any valid file with complete path
'RegFunction: Enumerated Type(DllRegisterServer,
DllUnregisterServer)
'Returns: Returns the status of the call in a enumerated type
'Comments: The utility REGSVR32.EXE need not be used to register/unregister
ActiveX
'components. This code can be embedded inside any application
that needs
'to register/unregister any ActiveX component from within the
code base
'SAMPLE FORM IS INCLUDED
'WORKS IN VB5.0/6.0

'HOW TO CALL:
'-----------
'Dim mEnum As STATUS
'
'TO REGISTER A COMPONENT USE
'mEnum = RegisterComponent("C:\windows\system\filename.dll",
DllRegisterServer) 'to Register
'
'If mEnum = [File Could Not Be Loaded Into Memory Space] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [Not A Valid ActiveX Component] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [ActiveX Component Registration Failed] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [ActiveX Component Registered Successfully] Then
' MsgBox "Your Message Here", vbExclamation
'End If
'
'TO UNREGISTER A COMPONENT USE
'mEnum = RegisterComponent("C:\windows\system\filename.dll",
DllUnRegisterServer) 'to UnRegister
'
'If mEnum = [File Could Not Be Loaded Into Memory Space] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [Not A Valid ActiveX Component] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [ActiveX Component Registration Failed] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [ActiveX Component UnRegistered Successfully] Then
' MsgBox "Your Message Here", vbExclamation
'End If
'************************************************************************************


Dim lngLib&, lngProcAddress&, lpThreadID&, fSuccess&, dwExitCode&, hThread&

If FileName = "" Then Exit Function

lngLib = LoadLibraryRegister(FileName)
If lngLib = 0 Then
RegisterComponent = [File Could Not Be Loaded Into Memory Space]
'Couldn't load component
Exit Function
End If

Select Case RegFunction
Case REGISTER_FUNCTIONS.DllRegisterServer
lngProcAddress = GetProcAddressRegister(lngLib, "DllRegisterServer")
Case REGISTER_FUNCTIONS.DllUnRegisterServer
lngProcAddress = GetProcAddressRegister(lngLib, "DllUnregisterServer")
Case Else
End Select

If lngProcAddress = 0 Then
RegisterComponent = [Not A Valid ActiveX Component] 'Not a
Valid ActiveX Component
If lngLib Then Call FreeLibraryRegister(lngLib)
Exit Function
Else
hThread = CreateThreadForRegister(ByVal 0&, 0&, ByVal lngProcAddress,
ByVal 0&, 0&, lpThreadID)
If hThread Then
fSuccess = (WaitForSingleObject(hThread, 10000) = WAIT_OBJECT_0)
If Not fSuccess Then
Call GetExitCodeThread(hThread, dwExitCode)
Call ExitThread(dwExitCode)
RegisterComponent = [ActiveX Component Registration Failed]
'Couldn't Register.
If lngLib Then Call FreeLibraryRegister(lngLib)
Exit Function
Else
If RegFunction = DllRegisterServer Then
RegisterComponent = [ActiveX Component Registered
Successfully] 'Success. OK
ElseIf RegFunction = DllUnRegisterServer Then
RegisterComponent = [ActiveX Component UnRegistered
Successfully] 'Success. OK
End If
End If
Call CloseHandle(hThread)
If lngLib Then Call FreeLibraryRegister(lngLib)
End If
End If
End Function
 
I try to put a dll in an external folder to the project, and make the
reference in runtime only, because I need this to detect that dll has, and
according to that I make a menu of options, each dll is separated a porject
with its own installer
 
Ok, Thanks !

Harry Strybos said:
Miguel Atilio Godoy Gadea said:
Can I add reference of a dll in runtime ?? how?

Here is some code I found on the web for registering a VB6 dll. It works
well. Perhaps it may give you some leads for .net:

Public Function RegisterComponent(ByVal FileName$, _
ByVal RegFunction As REGISTER_FUNCTIONS) As STATUS

'**********************************************************************************
'Author: Vasudevan S
'Helena, MT
'Function: RegisterComponent
'Purpose: Registers/Unregisters any ActiveX DLL/EXE/OCX component
'Entry Points in ActiveX DLL/EXE/OCX are DllRegisterServer and
DllUnRegisterServer
'Input: FileName: Any valid file with complete path
'RegFunction: Enumerated Type(DllRegisterServer,
DllUnregisterServer)
'Returns: Returns the status of the call in a enumerated type
'Comments: The utility REGSVR32.EXE need not be used to
register/unregister ActiveX
'components. This code can be embedded inside any application
that needs
'to register/unregister any ActiveX component from within the
code base
'SAMPLE FORM IS INCLUDED
'WORKS IN VB5.0/6.0

'HOW TO CALL:
'-----------
'Dim mEnum As STATUS
'
'TO REGISTER A COMPONENT USE
'mEnum = RegisterComponent("C:\windows\system\filename.dll",
DllRegisterServer) 'to Register
'
'If mEnum = [File Could Not Be Loaded Into Memory Space] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [Not A Valid ActiveX Component] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [ActiveX Component Registration Failed] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [ActiveX Component Registered Successfully] Then
' MsgBox "Your Message Here", vbExclamation
'End If
'
'TO UNREGISTER A COMPONENT USE
'mEnum = RegisterComponent("C:\windows\system\filename.dll",
DllUnRegisterServer) 'to UnRegister
'
'If mEnum = [File Could Not Be Loaded Into Memory Space] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [Not A Valid ActiveX Component] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [ActiveX Component Registration Failed] Then
' MsgBox "Your Message Here", vbExclamation
'ElseIf mEnum = [ActiveX Component UnRegistered Successfully] Then
' MsgBox "Your Message Here", vbExclamation
'End If
'************************************************************************************


Dim lngLib&, lngProcAddress&, lpThreadID&, fSuccess&, dwExitCode&,
hThread&

If FileName = "" Then Exit Function

lngLib = LoadLibraryRegister(FileName)
If lngLib = 0 Then
RegisterComponent = [File Could Not Be Loaded Into Memory Space]
'Couldn't load component
Exit Function
End If

Select Case RegFunction
Case REGISTER_FUNCTIONS.DllRegisterServer
lngProcAddress = GetProcAddressRegister(lngLib, "DllRegisterServer")
Case REGISTER_FUNCTIONS.DllUnRegisterServer
lngProcAddress = GetProcAddressRegister(lngLib, "DllUnregisterServer")
Case Else
End Select

If lngProcAddress = 0 Then
RegisterComponent = [Not A Valid ActiveX Component] 'Not a
Valid ActiveX Component
If lngLib Then Call FreeLibraryRegister(lngLib)
Exit Function
Else
hThread = CreateThreadForRegister(ByVal 0&, 0&, ByVal lngProcAddress,
ByVal 0&, 0&, lpThreadID)
If hThread Then
fSuccess = (WaitForSingleObject(hThread, 10000) = WAIT_OBJECT_0)
If Not fSuccess Then
Call GetExitCodeThread(hThread, dwExitCode)
Call ExitThread(dwExitCode)
RegisterComponent = [ActiveX Component Registration Failed]
'Couldn't Register.
If lngLib Then Call FreeLibraryRegister(lngLib)
Exit Function
Else
If RegFunction = DllRegisterServer Then
RegisterComponent = [ActiveX Component Registered
Successfully] 'Success. OK
ElseIf RegFunction = DllUnRegisterServer Then
RegisterComponent = [ActiveX Component UnRegistered
Successfully] 'Success. OK
End If
End If
Call CloseHandle(hThread)
If lngLib Then Call FreeLibraryRegister(lngLib)
End If
End If
End Function
 
Miguel said:
Can I add reference of a dll in runtime ?? how?

You don't need to.
Load the Assembly in code, then use Activator.CreateInstance to create
objects from it.
Better still, have your dynamically loaded classes implement a
[compile-time] known Interface; that way you get all the benefits of
Early-bound Type Safety, but can load /any/ Assembly at run-time, so
long as [the class[es] within] it implement the Interface.

HTH,
Phill W.
 
Back
Top