github.com/hspan/go-ole@v0.0.0/oleutil/oleutil.go (about)

     1  package oleutil
     2  
     3  import ole "github.com/hspan/go-ole"
     4  
     5  // ClassIDFrom retrieves class ID whether given is program ID or application string.
     6  func ClassIDFrom(programID string) (classID *ole.GUID, err error) {
     7  	return ole.ClassIDFrom(programID)
     8  }
     9  
    10  // CreateObject creates object from programID based on interface type.
    11  //
    12  // Only supports IUnknown.
    13  //
    14  // Program ID can be either program ID or application string.
    15  func CreateObject(programID string) (unknown *ole.IUnknown, err error) {
    16  	classID, err := ole.ClassIDFrom(programID)
    17  	if err != nil {
    18  		return
    19  	}
    20  
    21  	unknown, err = ole.CreateInstance(classID, ole.IID_IUnknown)
    22  	if err != nil {
    23  		return
    24  	}
    25  
    26  	return
    27  }
    28  
    29  // GetActiveObject retrieves active object for program ID and interface ID based
    30  // on interface type.
    31  //
    32  // Only supports IUnknown.
    33  //
    34  // Program ID can be either program ID or application string.
    35  func GetActiveObject(programID string) (unknown *ole.IUnknown, err error) {
    36  	classID, err := ole.ClassIDFrom(programID)
    37  	if err != nil {
    38  		return
    39  	}
    40  
    41  	unknown, err = ole.GetActiveObject(classID, ole.IID_IUnknown)
    42  	if err != nil {
    43  		return
    44  	}
    45  
    46  	return
    47  }
    48  
    49  // CallMethod calls method on IDispatch with parameters.
    50  func CallMethod(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT, err error) {
    51  	return disp.InvokeWithOptionalArgs(name, ole.DISPATCH_METHOD, params)
    52  }
    53  
    54  // MustCallMethod calls method on IDispatch with parameters or panics.
    55  func MustCallMethod(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT) {
    56  	r, err := CallMethod(disp, name, params...)
    57  	if err != nil {
    58  		panic(err.Error())
    59  	}
    60  	return r
    61  }
    62  
    63  // GetProperty retrieves property from IDispatch.
    64  func GetProperty(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT, err error) {
    65  	return disp.InvokeWithOptionalArgs(name, ole.DISPATCH_PROPERTYGET, params)
    66  }
    67  
    68  // MustGetProperty retrieves property from IDispatch or panics.
    69  func MustGetProperty(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT) {
    70  	r, err := GetProperty(disp, name, params...)
    71  	if err != nil {
    72  		panic(err.Error())
    73  	}
    74  	return r
    75  }
    76  
    77  // PutProperty mutates property.
    78  func PutProperty(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT, err error) {
    79  	return disp.InvokeWithOptionalArgs(name, ole.DISPATCH_PROPERTYPUT, params)
    80  }
    81  
    82  // MustPutProperty mutates property or panics.
    83  func MustPutProperty(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT) {
    84  	r, err := PutProperty(disp, name, params...)
    85  	if err != nil {
    86  		panic(err.Error())
    87  	}
    88  	return r
    89  }
    90  
    91  // PutPropertyRef mutates property reference.
    92  func PutPropertyRef(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT, err error) {
    93  	return disp.InvokeWithOptionalArgs(name, ole.DISPATCH_PROPERTYPUTREF, params)
    94  }
    95  
    96  // MustPutPropertyRef mutates property reference or panics.
    97  func MustPutPropertyRef(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT) {
    98  	r, err := PutPropertyRef(disp, name, params...)
    99  	if err != nil {
   100  		panic(err.Error())
   101  	}
   102  	return r
   103  }
   104  
   105  func ForEach(disp *ole.IDispatch, f func(v *ole.VARIANT) error) error {
   106  	newEnum, err := disp.GetProperty("_NewEnum")
   107  	if err != nil {
   108  		return err
   109  	}
   110  	defer newEnum.Clear()
   111  
   112  	enum, err := newEnum.ToIUnknown().IEnumVARIANT(ole.IID_IEnumVariant)
   113  	if err != nil {
   114  		return err
   115  	}
   116  	defer enum.Release()
   117  
   118  	for item, length, err := enum.Next(1); length > 0; item, length, err = enum.Next(1) {
   119  		if err != nil {
   120  			return err
   121  		}
   122  		if ferr := f(&item); ferr != nil {
   123  			return ferr
   124  		}
   125  	}
   126  	return nil
   127  }