当前位置: 首页>>代码示例>>Golang>>正文


Golang syscall.WriteFile函数代码示例

本文整理汇总了Golang中syscall.WriteFile函数的典型用法代码示例。如果您正苦于以下问题:Golang WriteFile函数的具体用法?Golang WriteFile怎么用?Golang WriteFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了WriteFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Write

func Write(fd int, p []byte) (n int, errno int) {
	var mode uint32
	var done uint32
	if isConsole, _ := GetConsoleMode(int32(fd), &mode); UnicodeConsoleOutput && isConsole {
		// TODO: The number of TCHARs to write. If the total size of the
		// specified number of characters exceeds 64 KB, the function fails with ERROR_NOT_ENOUGH_MEMORY.
		buf16 := utf16.Encode([]int(string(p)))
		//for _, c := range buf16 { print(c," ") } ; println()
		if ok, e := WriteConsole(int32(fd), buf16, &done); !ok {
			return 0, e
		}
		// convert length of utf16 characters to number of bytes written
		if done == uint32(len(buf16)) {
			done = uint32(len(p))
		} else {
			done = 0
			for _, rune := range utf16.Decode(buf16[:done]) {
				done += uint32(utf8.RuneLen(rune))
			}
		}
	} else {
		// TODO: This might as well fail with large writes, only Microsoft doesn't say that, see
		// http://code.google.com/p/msysgit/issues/detail?id=409 for example
		if ok, e := syscall.WriteFile(int32(fd), p, &done, nil); !ok {
			return 0, e
		}
	}
	return int(done), 0
}
开发者ID:tajtiattila,项目名称:test,代码行数:29,代码来源:ct.go

示例2: Write

func (d *winDevice) Write(data []byte) error {
	// first make sure we send the correct amount of data to the device
	outSize := int(d.info.OutputReportLength)
	buffer := make([]byte, outSize, outSize)
	copy(buffer, data)

	ol := new(syscall.Overlapped)
	if err := syscall.WriteFile(d.handle, buffer, nil, ol); err != nil {
		// IO Pending is ok we simply wait for it to finish a few lines below
		// all other errors should be reported.
		if err != syscall.ERROR_IO_PENDING {
			return err
		}
	}

	// now wait for the overlapped device access to finish.
	var written C.DWORD
	if C.GetOverlappedResult(d.h(), (*C.OVERLAPPED)((unsafe.Pointer)(ol)), &written, C.TRUE) == 0 {
		return syscall.GetLastError()
	}

	if int(written) != outSize {
		return errors.New("written bytes missmatch!")
	}
	return nil
}
开发者ID:tam7t,项目名称:hid,代码行数:26,代码来源:hid_windows.go

示例3: Write

func (p *Port) Write(buf []byte) (int, error) {
	p.wl.Lock()
	defer p.wl.Unlock()

	fmt.Println("Reset Event")

	if err := resetEvent(p.wo.HEvent); err != nil {
		return 0, err
	}

	fmt.Println("Reset Event Done")

	var n uint32

	fmt.Println("WriteFile")
	//Public Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
	//   ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToWrite As Int32, _
	//      ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As Boolean
	// Success = WriteFile(hParallelPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
	err := syscall.WriteFile(p.fd, buf, &n, p.wo)

	fmt.Println("WriteFile Done")
	if err != nil && err != syscall.ERROR_IO_PENDING {
		return int(n), err
	}

	return getOverlappedResult(p.fd, p.wo)
}
开发者ID:kity-xu,项目名称:xuxiaodong,代码行数:28,代码来源:parallel_windows.go

示例4: Write

// Write writes data to the serial port.
func (p *port) Write(b []byte) (n int, err error) {
	var done uint32
	if err = syscall.WriteFile(p.handle, b, &done, nil); err != nil {
		return
	}
	n = int(done)
	return
}
开发者ID:akofoed,项目名称:serial,代码行数:9,代码来源:serial_windows.go

示例5: Write

// Write writes to a file handle.
func (f *win32File) Write(b []byte) (int, error) {
	c, err := f.prepareIo()
	if err != nil {
		return 0, err
	}
	var bytes uint32
	err = syscall.WriteFile(f.handle, b, &bytes, &c.o)
	return f.asyncIo(c, f.writeDeadline, bytes, err)
}
开发者ID:ChristianKniep,项目名称:swarmkit,代码行数:10,代码来源:file.go

示例6: Write

// Write implements the net.Conn Write method.
func (c *PipeConn) Write(b []byte) (int, error) {
	overlapped, err := newOverlapped()
	if err != nil {
		return 0, err
	}
	var n uint32
	err = syscall.WriteFile(c.handle, b, &n, overlapped)
	return c.completeRequest(iodata{n, err}, c.writeDeadline, overlapped)
}
开发者ID:postfix,项目名称:npipe,代码行数:10,代码来源:npipe_windows.go

示例7: write

func (p *Port) write(b []byte) (n int, err error) {
	n = 0
	for {
		var done uint32
		err = syscall.WriteFile(p.handle, b[n:], &done, nil)
		n += int(done)
		if n == len(b) {
			break
		}
	}
	return
}
开发者ID:daemonzzz,项目名称:serial,代码行数:12,代码来源:serial_windows.go

示例8: Write

