ole32.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2010-2012 The W32 Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build windows
  5. package w32
  6. import (
  7. "syscall"
  8. "unsafe"
  9. )
  10. var (
  11. modole32 = syscall.NewLazyDLL("ole32.dll")
  12. procCoInitializeEx = modole32.NewProc("CoInitializeEx")
  13. procCoInitialize = modole32.NewProc("CoInitialize")
  14. procCoUninitialize = modole32.NewProc("CoUninitialize")
  15. procCreateStreamOnHGlobal = modole32.NewProc("CreateStreamOnHGlobal")
  16. )
  17. func CoInitializeEx(coInit uintptr) HRESULT {
  18. ret, _, _ := procCoInitializeEx.Call(
  19. 0,
  20. coInit)
  21. switch uint32(ret) {
  22. case E_INVALIDARG:
  23. panic("CoInitializeEx failed with E_INVALIDARG")
  24. case E_OUTOFMEMORY:
  25. panic("CoInitializeEx failed with E_OUTOFMEMORY")
  26. case E_UNEXPECTED:
  27. panic("CoInitializeEx failed with E_UNEXPECTED")
  28. }
  29. return HRESULT(ret)
  30. }
  31. func CoInitialize() {
  32. procCoInitialize.Call(0)
  33. }
  34. func CoUninitialize() {
  35. procCoUninitialize.Call()
  36. }
  37. func CreateStreamOnHGlobal(hGlobal HGLOBAL, fDeleteOnRelease bool) *IStream {
  38. stream := new(IStream)
  39. ret, _, _ := procCreateStreamOnHGlobal.Call(
  40. uintptr(hGlobal),
  41. uintptr(BoolToBOOL(fDeleteOnRelease)),
  42. uintptr(unsafe.Pointer(&stream)))
  43. switch uint32(ret) {
  44. case E_INVALIDARG:
  45. panic("CreateStreamOnHGlobal failed with E_INVALIDARG")
  46. case E_OUTOFMEMORY:
  47. panic("CreateStreamOnHGlobal failed with E_OUTOFMEMORY")
  48. case E_UNEXPECTED:
  49. panic("CreateStreamOnHGlobal failed with E_UNEXPECTED")
  50. }
  51. return stream
  52. }