Incorrect behavior of AttachConsole (WIN32 API function) using Syscall (using Cgo is OK)

283 views
Skip to first unread message

Pavel Korotkov

unread,
Oct 30, 2013, 5:47:43 PM10/30/13
to golan...@googlegroups.com
Hi gophers,

I noticed incorrect behavior of function AttachConsole in the code attached when using syscall.FindProc(...) - ....Call(...) approach. While, Cgo version works as expected (see files).
Test bench OSes: Windows 7 Professional x86-64 and Windows 8.1 Enterprise x86-64.
Could you please help me track the problem in my code? Any open issue, maybe?

Thanks in advance

Best regards,
PK
allocconsole.go
main.go

brainman

unread,
Oct 30, 2013, 6:34:08 PM10/30/13
to golan...@googlegroups.com
On Thursday, 31 October 2013 08:47:43 UTC+11, Pavel Korotkov wrote:

> ... I noticed incorrect behavior ...

Tell us about that. Tell us what your problem is. Thank you.

Alex

brainman

unread,
Oct 30, 2013, 7:59:45 PM10/30/13
to golan...@googlegroups.com
On Thu, Oct 31, 2013 at 9:50 AM, Pavel Korotkov <...> wrote:
>
> Well, I've got two AllocConsole wrappers:
>
> package main
>
> /*
> #define _WIN32_WINNT 0x0502
> #include <windows.h>
> */
> import "C"
>
> ...
>
> func AttachConsoleToParentProcess1_WINAPI() error {
>     funName := "AttachConsole"
>
>     r, err := C.AttachConsole(C.ATTACH_PARENT_PROCESS)
>     if r == 0 {
>         return fmt.Errorf(funName+"'s return flag is FALSE; Last error: %s", err.Error())
>     }
>     return nil
> }
>
> func AttachConsoleToParentProcess2_WINAPI() error {
>     funName := "AttachConsole"
>     fun, err := kernel32.FindProc(funName)
>     if err != nil {
>         return err
>     }
>
>     var app uint32 = 0xFFFFFFFF // Constant ATTACH_PARENT_PROCESS as stated in MSDN: (DWORD)-1
>     r, _, lastErr := fun.Call(uintptr(unsafe.Pointer(&app)))
>     if r == 0 {
>         return fmt.Errorf(funName+"'s return flag is FALSE; Last error: %s", lastErr.Error())
>     }
>
>     return nil
> }
>
> When I try to attach a console back to the parent process (main.go:55), only AttachConsoleToParentProcess1_WINAPI does the job. AttachConsoleToParentProcess2_WINAPI doesn't work on my machines. That's it. What else can I do?
>
> PS: Since I use Win7/Win8.1 one can omit '#define _WIN32_WINNT 0x0502' directive on top of the file.
>

I am sorry, but I don't understand what "does the job" and "doesn't work" means. Perhaps, if you can give us a program, and then tell us what you see when the program runs, and then tell us what you think is wrong with the output you see and why. Thank you.

Alex

brainman

unread,
Oct 30, 2013, 10:01:58 PM10/30/13
to golan...@googlegroups.com
I still do not know what your problem is, but in AttachConsoleToParentProcess2_WINAPI you have a bug. You should have:

r, _, lastErr := fun.Call(uintptr(app))

not

r, _, lastErr := fun.Call(uintptr(unsafe.Pointer(&app)))

Alex

Pavel Korotkov

unread,
Oct 31, 2013, 4:45:58 AM10/31/13
to brainman, golan...@googlegroups.com
Hi Alex

I was just about to describe the problem in detail... but you've already find the bug! Thank you very much!


On Thu, Oct 31, 2013 at 6:01 AM, brainman <alex.b...@gmail.com> wrote:
I still do not know what your problem is, but in AttachConsoleToParentProcess2_WINAPI you have a bug. You should have:

r, _, lastErr := fun.Call(uintptr(app))

You're right, that's an unfortunate copy-paste misprint. And this the reason why my program didn't work.
not

r, _, lastErr := fun.Call(uintptr(unsafe.Pointer(&app)))

Alex

--
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/ASbbFuSR448/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

С уважением,
Павел Коротков
Reply all
Reply to author
Forward
0 new messages