Springen naar inhoud


- - - - -
VS 2010 SOLVED

Late Binding Van Com Object Events

VS2010 COM Objects Late Binding

  • Log in a.u.b. om te beantwoorden
Er zijn 2 reacties in dit onderwerp

#1 Vozzie

Vozzie

    Moderator

  • Leden
  • PipPipPipPipPipPipPip
  • 1855 berichten
    Laatst bezocht 12 jul 2017 13:15
Inzender

Geplaatst op 17 maart 2013 - 20:26

Hy,

Weet iemand of en hoe dit kan in VB.Net? Het gaat over het "late" binden van events. In VB heb je AddHandler maar daarmee lukt het (mij) niet. Heb al zitten zoeken naar "Type.GetEvents" maar daar het een "COM Object" is, geeft dat ook geen resultaat...


Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Test test = new Test();
test.test();
Console.WriteLine("Ready...");
Console.ReadKey();
}
}

public delegate void OnQuitDelegate();

internal class Test
{
dynamic ie;
bool gekwiet;

public void ie_OnQuit()
{
Console.WriteLine("kwiet");
gekwiet = true;
}

public void test()
{
gekwiet = false;
Type t = Type.GetTypeFromProgID("InternetExplorer.Application");
ie = Activator.CreateInstance(t);
ie.Navigate("about:blank");
while (ie.ReadyState != 4) System.Threading.Thread.Sleep(5);
ie.OnQuit += new OnQuitDelegate(ie_OnQuit);
ie.Quit();
while(!gekwiet /* && todo:timeout */)
System.Windows.Forms.Application.DoEvents();
}
}
}


#2 Vozzie

Vozzie

    Moderator

  • Leden
  • PipPipPipPipPipPipPip
  • 1855 berichten
    Laatst bezocht 12 jul 2017 13:15
Inzender

Geplaatst op 17 maart 2013 - 21:21

Heb een mogelijke oplossing in mekaar geflanst,... Niet van het netste maar het werkt :)...

Visual Basic Code:
Option Strict Off

Imports Microsoft.CSharp
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Reflection

Public Module Module1

	Public Delegate Sub OnQuitDelegate()

	Private gekwiet As Boolean

	Sub ie_OnQuit()
		Console.WriteLine("kwiet")
		gekwiet = True
	End Sub

	Sub Main()
		gekwiet = False
		Dim eventName As String = "OnQuit"
		Dim quitEvent As IDynamicEvent = DynamicEvent.CreateDynamicEvent(eventName)
		Dim ie As Object = CreateObject("InternetExplorer.Application")
		'Dim iet As Type = ie.GetType()
		quitEvent.AddHandler(ie, New OnQuitDelegate(AddressOf ie_OnQuit))
		ie.Navigate("about:blank")
		Do Until ie.ReadyState = 4 : Threading.Thread.Sleep(5) : Loop
		ie.Quit()
		Do Until gekwiet : Threading.Thread.Sleep(5) : Loop
		Console.WriteLine("Ready...")
		Console.ReadKey()
	End Sub

End Module

Public Interface IDynamicEvent

	Sub [AddHandler](ByVal source As Object, ByVal del As [Delegate])
	Sub [RemoveHandler](ByVal source As Object, ByVal del As [Delegate])

End Interface

Friend NotInheritable Class DynamicEvent

	Friend Sub New()
	End Sub


	Friend Shared Function CreateDynamicEvent(ByVal name As String) As IDynamicEvent
		Using provider As CodeDomProvider = New CSharpCodeProvider()
			Dim parameters As New CompilerParameters() With {
				.GenerateInMemory = True
			}
			parameters.ReferencedAssemblies.AddRange({
				"System.dll",
				"System.Core.dll",
				"System.Data.dll",
				Assembly.GetExecutingAssembly().Location,
				"Microsoft.CSharp.dll"
			})
			Dim code As String = String.Format(
				"using System;" & vbCrLf &
				"" & vbCrLf &
				"namespace DynamicEvent {{" & vbCrLf &
				"   public class Dynamic{0}Event : {1}.IDynamicEvent" & vbCrLf &
				"   {{" & vbCrLf &
				"	   public void AddHandler(object source, Delegate del)" & vbCrLf &
				"	   {{" & vbCrLf &
				"		   dynamic dyn = source;" & vbCrLf &
				"		   dyn.{0} += del;" & vbCrLf &
				"	   }}" & vbCrLf &
				"	   public void RemoveHandler(object source, Delegate del)" & vbCrLf &
				"	   {{" & vbCrLf &
				"		   dynamic dyn = source;" & vbCrLf &
				"		   dyn.{0} -= del;" & vbCrLf &
				"	   }}" & vbCrLf &
				"   }}" & vbCrLf &
				"}}", name, GetType(IDynamicEvent).Namespace)

			Dim result As CompilerResults = provider.CompileAssemblyFromSource(parameters, code)
			If result.Errors.Count = 0 Then
				Dim codeType As Type = result.CompiledAssembly.GetType(String.Format("DynamicEvent.Dynamic{0}Event", name))
				Dim codeObject As Object = Activator.CreateInstance(codeType)
				Return DirectCast(codeObject, IDynamicEvent)
			Else
				Return Nothing
			End If
		End Using
	End Function

