當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。