FAQ
At wrapping a Windows function, how to handle a argument which is just a
struct (no pointer to struct)?

i.e.for the SetConsoleCursorPosition function:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025%28v=vs.85%29.aspx

* * *
type COORD struct {
     X, Y int16
}

func SetConsoleCursorPosition(hConsoleOutput syscall.Handle,
dwCursorPosition COORD) (err error) {
     r1, _, e1 := syscall.Syscall(procSetConsoleCursorPosition.Addr(), 2,
uintptr(hConsoleOutput), uintptr(dwCursorPosition), 0)
     if int(r1) == 0 {
         if e1 != 0 {
             err = error(e1)
         } else {
             err = syscall.EINVAL
         }
     }
     return
}
* * *
cannot convert dwCursorPosition (type COORD) to type uintptr

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • peterGo at May 27, 2013 at 7:43 am
    Archos,

    type COORD uint32

    Peter
    On Monday, May 27, 2013 3:24:30 AM UTC-4, Archos wrote:

    At wrapping a Windows function, how to handle a argument which is just a
    struct (no pointer to struct)?

    i.e.for the SetConsoleCursorPosition function:

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025%28v=vs.85%29.aspx

    * * *
    type COORD struct {
    X, Y int16
    }

    func SetConsoleCursorPosition(hConsoleOutput syscall.Handle,
    dwCursorPosition COORD) (err error) {
    r1, _, e1 := syscall.Syscall(procSetConsoleCursorPosition.Addr(), 2,
    uintptr(hConsoleOutput), uintptr(dwCursorPosition), 0)
    if int(r1) == 0 {
    if e1 != 0 {
    err = error(e1)
    } else {
    err = syscall.EINVAL
    }
    }
    return
    }
    * * *
    cannot convert dwCursorPosition (type COORD) to type uintptr
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Archos at May 27, 2013 at 8:02 am
    It solves it, thanks. But I have two questions:

    + How to convert a struct to a number type? I'm supposed that using package
    encoding/binary
    + How to handle it when the struct has several fields that don't fit into a
    int32? using []int32?

    El lunes, 27 de mayo de 2013 08:43:47 UTC+1, peterGo escribió:
    Archos,

    type COORD uint32

    Peter
    On Monday, May 27, 2013 3:24:30 AM UTC-4, Archos wrote:

    At wrapping a Windows function, how to handle a argument which is just a
    struct (no pointer to struct)?

    i.e.for the SetConsoleCursorPosition function:

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025%28v=vs.85%29.aspx

    * * *
    type COORD struct {
    X, Y int16
    }

    func SetConsoleCursorPosition(hConsoleOutput syscall.Handle,
    dwCursorPosition COORD) (err error) {
    r1, _, e1 := syscall.Syscall(procSetConsoleCursorPosition.Addr(), 2,
    uintptr(hConsoleOutput), uintptr(dwCursorPosition), 0)
    if int(r1) == 0 {
    if e1 != 0 {
    err = error(e1)
    } else {
    err = syscall.EINVAL
    }
    }
    return
    }
    * * *
    cannot convert dwCursorPosition (type COORD) to type uintptr
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • peterGo at May 27, 2013 at 8:24 am
    Archos,

    + How to convert a struct to a number type? I'm supposed that using package
    encoding/binary

    package main

    import "fmt"

    type COORD uint32

    type Coord struct {
         X, Y int16
    }

    func CoordToCOORD(coord Coord) COORD {
         return COORD(uint16(coord.X))<<16 | COORD(uint16(coord.Y))
    }

    func COORDToCoord(coord COORD) Coord {
         return Coord{int16(coord >> 16), int16(coord)}
    }

    func main() {
         fmt.Println(COORDToCoord(CoordToCOORD(Coord{42, 7})))
    }

    Output:

    {42 7}

    http://play.golang.org/p/eMIbPcahkf

    + How to handle it when the struct has several fields that don't fit into a
    int32? using []int32?

    If the struct fields don't fit into a uint32 then it will be a pointer to
    the struct.

    Peter
    On Monday, May 27, 2013 4:02:09 AM UTC-4, Archos wrote:

    It solves it, thanks. But I have two questions:

    + How to convert a struct to a number type? I'm supposed that using
    package encoding/binary
    + How to handle it when the struct has several fields that don't fit into
    a int32? using []int32?

    El lunes, 27 de mayo de 2013 08:43:47 UTC+1, peterGo escribió:
    Archos,

    type COORD uint32

    Peter
    On Monday, May 27, 2013 3:24:30 AM UTC-4, Archos wrote:

    At wrapping a Windows function, how to handle a argument which is just a
    struct (no pointer to struct)?

    i.e.for the SetConsoleCursorPosition function:

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025%28v=vs.85%29.aspx

    * * *
    type COORD struct {
    X, Y int16
    }

    func SetConsoleCursorPosition(hConsoleOutput syscall.Handle,
    dwCursorPosition COORD) (err error) {
    r1, _, e1 := syscall.Syscall(procSetConsoleCursorPosition.Addr(), 2,
    uintptr(hConsoleOutput), uintptr(dwCursorPosition), 0)
    if int(r1) == 0 {
    if e1 != 0 {
    err = error(e1)
    } else {
    err = syscall.EINVAL
    }
    }
    return
    }
    * * *
    cannot convert dwCursorPosition (type COORD) to type uintptr
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Archos at May 27, 2013 at 8:46 am
    Thanks, it works like a charm.

    El lunes, 27 de mayo de 2013 09:24:15 UTC+1, peterGo escribió:
    Archos,

    + How to convert a struct to a number type? I'm supposed that using
    package encoding/binary

    package main

    import "fmt"

    type COORD uint32

    type Coord struct {
    X, Y int16
    }

    func CoordToCOORD(coord Coord) COORD {
    return COORD(uint16(coord.X))<<16 | COORD(uint16(coord.Y))
    }

    func COORDToCoord(coord COORD) Coord {
    return Coord{int16(coord >> 16), int16(coord)}
    }

    func main() {
    fmt.Println(COORDToCoord(CoordToCOORD(Coord{42, 7})))
    }

    Output:

    {42 7}

    http://play.golang.org/p/eMIbPcahkf

    + How to handle it when the struct has several fields that don't fit into
    a int32? using []int32?

    If the struct fields don't fit into a uint32 then it will be a pointer to
    the struct.

    Peter
    On Monday, May 27, 2013 4:02:09 AM UTC-4, Archos wrote:

    It solves it, thanks. But I have two questions:

    + How to convert a struct to a number type? I'm supposed that using
    package encoding/binary
    + How to handle it when the struct has several fields that don't fit into
    a int32? using []int32?

    El lunes, 27 de mayo de 2013 08:43:47 UTC+1, peterGo escribió:
    Archos,

    type COORD uint32

    Peter
    On Monday, May 27, 2013 3:24:30 AM UTC-4, Archos wrote:

    At wrapping a Windows function, how to handle a argument which is just
    a struct (no pointer to struct)?

    i.e.for the SetConsoleCursorPosition function:

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025%28v=vs.85%29.aspx

    * * *
    type COORD struct {
    X, Y int16
    }

    func SetConsoleCursorPosition(hConsoleOutput syscall.Handle,
    dwCursorPosition COORD) (err error) {
    r1, _, e1 := syscall.Syscall(procSetConsoleCursorPosition.Addr(),
    2, uintptr(hConsoleOutput), uintptr(dwCursorPosition), 0)
    if int(r1) == 0 {
    if e1 != 0 {
    err = error(e1)
    } else {
    err = syscall.EINVAL
    }
    }
    return
    }
    * * *
    cannot convert dwCursorPosition (type COORD) to type uintptr
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • peterGo at May 27, 2013 at 9:24 am
    Archos,

    Since WinTel is little-endian, uint32 is probably YX, not XY.

    package main

    import "fmt"

    type COORD uint32

    type Coord struct {
         X, Y int16
    }

    func CoordToCOORD(coord Coord) COORD {
         return COORD(uint16(coord.Y))<<16 | COORD(uint16(coord.X))
    }

    func COORDToCoord(coord COORD) Coord {
         return Coord{int16(coord), int16(coord >> 16)}
    }

    func main() {
         fmt.Println(COORDToCoord(CoordToCOORD(Coord{42, 7})))
    }

    Output:

    {42 7}

    http://play.golang.org/p/ahmYPC06bw

    Run a test on Windows to see which one is right.

    Peter
    On 5/27/13, Archos wrote:
    Thanks, it works like a charm.

    El lunes, 27 de mayo de 2013 09:24:15 UTC+1, peterGo escribió:
    Archos,

    + How to convert a struct to a number type? I'm supposed that using
    package encoding/binary

    package main

    import "fmt"

    type COORD uint32

    type Coord struct {
    X, Y int16
    }

    func CoordToCOORD(coord Coord) COORD {
    return COORD(uint16(coord.X))<<16 | COORD(uint16(coord.Y))
    }

    func COORDToCoord(coord COORD) Coord {
    return Coord{int16(coord >> 16), int16(coord)}
    }

    func main() {
    fmt.Println(COORDToCoord(CoordToCOORD(Coord{42, 7})))
    }

    Output:

    {42 7}

    http://play.golang.org/p/eMIbPcahkf

    + How to handle it when the struct has several fields that don't fit into

    a int32? using []int32?

    If the struct fields don't fit into a uint32 then it will be a pointer to

    the struct.

    Peter
    On Monday, May 27, 2013 4:02:09 AM UTC-4, Archos wrote:

    It solves it, thanks. But I have two questions:

    + How to convert a struct to a number type? I'm supposed that using
    package encoding/binary
    + How to handle it when the struct has several fields that don't fit into

    a int32? using []int32?

    El lunes, 27 de mayo de 2013 08:43:47 UTC+1, peterGo escribió:
    Archos,

    type COORD uint32

    Peter
    On Monday, May 27, 2013 3:24:30 AM UTC-4, Archos wrote:

    At wrapping a Windows function, how to handle a argument which is just

    a struct (no pointer to struct)?

    i.e.for the SetConsoleCursorPosition function:

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025%28v=vs.85%29.aspx

    * * *
    type COORD struct {
    X, Y int16
    }

    func SetConsoleCursorPosition(hConsoleOutput syscall.Handle,
    dwCursorPosition COORD) (err error) {
    r1, _, e1 := syscall.Syscall(procSetConsoleCursorPosition.Addr(),
    2, uintptr(hConsoleOutput), uintptr(dwCursorPosition), 0)
    if int(r1) == 0 {
    if e1 != 0 {
    err = error(e1)
    } else {
    err = syscall.EINVAL
    }
    }
    return
    }
    * * *
    cannot convert dwCursorPosition (type COORD) to type uintptr
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/xedBAo1V6ws/unsubscribe?hl=en-US.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Archos at May 27, 2013 at 9:48 am
    Yes, this one is the right.

    El lunes, 27 de mayo de 2013 10:24:24 UTC+1, peterGo escribió:
    Archos,

    Since WinTel is little-endian, uint32 is probably YX, not XY.

    package main

    import "fmt"

    type COORD uint32

    type Coord struct {
    X, Y int16
    }

    func CoordToCOORD(coord Coord) COORD {
    return COORD(uint16(coord.Y))<<16 | COORD(uint16(coord.X))
    }

    func COORDToCoord(coord COORD) Coord {
    return Coord{int16(coord), int16(coord >> 16)}
    }

    func main() {
    fmt.Println(COORDToCoord(CoordToCOORD(Coord{42, 7})))
    }

    Output:

    {42 7}

    http://play.golang.org/p/ahmYPC06bw

    Run a test on Windows to see which one is right.

    Peter
    On 5/27/13, Archos <raul...@sent.com <javascript:>> wrote:
    Thanks, it works like a charm.

    El lunes, 27 de mayo de 2013 09:24:15 UTC+1, peterGo escribió:
    Archos,

    + How to convert a struct to a number type? I'm supposed that using
    package encoding/binary

    package main

    import "fmt"

    type COORD uint32

    type Coord struct {
    X, Y int16
    }

    func CoordToCOORD(coord Coord) COORD {
    return COORD(uint16(coord.X))<<16 | COORD(uint16(coord.Y))
    }

    func COORDToCoord(coord COORD) Coord {
    return Coord{int16(coord >> 16), int16(coord)}
    }

    func main() {
    fmt.Println(COORDToCoord(CoordToCOORD(Coord{42, 7})))
    }

    Output:

    {42 7}

    http://play.golang.org/p/eMIbPcahkf

    + How to handle it when the struct has several fields that don't fit
    into
    a int32? using []int32?

    If the struct fields don't fit into a uint32 then it will be a pointer
    to
    the struct.

    Peter
    On Monday, May 27, 2013 4:02:09 AM UTC-4, Archos wrote:

    It solves it, thanks. But I have two questions:

    + How to convert a struct to a number type? I'm supposed that using
    package encoding/binary
    + How to handle it when the struct has several fields that don't fit
    into
    a int32? using []int32?

    El lunes, 27 de mayo de 2013 08:43:47 UTC+1, peterGo escribió:
    Archos,

    type COORD uint32

    Peter
    On Monday, May 27, 2013 3:24:30 AM UTC-4, Archos wrote:

    At wrapping a Windows function, how to handle a argument which is
    just
    a struct (no pointer to struct)?

    i.e.for the SetConsoleCursorPosition function:
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025%28v=vs.85%29.aspx
    * * *
    type COORD struct {
    X, Y int16
    }

    func SetConsoleCursorPosition(hConsoleOutput syscall.Handle,
    dwCursorPosition COORD) (err error) {
    r1, _, e1 :=
    syscall.Syscall(procSetConsoleCursorPosition.Addr(),
    2, uintptr(hConsoleOutput), uintptr(dwCursorPosition), 0)
    if int(r1) == 0 {
    if e1 != 0 {
    err = error(e1)
    } else {
    err = syscall.EINVAL
    }
    }
    return
    }
    * * *
    cannot convert dwCursorPosition (type COORD) to type uintptr
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/xedBAo1V6ws/unsubscribe?hl=en-US.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Archos at May 27, 2013 at 9:06 am
    Just pretty (for reference):

    type COORD struct {
         X, Y int16
    }

    type COORD_u uint32

    func (c COORD) ToUint() COORD_u {
         return COORD_u(uint16(c.X))<<16 | COORD_u(uint16(c.Y))
    }

    func (c COORD_u) ToCoord() COORD {
         return COORD{int16(c >> 16), int16(c)}
    }

    El lunes, 27 de mayo de 2013 09:24:15 UTC+1, peterGo escribió:
    Archos,

    + How to convert a struct to a number type? I'm supposed that using
    package encoding/binary

    package main

    import "fmt"

    type COORD uint32

    type Coord struct {
    X, Y int16
    }

    func CoordToCOORD(coord Coord) COORD {
    return COORD(uint16(coord.X))<<16 | COORD(uint16(coord.Y))
    }

    func COORDToCoord(coord COORD) Coord {
    return Coord{int16(coord >> 16), int16(coord)}
    }

    func main() {
    fmt.Println(COORDToCoord(CoordToCOORD(Coord{42, 7})))
    }

    Output:

    {42 7}

    http://play.golang.org/p/eMIbPcahkf
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 27, '13 at 7:24a
activeMay 27, '13 at 9:48a
posts8
users2
websitegolang.org

2 users in discussion

Archos: 5 posts peterGo: 3 posts

People

Translate

site design / logo © 2022 Grokbase