End Class


#3 Vozzie

Vozzie

    Moderator

  • Leden
  • PipPipPipPipPipPipPip
  • 1855 berichten
    Laatst bezocht 12 jul 2017 13:15
Inzender

Geplaatst op 17 maart 2013 - 23:01

Om niet voor elke event een interface @ runtime te maken heb ik de code aangepast om voor meerdere events tegelijk interfaces te maken....

Opgelost met csharp script,...

Visual Basic Code:
Option Strict Off

Imports Microsoft.CSharp
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.Text

Public Module Module1

Public Delegate Sub OnQuitDelegate()
Public Delegate Sub DocumentCompleteDelegate(ByVal pDisp As Object, ByVal url As Object)

Private ie As Object
Private gekwiet As Boolean

Sub ie_DocumentComplete(ByVal pDisp As Object, ByVal URL As Object)
Console.WriteLine(URL)
ie.Quit()
End Sub

Sub ie_OnQuit()
Console.WriteLine("kwiet")
gekwiet = True
End Sub

Sub Main()
Dim eventHandlers() As [Delegate]
Dim dynamicEvents As Dictionary(Of String, IDynamicEvent)
eventHandlers = {New OnQuitDelegate(AddressOf ie_OnQuit), New DocumentCompleteDelegate(AddressOf ie_DocumentComplete)}
dynamicEvents = DynamicEvent.CreateDynamicEventList({"OnQuit", "DocumentComplete"})
ie = CreateObject("InternetExplorer.Application")
For i As Integer = 0 To dynamicEvents.Count - 1
dynamicEvents(dynamicEvents.Keys(i)).AddHandler(ie, eventHandlers(i))
Next
ie.Navigate("about:blank")
Do Until gekwiet : System.Windows.Forms.Application.DoEvents() : Loop
Console.WriteLine("Ready...")
Console.ReadKey()
End Sub

End Module

Public Interface IDynamicEvent

Sub [AddHandler](ByVal source As Object, ByVal del As [Delegate])
Sub [RemoveHandler](ByVal source As Object, ByVal del As [Delegate])

End Interface

Friend NotInheritable Class DynamicEvent

Private Sub New()
End Sub

Friend Shared Function CreateDynamicEventList(ByVal names() As String) As Dictionary(Of String, IDynamicEvent)
Dim result As New Dictionary(Of String, IDynamicEvent)
Using provider As CodeDomProvider = New CSharpCodeProvider()
Dim parameters As New CompilerParameters() With {
.GenerateInMemory = True
}
parameters.ReferencedAssemblies.AddRange({
"System.dll",
"System.Core.dll",
"System.Data.dll",
Assembly.GetExecutingAssembly().Location,
"Microsoft.CSharp.dll"
})
Dim uid As String = Guid.NewGuid().ToString("N")
Dim code As New StringBuilder()
code.Append(
"using System;" & vbCrLf &
"" & vbCrLf &
"namespace DynamicEvent {" & vbCrLf
)
For Each name As String In names
code.Append(
String.Format(
"public class Dynamic{0}Event{2} : {1}.IDynamicEvent" & vbCrLf &
"{{" & vbCrLf &
" public void AddHandler(dynamic source, Delegate del) {{ source.{0} += del; }}" & vbCrLf &
" public void RemoveHandler(dynamic source, Delegate del) {{ source.{0} -= del; }}" & vbCrLf &
"}}" & vbCrLf,
name,
GetType(IDynamicEvent).Namespace,
uid
)
)
Next
code.Append("}")
Dim codeResult As CompilerResults = provider.CompileAssemblyFromSource(parameters, code.ToString())
If codeResult.Errors.Count = 0 Then
For Each name As String In names
Dim codeType As Type = codeResult.CompiledAssembly.GetType(String.Format("DynamicEvent.Dynamic{0}Event{1}", name, uid))
Dim codeObject As Object = Activator.CreateInstance(codeType)
result.Add(name, DirectCast(codeObject, IDynamicEvent))
Next
Else
' todo: Compiler errors...
'Return Nothing
End If
End Using
Return result
End Function

Friend Shared Function CreateDynamicEvent(ByVal name As String) As IDynamicEvent
Return CreateDynamicEventList({name})(name)
End Function

End Class






Ook met taq VS 2010 SOLVED, VS2010, COM, Objects, Late Binding voorzien

0 gebruiker(s) lezen dit onderwerp

0 lid(leden), 0 bezoeker(s), 0 anonieme gebruikers

Inloggen


[VS 2010 SOLVED] Untitled 1

Met dank aan Jürgen voor de jarenlange inzet van visualbasic.be (anno dec 2000)
Met dank aan Mike en Ronneke voor de jarenlange inzet van vbib.be (anno dec 2010)
Met dank aan PascalBianca voor de jarenlange inzet van vbib.be (anno dec 2016)