Skip to content

Commit

Permalink
Notification via Windows API.
Browse files Browse the repository at this point in the history
  • Loading branch information
liudng committed Feb 12, 2015
1 parent 46255a8 commit 7c32980
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
Binary file added l-ubuntu-inotify-pdf.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions notification_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2014 The dogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html
package main
85 changes: 85 additions & 0 deletions notification_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2014 The dogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// https://msdn.microsoft.com/en-us/library/aa365261(v=vs.85).aspx
package main

import (
"syscall"
"unsafe"
)

var kernel32 = syscall.NewLazyDLL("kernel32.dll")
var (
procFindFirstChangeNotification = kernel32.NewProc("FindFirstChangeNotification")
procFindNextChangeNotification = kernel32.NewProc("FindNextChangeNotification")
procFindCloseChangeNotification = kernel32.NewProc("FindCloseChangeNotification")
procReadDirectoryChangesW = kernel32.NewProc("ReadDirectoryChangesW")
procWaitForMultipleObjects = kernel32.NewProc("WaitForMultipleObjects")
)

func boolToUint32(b bool) uint32 {
if b {
return 1
} else {
return 0
}
}

// HANDLE WINAPI FindFirstChangeNotification(
// _In_ LPCTSTR lpPathName,
// _In_ BOOL bWatchSubtree,
// _In_ DWORD dwNotifyFilter
// );
func FindFirstChangeNotification(pathName string, watchSubTree bool, mask uint32) (handle syscall.Handle, err error) {
r1, _, e1 := syscall.Syscall(
procFindFirstChangeNotification.Addr(),
3,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(pathName))),
uintptr(boolToUint32(watchSubTree)),
uintptr(mask))

handle = syscall.Handle(r1)
if handle == syscall.InvalidHandle {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

// BOOL WINAPI ReadDirectoryChangesW(
// _In_ HANDLE hDirectory,
// _Out_ LPVOID lpBuffer,
// _In_ DWORD nBufferLength,
// _In_ BOOL bWatchSubtree,
// _In_ DWORD dwNotifyFilter,
// _Out_opt_ LPDWORD lpBytesReturned,
// _Inout_opt_ LPOVERLAPPED lpOverlapped,
// _In_opt_ LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
// );
func ReadDirectoryChanges(handle syscall.Handle, buf *byte, buflen uint32, watchSubTree bool, mask uint32, retlen *uint32, overlapped *syscall.Overlapped, completionRoutine uintptr) (err error) {
r1, _, e1 := syscall.Syscall9(
procReadDirectoryChangesW.Addr(),
8,
uintptr(handle),
uintptr(unsafe.Pointer(buf)),
uintptr(buflen),
uintptr(boolToUint32(watchSubTree)),
uintptr(mask),
uintptr(unsafe.Pointer(retlen)),
uintptr(unsafe.Pointer(overlapped)),
uintptr(completionRoutine),
0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

0 comments on commit 7c32980

Please sign in to comment.