summaryrefslogtreecommitdiff
path: root/packages/w32/create_process.go
blob: 9caf9ffc01b9a58d92074614ba3e39fea2e8d746 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2010-2012 The W32 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package w32

import (
	"syscall"
	"unsafe"
)

var (
	kernel32 = syscall.NewLazyDLL("kernel32.dll")

	procCreateProcessW      = kernel32.NewProc("CreateProcessW")
	procTerminateProcess    = kernel32.NewProc("TerminateProcess")
	procGetExitCodeProcess  = kernel32.NewProc("GetExitCodeProcess")
	procWaitForSingleObject = kernel32.NewProc("WaitForSingleObject")
)

// WINBASEAPI WINBOOL WINAPI
// CreateProcessW (
// LPCWSTR lpApplicationName,
// LPWSTR lpCommandLine,
// LPSECURITY_ATTRIBUTES lpProcessAttributes,
// LPSECURITY_ATTRIBUTES lpThreadAttributes
// WINBOOL bInheritHandles
// DWORD dwCreationFlags
// LPVOID lpEnvironment
// LPCWSTR lpCurrentDirectory
// LPSTARTUPINFOW lpStartupInfo
// LPPROCESS_INFORMATION lpProcessInformation
//);
func CreateProcessW(
	lpApplicationName, lpCommandLine string,
	lpProcessAttributes, lpThreadAttributes *SECURITY_ATTRIBUTES,
	bInheritHandles BOOL,
	dwCreationFlags uint32,
	lpEnvironment unsafe.Pointer,
	lpCurrentDirectory string,
	lpStartupInfo *STARTUPINFOW,
	lpProcessInformation *PROCESS_INFORMATION,
) (e error) {

	var lpAN, lpCL, lpCD *uint16
	if len(lpApplicationName) > 0 {
		lpAN, e = syscall.UTF16PtrFromString(lpApplicationName)
		if e != nil {
			return
		}
	}
	if len(lpCommandLine) > 0 {
		lpCL, e = syscall.UTF16PtrFromString(lpCommandLine)
		if e != nil {
			return
		}
	}
	if len(lpCurrentDirectory) > 0 {
		lpCD, e = syscall.UTF16PtrFromString(lpCurrentDirectory)
		if e != nil {
			return
		}
	}

	ret, _, lastErr := procCreateProcessW.Call(
		uintptr(unsafe.Pointer(lpAN)),
		uintptr(unsafe.Pointer(lpCL)),
		uintptr(unsafe.Pointer(lpProcessAttributes)),
		uintptr(unsafe.Pointer(lpProcessInformation)),
		uintptr(bInheritHandles),
		uintptr(dwCreationFlags),
		uintptr(lpEnvironment),
		uintptr(unsafe.Pointer(lpCD)),
		uintptr(unsafe.Pointer(lpStartupInfo)),
		uintptr(unsafe.Pointer(lpProcessInformation)),
	)

	if ret == 0 {
		e = lastErr
	}

	return
}

func CreateProcessQuick(cmd string) (pi PROCESS_INFORMATION, e error) {
	si := &STARTUPINFOW{}
	e = CreateProcessW(
		"",
		cmd,
		nil,
		nil,
		0,
		0,
		unsafe.Pointer(nil),
		"",
		si,
		&pi,
	)
	return
}

func TerminateProcess(hProcess HANDLE, exitCode uint32) (e error) {
	ret, _, lastErr := procTerminateProcess.Call(
		uintptr(hProcess),
		uintptr(exitCode),
	)

	if ret == 0 {
		e = lastErr
	}

	return
}

func GetExitCodeProcess(hProcess HANDLE) (code uintptr, e error) {
	ret, _, lastErr := procGetExitCodeProcess.Call(
		uintptr(hProcess),
		uintptr(unsafe.Pointer(&code)),
	)

	if ret == 0 {
		e = lastErr
	}

	return
}

// DWORD WINAPI WaitForSingleObject(
//   _In_ HANDLE hHandle,
//   _In_ DWORD  dwMilliseconds
// );

func WaitForSingleObject(hHandle HANDLE, msecs uint32) (ok bool, e error) {

	ret, _, lastErr := procWaitForSingleObject.Call(
		uintptr(hHandle),
		uintptr(msecs),
	)

	if ret == WAIT_OBJECT_0 {
		ok = true
		return
	}

	// don't set e for timeouts, or it will be ERROR_SUCCESS which is
	// confusing
	if ret != WAIT_TIMEOUT {
		e = lastErr
	}
	return

}