Skip to content

Instantly share code, notes, and snippets.

@bitcrazed
Created January 10, 2019 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitcrazed/49a374508c70adc3e485d82e1851f821 to your computer and use it in GitHub Desktop.
Save bitcrazed/49a374508c70adc3e485d82e1851f821 to your computer and use it in GitHub Desktop.
GoTest
package main
import (
"fmt"
"syscall"
"time"
)
type backgroundProcess struct {
readPipeHandle syscall.Handle
writePipeHandle syscall.Handle
procInfo syscall.ProcessInformation
startupInfo syscall.StartupInfo
BufferSize int32
}
func (bg *backgroundProcess) CreateBackgroundProcess(lpCommandLine string) error {
// build some necessities.
var lpProcessAttrs syscall.SecurityAttributes
//lpProcessAttrs.BInheritHandle = true
var lpThreadAttrs syscall.SecurityAttributes
// create the pipe for the process to be read.
var lpPipeSecAttrs syscall.SecurityAttributes
lpCli := syscall.StringToUTF16Ptr(lpCommandLine)
syscall.CreatePipe(&bg.readPipeHandle, &bg.writePipeHandle, &lpPipeSecAttrs, uint32(bg.BufferSize))
err := syscall.CreateProcess(nil, lpCli, &lpProcessAttrs, &lpThreadAttrs, true, 0, nil, nil, &bg.startupInfo, &bg.procInfo)
if err != nil {
return fmt.Errorf("Could not create new process '%s'", lpCommandLine)
}
return err
}
// CloseBackgroundProcess is just a process wrapper for TerminateProcess().
func (bg *backgroundProcess) CloseBackgroundProcess() error {
err := syscall.TerminateProcess(bg.procInfo.Process, 0)
return err
}
func main() {
testBg := &backgroundProcess{
BufferSize: 1024,
}
// load the calculator as a test app that's easy to see.
err := testBg.CreateBackgroundProcess("C:\\Windows\\System32\\notepad.exe")
if err == nil {
// gotta wait for it to instantiate.
time.Sleep(time.Second * 1)
// make sure it closes.
err = testBg.CloseBackgroundProcess()
}
if err != nil {
fmt.Printf("Error: %s\n", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment