掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
ActiveX.EXEでイベントを発生させ、VB.NETで受け取るには? (ID:102433)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
> 上手く動作させることが出来なかったため、質問させて頂きました。 ActiveX 側の「バージョン間の互換性」を、バイナリ互換にしていますか? > Private WithEvents testForm As TestForm 実際のコードは、どのようになっているのでしょうか? コピー & ペーストしたコードなら、 Private WithEvents testForm As testForm か Private WithEvents TestForm As TestForm になっているはず…。 > Public Class TestForm2 > Private Sub FrmMain_Load(…) Handles MyBase.Load > Private Sub frmMain_FormClosed(…) Handles Me.FormClosed TestForm2 に対するイベントハンドラのプレフィックスが FrmMain や frmMain になっているのは、意図的なものでしょうか? # Handles が MyBase だったり Me だったりするのは仕方ないですが。 > このイベントを、VB.NET(初めはVB6の標準EXEでも可)にて受け取りたいのですが、 > どのようにしたら良いのでしょうか? 提示のコードで、VB6 の標準EXE から、何の問題も無く呼べましたので、 提示していない部分に問題があるのでは無いでしょうか。 一応、こちらの検証コードを載せておきます。 '★★★ VB6 ActiveX EXE [TestProject] (スタートモード= ActiveX コンポーネント) '*** フォーム TestForm (ボタン Command1 を用意) Option Explicit Public Event CallingEventForm(ByVal str1 As String, ByVal str2 As String, ByVal str3 As String, ByVal str4 As String) Private Sub AAA_BBB() RaiseEvent CallingEventForm("str1", "str2", "str3", "str4") End Sub Private Sub Command1_Click() AAA_BBB End Sub '*** クラス TestClass (Instancing = MultiUse) Option Explicit Public Event CallingEvent(ByVal str1 As String, ByVal str2 As String, ByVal str3 As String, ByVal str4 As String) Private WithEvents TestForm As TestForm '本来は、別名にした方が無難 Public Sub openForm() Set TestForm = New TestForm TestForm.Show 'Call 無しのメソッド呼び出し End Sub Private Sub TestForm_CallingEventForm(ByVal str1 As String, ByVal str2 As String, ByVal str3 As String, ByVal str4 As String) RaiseEvent CallingEvent(str1, str2, str3, str4) End Sub Public Sub closeForm() Call Unload(TestForm) 'Call 付きのメソッド呼び出し End Sub '★★★ VB6 標準 EXE [Project1] (TestProject.exe を参照設定) '*** フォーム Form1 Option Explicit Private WithEvents TestClass As TestProject.TestClass Private Sub Form_Load() Set TestClass = New TestProject.TestClass TestClass.openForm End Sub Private Sub Form_Unload(Cancel As Integer) TestClass.closeForm End Sub Private Sub TestClass_CallingEvent(ByVal str1 As String, ByVal str2 As String, ByVal str3 As String, ByVal str4 As String) MsgBox str1 & vbCrLf & str2 & vbCrLf & str3 & vbCrLf & str4, vbInformation End Sub '★★★ VB2008 WinForm アプリ [WindowsApplication1] (TestProject.exe を参照設定) '*** フォーム TestForm2 Public Class TestForm2 Private WithEvents testClass As TestProject.TestClass = New TestProject.TestClass Private Sub FrmMain_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load testClass.openForm() End Sub Private Sub frmMain_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed testClass.closeForm() End Sub Private Sub CCC(ByVal str1 As String, ByVal str2 As String, ByVal str3 As String, ByVal str4 As String) Handles testClass.CallingEvent MsgBox(str1 & vbCrLf & str2 & vbCrLf & str3 & vbCrLf & str4, vbInformation) End Sub End Class 蛇足ですが、VB.NET から ActiveX コンポーネントのイベントを利用する場合は、 WithEvents を使うのではなく AddHandler / RemoveHandler を用いた方が、 ReleaseComObject しやすくなります(自動解放に任せるなら WithEvents で充分ですが)。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.