Cicode Programming Reference > Cicode Function Categories > ActiveX Objects Introduction > CreateObject

CreateObject

Creates a new instance of an ActiveX object. If you use this function to create an ActiveX object, it will have no visual component (only the automation component will be created).

If you assign an object created with the CreateObject() function to a local variable, that object will remain in existence until the variable it is assigned to goes out of scope. This means that such an object will only be released when the Cicode function that created it ends.

If you assign an object created with the CreateObject() function to a module or global scope variable, then that object will remain in existence until the variable either has another object assigned or is set to NullObject, provided the CreateObject() call is not made within a loop.

Objects created by calls to CreateObject() within WHILE or FOR loops are only released on termination of the Cicode function in which they are created, regardless of the scope of the variable to which the object is assigned. The use of CreateObject() within a loop may therefore result in the exhaustion of system resources, and is not generally recommended unless performed as shown in the examples below.

 

UNINTENDED EQUIPMENT OPERATION

Do not use the CreateObject() function within a loop except in strict accordance with the following instructions.

Failure to follow these instructions can result in death, serious injury, or equipment damage.

Syntax

CreateObject(sClass)

sClass:

The class of the object. You can use the object's human readable name, its program ID, or its GUID. If the class does not exist, the function will return an error.

For example:

Return Value

The newly created object, if successful, otherwise an error is generated.

Related Functions

DspAnCreateControlObject, CreateControlObject

Example

The following examples show correct techniques for calling CreateObject() within a loop.

/* In the example below, the variable objTest is local. Resources 
associated with calls to ProcessObject() will be released each 
time that function ends. */
FUNCTION Forever()
WHILE 1 DO
ProcessObject();
Sleep(1);
END
END
FUNCTION ProcessObject()
.OBJECT objTest;
objTest=CreateObject("MyObject");
- do something
END
/* In the example below, the variable objTest is global. Resources associated with calls to ProcessObject() will be released when objTest is set to NullObject. */
FUNCTION Forever()
WHILE 1 DO
ProcessObject();
Sleep(1);
END
END
FUNCTION ProcessObject()
objTest=CreateObject("MyObject");
- do something
objTest=NullObject;
END

See Also

ActiveX Functions