func (p *virtioPort) Write(buf []byte) (int, error) {
	p.wl.Lock()
	defer p.wl.Unlock()
	if err := resetEvent(p.wo.HEvent); err != nil {
		return 0, err
	}
	var n uint32
	err := syscall.WriteFile(p.fd, buf, &n, p.wo)
	if err != nil && err != syscall.ERROR_IO_PENDING {
		return int(n), err
	}
	return getOverlappedResult(p.fd, p.wo)
}
开发者ID:npe9,项目名称:minimega,代码行数:13,代码来源:virtio_windows.go

示例9: Write

func (p *serialPort) Write(buf []byte) (int, os.Error) {
	p.wl.Lock()
	defer p.wl.Unlock()

	if err := resetEvent(p.wo.HEvent); err != nil {
		return 0, err
	}
	var n uint32
	e := syscall.WriteFile(p.fd, buf, &n, p.wo)
	if e != 0 && e != syscall.ERROR_IO_PENDING {
		return int(n), errno(uintptr(e))
	}
	return getOverlappedResult(p.fd, p.wo)
}
开发者ID:rwen,项目名称:goserial,代码行数:14,代码来源:serial_windows.go

示例10: write

func (p *Port) write(b []byte) (n int, err error) {
	for {
		var done uint32
		err = syscall.WriteFile(p.handle, b[n:], &done, nil)
		n += int(done)

		if err != nil || n == len(b) {
			break
		}
	}

	if err != nil {
		err = p.flush()
	}

	return
}
开发者ID:datawolf,项目名称:go-study,代码行数:17,代码来源:serial_windows.go

示例11: Write

func (dev *SerialDevice) Write(buf []byte) (int, error) {
	dev.wl.Lock()
	defer dev.wl.Unlock()

	if err := syscallEvent(dev.wo.HEvent, "reset"); err != nil {
		return 0, err
	}

	var n uint32
	err := syscall.WriteFile(dev.fd, buf, &n, dev.wo)
	if err != nil && err != syscall.ERROR_IO_PENDING {
		fmt.Printf("%v\n", err)
		return int(n), fmt.Errorf("%#v", err)
	}

	return getOverlapResult(dev.fd, dev.wo)
}
开发者ID:shotantan,项目名称:5DPrint,代码行数:17,代码来源:serial_windows.go

示例12: Write

func (p *serialPort) Write(buf []byte) (int, error) {
	p.wl.Lock()
	defer p.wl.Unlock()

	if err := resetEvent(p.wo.HEvent); err != nil {
		return 0, err
	}
	var n uint32
	err := syscall.WriteFile(p.fd, buf, &n, p.wo)
	//fmt.Printf("n %d  err %v\n", n, err)
	_ = fmt.Printf
	if err != nil && err != syscall.ERROR_IO_PENDING {
		//fmt.Printf("returning...\n")
		return int(n), err
	}
	return getOverlappedResult(p.fd, p.wo)
}
开发者ID:distributed,项目名称:sers,代码行数:17,代码来源:sers_windows.go

示例13: Write

func (t *Interface) Write(ch chan []byte) (err error) {
	tx := syscall.Overlapped{}
	var hevent windows.Handle
	hevent, err = windows.CreateEvent(nil, 0, 0, nil)
	if err != nil {
		return
	}
	tx.HEvent = syscall.Handle(hevent)
	for {
		select {
		case data := <-ch:
			var l uint32
			syscall.WriteFile(t.file, data, &l, &tx)
			syscall.WaitForSingleObject(tx.HEvent, syscall.INFINITE)
			tx.Offset += uint32(len(data))
		}
	}
}
开发者ID:subutai-io,项目名称:p2p,代码行数:18,代码来源:tuntap_windows.go

示例14: pwrite

// pwrite writes len(b) bytes to the File starting at byte offset off.
// It returns the number of bytes written and an error, if any.
func (f *File) pwrite(b []byte, off int64) (n int, err error) {
	f.l.Lock()
	defer f.l.Unlock()
	curoffset, e := syscall.Seek(f.fd, 0, 1)
	if e != nil {
		return 0, e
	}
	defer syscall.Seek(f.fd, curoffset, 0)
	o := syscall.Overlapped{
		OffsetHigh: uint32(off >> 32),
		Offset:     uint32(off),
	}
	var done uint32
	e = syscall.WriteFile(syscall.Handle(f.fd), b, &done, &o)
	if e != nil {
		return 0, e
	}
	return int(done), nil
}
开发者ID:timnau,项目名称:golang,代码行数:21,代码来源:file_windows.go

示例15: Write

func (p *dev) Write(buf []byte) (int, error) {
	var done uint32

	for {
		var ov syscall.Overlapped

		ov.HEvent = p.ev.w
		if e := syscall.WriteFile(p.fd, buf, &done, &ov); e != nil {
			if e != syscall.ERROR_IO_PENDING {
				return 0, &os.PathError{"writing to", p.name, e}
			}
			if e = win.GetOverlappedResult(p.fd, &ov, &done, 1); e != nil {
				return 0, &os.PathError{"writing to", p.name, e}
			}
		}
		if done > 0 {
			break
		}
	}
	return int(done), nil
}
开发者ID:jeffallen,项目名称:g,代码行数:21,代码来源:windows.go


注:本文中的syscall.WriteFile函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。