.net 2010 vb: COM interop DLL WITHEVENTS
-
Tuesday, September 18, 2012 4:59 PM
I am trying to write a COM Interop DLL that instantiates an instance of FileSystemWatcher and sets the Network folder and file name to watch for. When the file appears in the watched folder I want the dll to raise and event "FileReady" so the calling VBA macro will capture the event and then call some VBA macros. "Wait_For_File" sets the path and file name. LogChange raises the event.
This is the class module.
Option Explicit On Option Strict On Imports System Imports System.IO Imports System.Diagnostics Imports System.Runtime.InteropServices Public Delegate Sub FileReadyDelegate(ByVal File_Path As String, ByVal File_Name As String, ByVal Msg As String, ByVal ChangeType As WaitForDirectoryChange.ChangeTypes) <InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)> _ Public Interface IFileReadyEvents <DispId(1)> _ Sub FileReady(ByVal File_Path As String, ByVal File_Name As String, ByVal Msg As String, ByVal ChangeType As WaitForDirectoryChange.ChangeTypes) End Interface Public Interface IWaitForDirectoryChange 'Subs, Functions, Properties go here 'No subs, functions, or events need to be declared here 'to make our events work properly in COM Enum ChangeTypes As Integer All = System.IO.WatcherChangeTypes.All Changed = System.IO.WatcherChangeTypes.Changed Created = System.IO.WatcherChangeTypes.Created Deleted = System.IO.WatcherChangeTypes.Deleted Renamed = System.IO.WatcherChangeTypes.Renamed End Enum End Interface <ComSourceInterfaces(GetType(IFileReadyEvents)), ClassInterface(ClassInterfaceType.None)> _ Public Class WaitForDirectoryChange : Implements IWaitForDirectoryChange Public Event FileReady As FileReadyDelegate Enum ChangeTypes As Integer All = System.IO.WatcherChangeTypes.All Changed = System.IO.WatcherChangeTypes.Changed Created = System.IO.WatcherChangeTypes.Created Deleted = System.IO.WatcherChangeTypes.Deleted Renamed = System.IO.WatcherChangeTypes.Renamed End Enum Dim WatchFolder As FileSystemWatcher Private Property mFile_Path As String Private Property mFile_Name As String Private Property mEvent_Text As String Private Property mFile_Ready As Boolean = False Public Sub Wait_For_File(ByVal File_Path As String, ByVal File_Name As String) If (File_Path + File_Name) = "" Then Err.Raise(555, "Wait_For_File", "Wait_For_File args empty") End If If Right(File_Path, 1) = "\" Then mFile_Path = Left(File_Path, Len(File_Path) - 1) Else mFile_Path = File_Path End If mFile_Name = File_Name WatchFolder = New System.IO.FileSystemWatcher() WatchFolder.Path = mFile_Path WatchFolder.Filter = mFile_Name WatchFolder.NotifyFilter = IO.NotifyFilters.DirectoryName WatchFolder.NotifyFilter = WatchFolder.NotifyFilter Or _ IO.NotifyFilters.FileName WatchFolder.NotifyFilter = WatchFolder.NotifyFilter Or _ IO.NotifyFilters.Attributes WatchFolder.NotifyFilter = WatchFolder.NotifyFilter Or _ IO.NotifyFilters.CreationTime ' add the handler to each event AddHandler WatchFolder.Changed, AddressOf logchange AddHandler WatchFolder.Created, AddressOf logchange AddHandler WatchFolder.Deleted, AddressOf logchange 'Set this property to true to start watching WatchFolder.EnableRaisingEvents = True End Sub Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs) Dim WhatChanged As ChangeTypes If e.ChangeType = IO.WatcherChangeTypes.Changed Then mEvent_Text = "File " & e.FullPath & _ " has been modified" End If If e.ChangeType = IO.WatcherChangeTypes.Created Then mEvent_Text = "File " & e.FullPath & _ " has been created" If UCase(e.Name) = UCase(mFile_Name) Then WatchFolder.EnableRaisingEvents = False WhatChanged = CType(e.ChangeType, ChangeTypes) RaiseEvent FileReady(mFile_Path, mFile_Name, mEvent_Text, WhatChanged) Beep() End If End If If e.ChangeType = IO.WatcherChangeTypes.Deleted Then mEvent_Text = "File " & e.FullPath & _ " has been deleted" End If End Sub Public Sub New() End Sub End Class
All Replies
-
Thursday, September 20, 2012 3:15 AM
Hello,
Please look at this http://stackoverflow.com/questions/24315/is-it-possible-to-raise-an-event-when-a-file-becomes-accessible which may helps.
Think again!
- Marked As Answer by Shanks ZenMicrosoft Contingent Staff, Moderator Tuesday, October 02, 2012 6:23 AM

