Share via


COM::getObject Method

Returns an instance of a COM object that is running.

Syntax

client server public static COM getObject(str className)

Run On

Called

Parameters

  • className
    Type: str
    The ProgID value or class name of the COM object that is used to create the instance of the COM class.

Return Value

Type: COM Class
An instance of the COM class for the class that is specified by the className parameter; nullNothingnullptrunita null reference (Nothing in Visual Basic) if the instance could not be created.

Remarks

If multiple instances of the specified COM object are running, we cannot guarantee which instance will be returned by the getObject method.

Some COM objects do not support the extended features that enable calls to this method.

Examples

The following example shows how to retrieve an instance of a running COM object.

COM objExcel, objWorkBook, objWorkBooks; 
InteropPermission perm; 
  
// Set code access permission to help protect the use of COM.new. 
perm = new InteropPermission(InteropKind::ComInterop); 
perm.assert(); 
  
objExcel = COM::getObject("Excel.Application"); 
  
if (!objExcel) 
{ 
    // Unable to connect to a running instance of Microsoft Excel. 
    // Try starting it. 
    objExcel = new COM("Excel.Application"); 
    if (!objExcel) 
    { 
        Info ("Unable to find or create an instance of Excel"); 
        return;  // Or other error action. 
    }  
} 
// Close code access permission scope. 
CodeAccessPermission::revertAssert(); 
  
objExcel.visible(true); 
objWorkBooks = objExcel.workbooks(); 
if (objWorkBooks) 
{ 
    objWorkBook = objWorkBooks.open("d:\\mydata\\book1.xls"); 
}

See Also

COM Class

COM.new Method

COM::getObjectEx Method