我正在为IIS Hosted Web Core (HWC) WebCoreActivate
和WebCoreShutdown
API进行两次系统调用。我想添加一个用户上下文,所以当在Windows中进行系统调用时,它们会在特定用户的上下文下运行 - 即该用户的权限。
type WebCore struct {
activated bool
Handle syscall.Handle
Credentials syscall.Credential
}
func New() (error, *WebCore) {
hwebcore, err := syscall.LoadLibrary(os.ExpandEnv(`${windir}\system32\inetsrv\hwebcore.dll`))
if err != nil {
return err, nil
}
return nil, &WebCore{
activated: false,
Handle: hwebcore,
}
}
func (w *WebCore) Activate(appHostConfigPath, rootWebConfigPath, instanceName string) error {
if !w.activated {
webCoreActivate, err := syscall.GetProcAddress(w.Handle, "WebCoreActivate")
if err != nil {
return err
}
var nargs uintptr = 3
_, _, exitCode := syscall.Syscall(uintptr(webCoreActivate),
nargs,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(appHostConfigPath))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(rootWebConfigPath))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(instanceName))))
if exitCode != 0 {
return fmt.Errorf("WebCoreActivate returned exit code: %d", exitCode)
}
fmt.Printf("Server Started for %+v\n", instanceName)
w.activated = true
}
return nil
}
func (w *WebCore) Shutdown(immediate int, instanceName string) error {
if w.activated {
webCoreShutdown, err := syscall.GetProcAddress(w.Handle, "WebCoreShutdown")
if err != nil {
return err
}
var nargs uintptr = 1
_, _, exitCode := syscall.Syscall(uintptr(webCoreShutdown),
nargs, uintptr(unsafe.Pointer(&immediate)), 0, 0)
if exitCode != 0 {
return fmt.Errorf("WebCoreShutdown returned exit code: %d", exitCode)
}
fmt.Printf("Server Shutdown for %+v\n", instanceName)
}
return nil
}
我一直在阅读exec.Command().SysProcAttr
结构,以及如何通过syscall.Credentials{}
结构附加特定用户凭据。但是,这似乎仅限于通过os.exec
执行命令。如果我想将特定用户的上下文绑定到Windows中的本机系统调用,我该怎么做?