Results 1 to 6 of 6

Thread: Assembly.LoadFrom HOW to do events (AddHandler )?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    Assembly.LoadFrom HOW to do events (AddHandler )?

    Loading Assemblies using Assembly.Load, Assembly.LoadFrom and Assembly.LoadFile - CodeProject
    https://www.codeproject.com/articles...s-using-assemb
    i want to load com dll from vb.net

    and make a vb.net dll for vb6
    it's can load (*.dll) in vb6,callbyname,or bind events,how to do?

    Code:
    Dim sw  As WebSocketServer = New WebSocketBuilder(8889).Build()
    		
    		AddHandler sw.NewSessionConnected, AddressOf Sw_NewSessionConnected
    		AddHandler sw.NewMessageReceived, sub(wss As WebSocketSession  , msg As String )  
    		                                  	c = System.Math.Max(System.Threading.Interlocked.Increment(c),c - 1)
    		                                  	Console.WriteLine("count =" & c & "次" & Left( msg,100))
    
    		                                  	File.WriteAllText( path1 & c & ".htm", MSG, Encoding.Default)
    		                                  	If c>2000 Then c=0
    		                                  End sub
    Code:
    Dim asm As System.Reflection.Assembly  = System.Reflection.Assembly.LoadFrom(DLL2)
    Dim ServerConfig1 As Object = asm.CreateInstance("SuperSocket.SocketBase.Config.ServerConfig", False )
    
    	ServerConfig1.ip="127.0.0.1"
    	ServerConfig1.port=8889
    	ServerConfig1.MaxConnectionNumber = 1024
    	ServerConfig1.MaxRequestLength = 1024 * 1024 * 1024

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    Re: Assembly.LoadFrom HOW to do events (AddHandler )?

    AssemblyResolve event not firing?
    https://social.msdn.microsoft.com/Fo...forum=netfxbcl

    D1 is loaded with:

    Dim myAssembly As [Assembly] = [Assembly].LoadFrom(strLoadPath, Assembly.GetExecutingAssembly().Evidence)



    It fires just fine for DLL's that the main executable directly references but not for DLL's that were initially loaded using Assembly.LoadFrom(<dll path>).



    Currently everything is running in a single appdomain and the event handler is added for the CurrentDomain.



    AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveAssemblyHandler



    I want to move these child modules to separate appdomains but I have some shared data issues to resolve first.



    If I can get the event to fire I can get the file downloaded and loaded in memory.



    Anybody have any ideas?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    Re: Assembly.LoadFrom HOW to do events (AddHandler )?

    Code:
    		Dim AssDll1 As System.Reflection.Assembly  = System.Reflection.Assembly.LoadFrom(DLL)
    		Dim WebSocketServerType As Type = AssDll1.GetType("SuperSocket.Websocket.WebSocketServer")			
    		_wsServer   = AssDll1.CreateInstance("SuperSocket.Websocket.WebSocketServer", True)	
    		
    		WebSocketServerType=_wsServer.GetType
    		'=================================
    		'Take the event object
    		Dim Event_NewSessionConnected As EventInfo = WebSocketServerType.GetEvent("NewSessionConnected")		
    		'Take the procedure for VB6 to bind to the event
    		Dim MyEvents_NewSessionConnected As MethodInfo = Me.GetType.GetMethod("Sw_NewSessionConnected")
    		'Constructing event handles
    		Dim Handler As [Delegate] = [Delegate].CreateDelegate(Event_NewSessionConnected.EventHandlerType, Me, MyEvents_NewSessionConnected) '
    		'Binding event to the VB6 procedure
    		Event_NewSessionConnected.AddEventHandler(_wsServer, Handler)
    Code:
    	Public   Sub Sw_NewSessionConnected(session As Object ) ' SuperSocket.WebSocket.WebSocketSession
    		debug.Print ("Sw_NewSessionConnected2")
    		session.Send("linkok")		
    	End Sub

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    Re: Assembly.LoadFrom HOW to do events (AddHandler )?

    Code:
    Call BindEvent(WebSocketServerType,"NewSessionConnected","Sw_NewSessionConnected")
    
    	Sub BindEvent(ObjTypeItem As type,EventName As String,MySubName As String )
    		Dim Event1 As EventInfo = ObjTypeItem.GetEvent(EventName)		
    	
    		Dim MySubForEvent As MethodInfo = Me.GetType.GetMethod(MySubName)
    
    		Dim Handler1 As [Delegate] = [Delegate].CreateDelegate(Event1.EventHandlerType, Me, MySubForEvent) '
    
    		Event1.AddEventHandler(_wsServer, Handler1)		
    	End Sub
    It is convenient to make a general function binding event. Is there a way to take over all the events and deal with them in a sub?

    Code:
    sub allevent(object,arg)
    'arg.name//arg.value
    end sub

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    Re: Assembly.LoadFrom HOW to do events (AddHandler )?

    Code:
    Private Sub WireAllEvents(ByVal obj As Object)
        'Grab all of the events for the supplied object
        Dim Events = obj.GetType().GetEvents()
        'This points to the method that we want to invoke each time
        Dim HandlerMethod = Me.GetType().GetMethod("GlobalHandler")
        'Loop through all of the events
        For Each ev In Events
            'Wire in a handler for the event
            ev.AddEventHandler(obj, [Delegate].CreateDelegate(ev.EventHandlerType, Me, HandlerMethod))
        Next
    End Sub
    Public Sub GlobalHandler(ByVal sender As Object, ByVal e As EventArgs)
        'Probably want to do something more meaningful here than just tracing
        Trace.WriteLine(e)
    End Sub

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    Re: Assembly.LoadFrom HOW to do events (AddHandler )?

    c# - Binding an event handler to any type of event using reflection - Stack Overflow
    https://stackoverflow.com/questions/...ing-reflection

    Code:
    First create this helper class:
    
    public class HandlerHelper<T> where T : EventArgs
    {
        private readonly EventHandler m_HandlerToCall;
    
        public HandlerHelper(EventHandler handler_to_call)
        {
            m_HandlerToCall = handler_to_call;
        }
    
        public void Handle(object sender, T args)
        {
            m_HandlerToCall.Invoke(sender, args);
        }
    }
    This generic class has a Handle method that will be used to be our delegate for the many event handler types. Note that this class is generic. T will be one of the many EventArgs derived classes for different event types.
    Code:
    foreach (var event_name in event_names)
    {
        var event_info = control.GetType().GetEvent(event_name);
    
        var event_handler_type = event_info.EventHandlerType;
    
        var event_args_type = event_handler_type.GetMethod("Invoke").GetParameters()[1].ParameterType;
    
        var helper_type = typeof(HandlerHelper<>).MakeGenericType(event_args_type);
    
        var helper = Activator.CreateInstance(helper_type, event_handler);
    
        Delegate my_delegate = Delegate.CreateDelegate(event_handler_type, helper, "Handle");
    
        event_info.AddEventHandler(button, my_delegate);
    }
    how to run in vb.net?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width