Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to implement CreateThread and WaitForSingleObject #39

Open
jamesbcook opened this issue Jun 4, 2014 · 2 comments
Open

Trying to implement CreateThread and WaitForSingleObject #39

jamesbcook opened this issue Jun 4, 2014 · 2 comments

Comments

@jamesbcook
Copy link

I'm trying to get these to work, but don't know where to look if something is failing or working.

CreateThread

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx

Current Code

func CreateThread(attrib, stackSize, startAddress, parameter, createFlag, threadID uintptr) (uintptr, error) {                                                                                                                                                                 
    hand, _, err := createThread.Call(                                                                                                                                                                                                                                     
            attrib,                                                                                                                                                                                                                                                        
            stackSize,                                                                                                                                                                                                                                                     
            startAddress,                                                                                                                                                                                                                                                  
            parameter,                                                                                                                                                                                                                                                     
            createFlag,                                                                                                                                                                                                                                                    
            threadID)                                                                                                                                                                                                                                                      
    return hand, err                                                                                                                                                                                                                                                       
}       

WaitForSingleObject

http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx

Current Code

func WaitForSingleObject(handle, milliSeconds uintptr) (uintptr, error) {                                                                                                                                                                                                      
    value, _, err := waitForSingleObject.Call(                                                                                                                                                                                                                             
            handle,                                                                                                                                                                                                                                                        
            milliSeconds)                                                                                                                                                                                                                                                  
    return value, err                                                                                                                                                                                                                                                      
} 
Vars at the top of the program
var (                                                                                                                                                                                                                                                                          
    kernel32            = syscall.MustLoadDLL("kernel32.dll")                                                                                                                                                                                                              
    VirtualAlloc        = kernel32.MustFindProc("VirtualAlloc")                                                                                                                                                                                                            
    moveMemory          = kernel32.MustFindProc("RtlMoveMemory")                                                                                                                                                                                                           
    createThread        = kernel32.MustFindProc("CreateThread")                                                                                                                                                                                                            
    waitForSingleObject = kernel32.MustFindProc("WaitForSingleObject")                                                                                                                                                                                                     
)                        
@biorhitm
Copy link
Contributor

package main

import (
    "C"
    "fmt"
    "syscall"
    "unsafe"
)

var (
    kernel32            = syscall.NewLazyDLL("kernel32.dll")
    createThread        = kernel32.NewProc("CreateThread")
    waitForSingleObject = kernel32.NewProc("WaitForSingleObject")
    getExitCodeThread   = kernel32.NewProc("GetExitCodeThread")
)

func ThreadFunc(P uintptr) int64 {
    fmt.Printf("Hello from thread\n")
    return 0xAA // or your favorite number
}

func CreateThread(attrib, stackSize, startAddress, parameter, createFlag uintptr,
    threadID *uint) uintptr {
    hand, _, _ := createThread.Call(
        attrib,
        stackSize,
        startAddress,
        parameter,
        createFlag,
        uintptr(unsafe.Pointer(threadID)))
    return hand
}

func WaitForSingleObject(handle, milliSeconds uintptr) uintptr {
    value, _, _ := waitForSingleObject.Call(
        handle,
        milliSeconds)
    return value
}

func GetExitCodeThread(handle uintptr, exitCode *uint) bool {
    value, _, _ := getExitCodeThread.Call(
        handle,
        uintptr(unsafe.Pointer(exitCode)))
    return value != 0
}

func main() {
    var threadId uint = 0

    funcAddr := syscall.NewCallback(ThreadFunc)
    h := CreateThread(0, 0, funcAddr, 0, 0, &threadId)
    if h != 0 {
        fmt.Printf("threadId: %d h: %d\n", threadId, h)

        waitResult := WaitForSingleObject(h, 1000)
        if waitResult == 0 {
            var exitCode uint = 0
            GetExitCodeThread(h, &exitCode)
            fmt.Printf("ThreadExitCode: %d\n", exitCode)
        }
    }
}

@xackery
Copy link

xackery commented Nov 3, 2015

This has been addressed, I assume?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants