How to convert "DeviceIoControl()" of Windows to Go

702 views
Skip to first unread message

christian...@gmx.net

unread,
Jun 26, 2013, 5:19:16 PM6/26/13
to golan...@googlegroups.com
Hi there,


I'm currently a little lost in the "syscall" space of Go. I tried to migrate a the following code from C to Go:

DeviceIoControl(fd, IOCTL_FUNCTION, &flag, sizeof(flag), &flag, sizeof(flag), &len, NULL)

But there isn't such a low level function in Go, so I rewrote the "CTL_CODE" Macro from Windows
and tried instead (don't hit me, I'm not very expirienced with Windows and the arguments fitted):

code := CtlCode(FILE_DEVICE_UNKNOWN, IOCTL_FUNCTION, METHOD_BUFFERED, FILE_ANY_ACCESS)
err = syscall.WSAIoctl(handle, code, &flag[0], flagLen, &flag[0], flagLen, &retLen, nil, 0)
if err != nil { ... 

But I allways get an error like "An operation was attempted on something that is not a socket".
I know that, because I try to access an opened character device ...

Question: What is the correct way of converting the "DeviceIoControl()" into Go ?


Thanx a lot,
C.

brainman

unread,
Jun 26, 2013, 8:18:22 PM6/26/13
to golan...@googlegroups.com, christian...@gmx.net
On Thursday, 27 June 2013 07:19:16 UTC+10, christian...@gmx.net wrote:

I'm currently a little lost in the "syscall" space of Go. I tried to migrate a the following code from C to Go:

DeviceIoControl(fd, IOCTL_FUNCTION, &flag, sizeof(flag), &flag, sizeof(flag), &len, NULL)


Show us a complete small program. We will help you to fix it.

Alex

christian...@gmx.net

unread,
Jun 27, 2013, 6:31:42 AM6/27/13
to golan...@googlegroups.com, christian...@gmx.net
Of course - here is the snippet ...

I'm afraid, but ...
- the error is not the equal (don't ask me why - it's a copy-n-paste. It seems I had to init the WSAxxx function somhow)
- the general question is the same: Which is the correct function to use to replace of DeviceIoControl() !?

Thank you,
C.

christian...@gmx.net

unread,
Jun 27, 2013, 6:37:13 AM6/27/13
to golan...@googlegroups.com, christian...@gmx.net
Once again the link - and without the tinsel ...

brainman

unread,
Jun 27, 2013, 8:08:33 AM6/27/13
to golan...@googlegroups.com, christian...@gmx.net
On Thursday, 27 June 2013 20:31:42 UTC+10, christian...@gmx.net wrote:

> ... Which is the correct function to use to replace of DeviceIoControl() !?

If the spec says you must use DeviceIoControl, then nothing else will do. Just use DeviceIoControl. Since DeviceIoControl does not exist in syscall, you have to find it yourself. All these live in dlls, so it is quite simple.

The quick and dirty way is similar to: http://play.golang.org/p/KjypOYjzTD. I was playing with Windows memory, but you should get the idea. Be careful, because no one verify your call parameters, so Windows expect everything to be precise - number of parameters, their types/sizes, their alignment. If you make mistake, you are dead.

Let me know, if you need more help.

Alex

christian...@gmx.net

unread,
Jun 27, 2013, 8:35:47 AM6/27/13
to golan...@googlegroups.com, christian...@gmx.net
Thanx a lot, but the yellow sponge would say ... "alga grits"

C.

christian...@gmx.net

unread,
Jun 27, 2013, 7:41:10 PM6/27/13
to golan...@googlegroups.com, christian...@gmx.net
Again "muchos gracias" - it worked, and only because of your perfect answer.

- Basically it was very easy, but
- I spend a lot of time, because one of the dll constants started with 1, and
- the converting in Go (here from/to uintptr) is allways time consuming pain for me
- And I didn't implement overlapping support and such spicy things.

Jipiie,
C.
Reply all
Reply to author
Forward
0 new messages