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

     1  package ole
     2  
     3  // Connection contains IUnknown for fluent interface interaction.
     4  //
     5  // Deprecated. Use oleutil package instead.
     6  type Connection struct {
     7  	Object *IUnknown // Access COM
     8  }
     9  
    10  // Initialize COM.
    11  func (*Connection) Initialize() (err error) {
    12  	return coInitialize()
    13  }
    14  
    15  // Uninitialize COM.
    16  func (*Connection) Uninitialize() {
    17  	CoUninitialize()
    18  }
    19  
    20  // Create IUnknown object based first on ProgId and then from String.
    21  func (c *Connection) Create(progId string) (err error) {
    22  	var clsid *GUID
    23  	clsid, err = CLSIDFromProgID(progId)
    24  	if err != nil {
    25  		clsid, err = CLSIDFromString(progId)
    26  		if err != nil {
    27  			return
    28  		}
    29  	}
    30  
    31  	unknown, err := CreateInstance(clsid, IID_IUnknown)
    32  	if err != nil {
    33  		return
    34  	}
    35  	c.Object = unknown
    36  
    37  	return
    38  }
    39  
    40  // Release IUnknown object.
    41  func (c *Connection) Release() {
    42  	c.Object.Release()
    43  }
    44  
    45  // Load COM object from list of programIDs or strings.
    46  func (c *Connection) Load(names ...string) (errors []error) {
    47  	var tempErrors []error = make([]error, len(names))
    48  	var numErrors int = 0
    49  	for _, name := range names {
    50  		err := c.Create(name)
    51  		if err != nil {
    52  			tempErrors = append(tempErrors, err)
    53  			numErrors += 1
    54  			continue
    55  		}
    56  		break
    57  	}
    58  
    59  	copy(errors, tempErrors[0:numErrors])
    60  	return
    61  }
    62  
    63  // Dispatch returns Dispatch object.
    64  func (c *Connection) Dispatch() (object *Dispatch, err error) {
    65  	dispatch, err := c.Object.QueryInterface(IID_IDispatch)
    66  	if err != nil {
    67  		return
    68  	}
    69  	object = &Dispatch{dispatch}
    70  	return
    71  }
    72  
    73  // Dispatch stores IDispatch object.
    74  type Dispatch struct {
    75  	Object *IDispatch // Dispatch object.
    76  }
    77  
    78  // Call method on IDispatch with parameters.
    79  func (d *Dispatch) Call(method string, params ...interface{}) (result *VARIANT, err error) {
    80  	id, err := d.GetId(method)
    81  	if err != nil {
    82  		return
    83  	}
    84  
    85  	result, err = d.Invoke(id, DISPATCH_METHOD, params)
    86  	return
    87  }
    88  
    89  // MustCall method on IDispatch with parameters.
    90  func (d *Dispatch) MustCall(method string, params ...interface{}) (result *VARIANT) {
    91  	id, err := d.GetId(method)
    92  	if err != nil {
    93  		panic(err)
    94  	}
    95  
    96  	result, err = d.Invoke(id, DISPATCH_METHOD, params)
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  
   101  	return
   102  }
   103  
   104  // Get property on IDispatch with parameters.
   105  func (d *Dispatch) Get(name string, params ...interface{}) (result *VARIANT, err error) {
   106  	id, err := d.GetId(name)
   107  	if err != nil {
   108  		return
   109  	}
   110  	result, err = d.Invoke(id, DISPATCH_PROPERTYGET, params)
   111  	return
   112  }
   113  
   114  // MustGet property on IDispatch with parameters.
   115  func (d *Dispatch) MustGet(name string, params ...interface{}) (result *VARIANT) {
   116  	id, err := d.GetId(name)
   117  	if err != nil {
   118  		panic(err)
   119  	}
   120  
   121  	result, err = d.Invoke(id, DISPATCH_PROPERTYGET, params)
   122  	if err != nil {
   123  		panic(err)
   124  	}
   125  	return
   126  }
   127  
   128  // Set property on IDispatch with parameters.
   129  func (d *Dispatch) Set(name string, params ...interface{}) (result *VARIANT, err error) {
   130  	id, err := d.GetId(name)
   131  	if err != nil {
   132  		return
   133  	}
   134  	result, err = d.Invoke(id, DISPATCH_PROPERTYPUT, params)
   135  	return
   136  }
   137  
   138  // MustSet property on IDispatch with parameters.
   139  func (d *Dispatch) MustSet(name string, params ...interface{}) (result *VARIANT) {
   140  	id, err := d.GetId(name)
   141  	if err != nil {
   142  		panic(err)
   143  	}
   144  
   145  	result, err = d.Invoke(id, DISPATCH_PROPERTYPUT, params)
   146  	if err != nil {
   147  		panic(err)
   148  	}
   149  	return
   150  }
   151  
   152  // GetId retrieves ID of name on IDispatch.
   153  func (d *Dispatch) GetId(name string) (id int32, err error) {
   154  	var dispid []int32
   155  	dispid, err = d.Object.GetIDsOfName([]string{name})
   156  	if err != nil {
   157  		return
   158  	}
   159  	id = dispid[0]
   160  	return
   161  }
   162  
   163  // GetIds retrieves all IDs of names on IDispatch.
   164  func (d *Dispatch) GetIds(names ...string) (dispid []int32, err error) {
   165  	dispid, err = d.Object.GetIDsOfName(names)
   166  	return
   167  }
   168  
   169  // Invoke IDispatch on DisplayID of dispatch type with parameters.
   170  //
   171  // There have been problems where if send cascading params..., it would error
   172  // out because the parameters would be empty.
   173  func (d *Dispatch) Invoke(id int32, dispatch int16, params []interface{}) (result *VARIANT, err error) {
   174  	if len(params) < 1 {
   175  		result, err = d.Object.Invoke(id, dispatch)
   176  	} else {
   177  		result, err = d.Object.Invoke(id, dispatch, params...)
   178  	}
   179  	return
   180  }
   181  
   182  // Release IDispatch object.
   183  func (d *Dispatch) Release() {
   184  	d.Object.Release()
   185  }
   186  
   187  // Connect initializes COM and attempts to load IUnknown based on given names.
   188  func Connect(names ...string) (connection *Connection) {
   189  	connection.Initialize()
   190  	connection.Load(names...)
   191  	return
   192  }