本文整理匯總了Golang中syscall.CloseHandle函數的典型用法代碼示例。如果您正苦於以下問題:Golang CloseHandle函數的具體用法?Golang CloseHandle怎麽用?Golang CloseHandle使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了CloseHandle函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetProcessNameMap
func GetProcessNameMap() map[uint32]string {
defer metrics("GetProcessNameMap")(time.Now())
snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0)
if err != nil {
common.Error("Fail to syscall CreateToolhelp32Snapshot: %v", err)
return nil
}
defer syscall.CloseHandle(snapshot)
var procEntry syscall.ProcessEntry32
procEntry.Size = uint32(unsafe.Sizeof(procEntry))
if err = syscall.Process32First(snapshot, &procEntry); err != nil {
common.Error("Fail to syscall Process32First: %v", err)
return nil
}
processNameMap := make(map[uint32]string)
for {
processNameMap[procEntry.ProcessID] = parseProcessName(procEntry.ExeFile)
if err = syscall.Process32Next(snapshot, &procEntry); err != nil {
if err == syscall.ERROR_NO_MORE_FILES {
return processNameMap
}
common.Error("Fail to syscall Process32Next: %v", err)
return nil
}
}
}
示例2: BottomHalf
func (sub *Subprocess) BottomHalf(d *SubprocessData, sig chan *SubprocessResult) {
hProcess := d.platformData.hProcess
hJob := d.platformData.hJob
result := &SubprocessResult{}
var waitResult uint32
waitResult = syscall.WAIT_TIMEOUT
var runState runningState
for result.SuccessCode == 0 && waitResult == syscall.WAIT_TIMEOUT {
waitResult, _ = syscall.WaitForSingleObject(hProcess, uint32(sub.TimeQuantum.Nanoseconds()/1000000))
if waitResult != syscall.WAIT_TIMEOUT {
break
}
_ = UpdateProcessTimes(&d.platformData, result, false)
if sub.MemoryLimit > 0 {
UpdateProcessMemory(&d.platformData, result)
}
runState.Update(sub, result)
}
switch waitResult {
case syscall.WAIT_OBJECT_0:
_ = syscall.GetExitCodeProcess(hProcess, &result.ExitCode)
case syscall.WAIT_TIMEOUT:
for waitResult == syscall.WAIT_TIMEOUT {
syscall.TerminateProcess(hProcess, 0)
waitResult, _ = syscall.WaitForSingleObject(hProcess, 100)
}
}
_ = UpdateProcessTimes(&d.platformData, result, true)
UpdateProcessMemory(&d.platformData, result)
syscall.CloseHandle(hProcess)
if hJob != syscall.InvalidHandle {
syscall.CloseHandle(hJob)
}
sub.SetPostLimits(result)
for _ = range d.startAfterStart {
err := <-d.bufferChan
if err != nil {
log.Error(err)
}
}
if d.stdOut.Len() > 0 {
result.Output = d.stdOut.Bytes()
}
if d.stdErr.Len() > 0 {
result.Error = d.stdErr.Bytes()
}
sig <- result
}
示例3: terminateAndClose
func (d *PlatformData) terminateAndClose() (err error) {
if err = terminateProcessLoop(d.hProcess); err != nil {
return
}
syscall.CloseHandle(d.hThread)
syscall.CloseHandle(d.hProcess)
return
}
示例4: RunAndCollectSysStats
func RunAndCollectSysStats(cmd *exec.Cmd, res *Result, N uint64, prefix string) (string, error) {
initJobOnce.Do(initJob)
childMu.Lock()
children := childProcesses
childProcesses = []syscall.Handle{}
childMu.Unlock()
for _, proc := range children {
syscall.CloseHandle(proc)
}
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
t0 := time.Now()
if err := cmd.Run(); err != nil {
return out.String(), err
}
t1 := time.Now()
res.RunTime = uint64(t1.Sub(t0)) / N
res.Metrics[prefix+"time"] = res.RunTime
childMu.Lock()
children = childProcesses
childProcesses = []syscall.Handle{}
childMu.Unlock()
if len(children) == 0 {
log.Printf("sysStats.Collect: no child processes?")
return out.String(), nil
}
defer func() {
for _, proc := range children {
syscall.CloseHandle(proc)
}
}()
cputime := uint64(0)
rss := uint64(0)
for _, proc := range children {
var Mem PROCESS_MEMORY_COUNTERS
if err := getProcessMemoryInfo(proc, &Mem); err != nil {
log.Printf("GetProcessMemoryInfo failed: %v", err)
return out.String(), nil
}
var CPU syscall.Rusage
if err := syscall.GetProcessTimes(proc, &CPU.CreationTime, &CPU.ExitTime, &CPU.KernelTime, &CPU.UserTime); err != nil {
log.Printf("GetProcessTimes failed: %v", err)
return out.String(), nil
}
cputime += getCPUTime(CPU) / N
rss += uint64(Mem.PeakWorkingSetSize)
}
res.Metrics[prefix+"cputime"] = cputime
res.Metrics[prefix+"rss"] = rss
return out.String(), nil
}
示例5: Close
func (d *dev) Close() (err error) {
d.Drain()
syscall.CloseHandle(d.ev.r)
syscall.CloseHandle(d.ev.w)
if e := syscall.CloseHandle(d.fd); e != nil {
err = d.error("close", e)
}
return nil
}
示例6: Prepare
// Login and load user profile. Also, set finalizer on s to logout() above.
func (s *LoginInfo) Prepare() error {
var err error
if s.Username == "" {
return nil
}
ec := tools.ErrorContext("LoginInfo.Prepare")
s.HUser, err = win32.LogonUser(
syscall.StringToUTF16Ptr(s.Username),
syscall.StringToUTF16Ptr("."),
syscall.StringToUTF16Ptr(s.Password),
win32.LOGON32_LOGON_INTERACTIVE,
win32.LOGON32_PROVIDER_DEFAULT)
if err != nil {
return ec.NewError(err)
}
s.HProfile, err = loadProfile(s.HUser, s.Username)
if err != nil {
syscall.CloseHandle(s.HUser)
s.HUser = syscall.InvalidHandle
return ec.NewError(err)
}
runtime.SetFinalizer(s, logout)
return nil
}
示例7: loop
// TODO(pknap) : doc
func (r *readdcw) loop() {
var n, key uint32
var overlapped *syscall.Overlapped
for {
err := syscall.GetQueuedCompletionStatus(r.cph, &n, &key, &overlapped, syscall.INFINITE)
if key == stateCPClose {
r.Lock()
handle := r.cph
r.cph = syscall.InvalidHandle
r.Unlock()
syscall.CloseHandle(handle)
r.wg.Done()
return
}
if overlapped == nil {
// TODO: check key == rewatch delete or 0(panic)
continue
}
overEx := (*overlappedEx)(unsafe.Pointer(overlapped))
if n == 0 {
r.loopstate(overEx)
} else {
r.loopevent(n, overEx)
if err = overEx.parent.readDirChanges(); err != nil {
// TODO: error handling
}
}
}
}
示例8: TestCreateToolhelp32Snapshot
func TestCreateToolhelp32Snapshot(t *testing.T) {
handle, err := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
if err != nil {
t.Fatal(err)
}
defer syscall.CloseHandle(syscall.Handle(handle))
// Iterate over the snapshots until our PID is found.
pid := uint32(syscall.Getpid())
for {
process, err := Process32Next(handle)
if errors.Cause(err) == syscall.ERROR_NO_MORE_FILES {
break
}
if err != nil {
t.Fatal(err)
}
t.Logf("CreateToolhelp32Snapshot: ProcessEntry32=%v", process)
if process.ProcessID == pid {
assert.EqualValues(t, syscall.Getppid(), process.ParentProcessID)
return
}
}
assert.Fail(t, "Snapshot not found for PID=%v", pid)
}
示例9: mmap
// mmap memory maps a DB's data file.
// Based on: https://github.com/edsrzf/mmap-go
func mmap(db *DB, sz int) error {
if !db.readOnly {
// Truncate the database to the size of the mmap.
if err := db.file.Truncate(int64(sz)); err != nil {
return fmt.Errorf("truncate: %s", err)
}
}
// Open a file mapping handle.
sizelo := uint32(sz >> 32)
sizehi := uint32(sz) & 0xffffffff
h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizelo, sizehi, nil)
if h == 0 {
return os.NewSyscallError("CreateFileMapping", errno)
}
// Create the memory map.
addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
if addr == 0 {
return os.NewSyscallError("MapViewOfFile", errno)
}
// Close mapping handle.
if err := syscall.CloseHandle(syscall.Handle(h)); err != nil {
return os.NewSyscallError("CloseHandle", err)
}
// Convert to a byte array.
db.data = ((*[maxMapSize]byte)(unsafe.Pointer(addr)))
db.datasz = sz
return nil
}
示例10: loadFileId
func (s *winSys) loadFileId() error {
if s.path == "" {
// already done
return nil
}
s.Lock()
defer s.Unlock()
pathp, e := syscall.UTF16PtrFromString(s.path)
if e != nil {
return e
}
h, e := syscall.CreateFile(pathp, 0, 0, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
if e != nil {
return e
}
defer syscall.CloseHandle(h)
var i syscall.ByHandleFileInformation
e = syscall.GetFileInformationByHandle(syscall.Handle(h), &i)
if e != nil {
return e
}
s.path = ""
s.vol = i.VolumeSerialNumber
s.idxhi = i.FileIndexHigh
s.idxlo = i.FileIndexLow
return nil
}
示例11: Open
func Open(filename string) (mf *memfile, err error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
fs, err := f.Stat()
if err != nil {
return nil, err
}
fsize := fs.Size()
fmap, err := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, 0, uint32(fsize), nil)
if err != nil {
return nil, err
}
defer syscall.CloseHandle(fmap)
ptr, err := syscall.MapViewOfFile(fmap, syscall.FILE_MAP_READ, 0, 0, uintptr(fsize))
if err != nil {
return nil, err
}
defer func() {
if recover() != nil {
mf = nil
err = errors.New("Failed option a file")
}
}()
return &memfile{ptr, fsize, (*[1 << 30]byte)(unsafe.Pointer(ptr))[:fsize]}, nil
}
示例12: munmap
// munmap Windows implementation
// Based on: https://github.com/edsrzf/mmap-go
// Based on: https://github.com/boltdb/bolt/bolt_windows.go
func munmap(b []byte) (err error) {
handleLock.Lock()
defer handleLock.Unlock()
addr := (uintptr)(unsafe.Pointer(&b[0]))
if err := syscall.UnmapViewOfFile(addr); err != nil {
return os.NewSyscallError("UnmapViewOfFile", err)
}
handle, ok := handleMap[addr]
if !ok {
// should be impossible; we would've seen the error above
return errors.New("unknown base address")
}
delete(handleMap, addr)
e := syscall.CloseHandle(syscall.Handle(handle))
if e != nil {
return os.NewSyscallError("CloseHandle", e)
}
file, ok := fileMap[addr]
if !ok {
// should be impossible; we would've seen the error above
return errors.New("unknown base address")
}
delete(fileMap, addr)
e = file.Close()
if e != nil {
return errors.New("close file" + e.Error())
}
return nil
}
示例13: munmap
func munmap(b []byte) (err error) {
m := MMap(b)
dh := m.header()
addr := dh.Data
length := uintptr(dh.Len)
flush(addr, length)
err = syscall.UnmapViewOfFile(addr)
if err != nil {
return err
}
handleLock.Lock()
defer handleLock.Unlock()
handle, ok := handleMap[addr]
if !ok {
// should be impossible; we would've errored above
return errors.New("unknown base address")
}
delete(handleMap, addr)
e := syscall.CloseHandle(syscall.Handle(handle))
return os.NewSyscallError("CloseHandle", e)
}
示例14: openDevice
func openDevice(ifPattern string) (*Interface, error) {
createNewTAPDevice()
handle, err := queryNetworkKey()
if err != nil {
Log(Error, "Failed to query Windows registry: %v", err)
return nil, err
}
dev, err := queryAdapters(handle)
if err != nil {
Log(Error, "Failed to query network adapters: %v", err)
return nil, err
}
if dev == nil {
Log(Error, "All devices are in use")
return nil, errors.New("No free TAP devices")
}
if dev.Name == "" {
Log(Error, "Failed to query network adapters: %v", err)
return nil, errors.New("Empty network adapter")
}
err = syscall.CloseHandle(handle)
if err != nil {
Log(Error, "Failed to close retrieved handle: %v", err)
}
return dev, nil
}
示例15: mmapFile
func mmapFile(f *os.File) mmapData {
st, err := f.Stat()
if err != nil {
log.Fatal(err)
}
size := st.Size()
if int64(int(size+4095)) != size+4095 {
log.Fatalf("%s: too large for mmap", f.Name())
}
if size == 0 {
return mmapData{f, nil, nil}
}
h, err := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, uint32(size>>32), uint32(size), nil)
if err != nil {
log.Fatalf("CreateFileMapping %s: %v", f.Name(), err)
}
defer syscall.CloseHandle(syscall.Handle(h))
addr, err := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, 0)
if err != nil {
log.Fatalf("MapViewOfFile %s: %v", f.Name(), err)
}
data := (*[1 << 30]byte)(unsafe.Pointer(addr))
return mmapData{f, data[:size], data[:]}
}