diff --git a/go-clr.go b/go-clr.go index dc031ae..0eb44c9 100644 --- a/go-clr.go +++ b/go-clr.go @@ -354,6 +354,8 @@ func InvokeAssembly(methodInfo *MethodInfo, params []string) (stdout string, std Val: uintptr(0), } + defer SafeArrayDestroy(paramSafeArray) + err = methodInfo.Invoke_3(nullVariant, paramSafeArray) if err != nil { stderr = err.Error() diff --git a/safearray.go b/safearray.go index 4d55842..016b7b4 100644 --- a/safearray.go +++ b/safearray.go @@ -312,3 +312,29 @@ func SafeArrayGetUBound(psa *SafeArray, nDim uint32) (uint32, error) { } return plUbound, nil } + +// SafeArrayDestroy Destroys an existing array descriptor and all of the data in the array. +// If objects are stored in the array, Release is called on each object in the array. +// HRESULT SafeArrayDestroy( +// SAFEARRAY *psa +// ); +func SafeArrayDestroy(psa *SafeArray) error { + debugPrint("Entering into safearray.SafeArrayDestroy()...") + + modOleAuto := syscall.MustLoadDLL("OleAut32.dll") + safeArrayDestroy := modOleAuto.MustFindProc("SafeArrayDestroy") + + hr, _, err := safeArrayDestroy.Call( + uintptr(unsafe.Pointer(psa)), + 0, + 0, + ) + + if err != syscall.Errno(0) { + return fmt.Errorf("the oleaut32!SafeArrayDestroy function call returned an error:\n%s", err) + } + if hr != S_OK { + return fmt.Errorf("the oleaut32!SafeArrayDestroy function returned a non-zero HRESULT: 0x%x", hr) + } + return nil +}