Oracle COM Automation Developer's Guide Release 8.1.6 for Windows NT Part Number A73027-01 |
|
This chapter describes the core functionality of Oracle COM Automation feature. Specific topics discussed are:
Oracle COM Automation feature provides a mechanism to manipulate COM objects. It acts as a generic wrapper interface of the IDispatch interface (OLE Automation).
When an Oracle COM Automation feature API is invoked from PL/SQL, the feature converts the parameters to the appropriate OLE Automation data types and then invokes the corresponding IDispatch API with the converted parameters. The feature externalizes the following APIs:
This section describes each of the APIs and how to use them in PL/SQL blocks. A typical PL/SQL block performs the following steps to create and manipulate a COM object using Oracle COM Automation feature:
Oracle COM Automation feature enables you to use components that provide additional functionality that PL/SQL does not support. You can choose to build your own custom components or use the thousands of pre-built components that are available from third-party ISVs.
Before you can begin building a solution using Oracle COM Automation feature, you must know two things about the COM objects that you intend to use.
Excel.Worksheet.1
Use the progID to tell the CreateObject API which COM object to instantiate.
The following is an example of an OLE Automation method prototype in Interface Definition Language (IDL) grammar:
[id(0x6003000)] long Post([in, out] long* lngAccountNo, [in, out] long* lngAmount, [in, out] BSTR* strResult);
Microsoft provides a tool called the OLE/COM Object Viewer with Microsoft Visual C++ for browsing the properties and methods of COM objects on a local system. This tool enables you to quickly and easily determine the properties and methods that each COM object exposes.
Because Oracle uses PL/SQL datatypes and OLE Automation uses Microsoft Visual Basic datatypes, Oracle COM Automation feature must convert the data that it receives from PL/SQL and pass it to the OLE Automation object, and vice versa.
Table 4-1, "PL/SQL to Visual Basic Datatypes" shows the conversion from PL/SQL datatypes to Microsoft Visual Basic datatypes:
Table 4-1 PL/SQL to Visual Basic Datatypes
PL/SQL Datatype | Microsoft Visual Basic Datatype |
---|---|
Varchar2 |
String |
Boolean |
Boolean |
Binary_Integer |
Byte, Integer, or Long |
Double Precision |
Double, Single, or Currency |
Date |
Date |
Table 4-2, "Visual Basic to PL/SQL Datatypes" shows the conversion from Microsoft Visual Basic datatypes to PL/SQL datatypes:
Table 4-2 Visual Basic to PL/SQL Datatypes
Microsoft Visual Basic Datatype | PL/SQL Datatype |
---|---|
Boolean |
Boolean |
Long, Integer, Byte, Object |
Binary_Integer |
String |
Varchar2 |
Double, Single, or Currency |
Double Precision |
Date |
Date |
These APIs return an integer return code. The return code is 0 when successful or a non-zero HRESULT when an error occurs. An HRESULT is an OLE error code of the hexadecimal form 0x800nnnnn, but when it is returned as a binary_integer value, it has the form -214nnnnnnn. For example, passing an invalid object name to CreateObject causes it to return a binary_integer HRESULT of -2147221005, which is 0x800401f3 in hexadecimal.
See "GetLastError" and "OLE Automation Errors" for additional information on how to interpret the return codes from Oracle COM Automation feature. For complete information on HRESULTs, refer to the Microsoft documentation on HRESULTs.
The following section describes the PL/SQL APIs for manipulating COM objects using the OLE Automation interface. Each of the following PL/SQL stored procedures reside in the package ORDCOM.
Instantiates a COM object in an OLE Automation server.
FUNCTION CreateObject(
progidVARCHAR2,
reservedBINARY_INTEGER,
servernameVARCHAR2,
objecttokenOUT BINARY_INTEGER) RETURN BINARY_INTEGER;
where:
The created OLE Automation object is freed with a corresponding call to DestroyObject. This nullifies the internal representation of the object in the Oracle COM Automation Feature and releases all the interfaces associated with the object.
This procedure returns a 0 when successful or a non-zero HRESULT when an error occurs.
hresult binary_integer; applicationToken binary_integer:=-1; hresult :=ORDCOM.CreateObject('Excel.Application', 0, `', applicationToken); IF hresult = -1 THEN dbms_output.put_line(hresult); END IF;
Destroys a created OLE Automation object.
FUNCTION DestroyObject(
objecttokenBINARY_INTEGER) RETURN BINARY_INTEGER;
where:
objecttoken |
is the object token of an OLE Automation object previously created by CreateObject. |
Calling DestroyObject nullifies the internal representation of the object in the Oracle COM Automation Feature and releases all the interfaces associated with the object.
This procedure returns a 0 when successful or a non-zero HRESULT when an error occurs.
hresult binary_integer; applicationToken binary_integer:=-1; /* At some point before this, we called CreateObject and got a valid applicationToken. */ hresult:=ORDCOM.DestroyObject(applicationToken);
Obtains the OLE Automation error information about the last error that occurred.
FUNCTION GetLastError(
sourceOUT VARCHAR2,
descriptionOUT VARCHAR2,
helpfileOUT VARCHAR2,
helpidOUT BINARY_INTEGER) RETURN BINARY_INTEGER;
where:
Each call to an Oracle COM Automation feature API (except GetLastError) resets the error information, so that GetLastError obtains error information only for the most recent Oracle COM Automation feature API call. Because GetLastError does not reset the last error information, it can be called multiple times to get the same error information.
This procedure returns a 0 when successful or a non-zero HRESULT when an error occurs.
See "OLE Automation Errors" for a description of the types of errors that can be returned by this function.
applicationToken binary_integer:=-1; hresult binary_integer; error_src varchar2(255); error_description varchar2(255); error_helpfile varchar2(255); error_helpID binary_integer; hresult:=ORDCOM.CreateObject('Excel.Application', 0, `', applicationToken); IF hresult=-1 THEN ORDCOM.GetLastError(error_src, error_description, error_helpfile, error_ helpID); dbms_output.put_line(error_src); dbms_output.put_line(error_description); dbms_output.put_line(error_helpfile); return hresult; END IF;
Gets a property value of an OLE Automation object.
FUNCTION GetProperty(
objecttoken
BINARY_INTEGER,
propertynameVARCHAR2,
argcountBINARY_INTEGER,
propertyvalue
OUT any PL/SQL datatype) RETURN BINARY_INTEGER;
where:
If the property returns an OLE object, you must specify a local variable of datatype binary_integer or the propertyvalue parameter. An object token is stored in the local variable, and this object token can be used with other OLE Automation stored procedures.
When the property returns an array, if propertyvalue is specified, it is set to NULL.
This procedure returns a 0 when successful or a non-zero HRESULT when an error occurs.
ChartObject binary_integer:=-1; ChartToken binary_integer := -1 hresult binary_integer; /* Previously, ChartObject, CharToken were initialized calling CreateObject */ hresult:=ORDCOM.getProperty(ChartObject, 'Chart', 0, ChartToken); IF hresult=-1 THEN /* Do error checking here */ return hresult; END IF;
Sets a property of an OLE Automation object to a new value.
FUNCTION SetProperty(objecttoken BINARY_INTEGER, propertyname VARCHAR2, newvalue ANY PL/SQL DATATYPE, datatype VARCHAR2) RETURN BINARY_INTEGER;
where:
This procedure returns a 0 when successful or a non-zero HRESULT when an error occurs.
RangeToken binary_integer:=-1; hresult binary_integer; /* Previously, RangeToken has been initialized to a valid object token with a property by the name of value. */ hresult:=ORDCOM.SetProperty(RangeToken, 'Value', `EmpNo', `BSTR'); IF hresult=-1 THEN /* Do error checking here */ return hresult; END IF;
Initializes the parameter set to pass to an Invoke call.
PROCEDURE InitArg();
Each SetArg or SetPtrArg procedure sets the nth parameter value. The InitArg call initializes the parameter set. After InitArg has been called, a SetArg or SetPtrArg call sets the first parameter to the specified value. A second SetArg or SetPtrArg call sets the second parameter in the parameter list. Subsequent calls set the nth parameters in the parameter list, where n is the number of times SetArg or SetPtrArg has been called after an InitArg call. Another call to InitArg resets the argument list and a call to SetArg or SetPtrArg sets the first parameter again.
See "Invoke" for sample code.
Used to construct the parameter list for the next Invoke call. SetArg sets a parameter's value to be passed by value.
PROCEDURE SetArg(paramvalue ANY PL/SQL DATATYPE, datatype VARCHAR2);
where:
Each SetArg or SetPtrArg procedure sets the nth parameter value. The InitArg call initializes the parameter set. After InitArg has been called, a SetArg or SetPtrArg call sets the first parameter to the specified value. A second SetArg or SetPtrArg call sets the second parameter in the parameter list. Subsequent calls set the nth parameters in the parameter list, where n is the number of times SetArg or SetPtrArg has been called after an InitArg call. Another call to InitArg resets the argument list and a call to SetArg or SetPtrArg sets the first parameter again.
See "Invoke" for sample code.
Constructs the parameter list for the next Invoke call. SetPtrArg sets a parameter's value to be passed by reference.
PROCEDURE SetPtrArg(paramvalue ANY PL/SQL DATATYPE, datatype VARCHAR2);
where:
Each SetArg or SetPtrArg procedure sets the nth parameter value. The InitArg call initializes the parameter set. After InitArg has been called, a SetArg or SetPtrArg call sets the first parameter to the specified value. A second SetArg or SetPtrArg call sets the second parameter in the parameter list. Subsequent calls set the nth parameters in the parameter list, where n is the number of times SetArg or SetPtrArg has been called after an InitArg call. Another call to InitArg resets the argument list and a call to SetArg or SetPtrArg sets the first parameter again.
See "Invoke" for sample code.
Calls a method of an OLE Automation object. This function uses the parameter list, previously created by the calls to InitArg, SetArg, and SetPtrArg, as input for the OLE Automation method.
FUNCTION Invoke(objecttoken
BINARY_INTEGER,methodname
VARCHAR2, argcount BINARY_INTEGER,returnvalue
OUT ANY PL/SQL DATATYPE) RETURN BINARY_INTEGER;
where:
If the method's return value is an OLE object, then the developer must specify a local variable of datatype binary_integer for the returnvalue parameter. An object token is stored in the local variable, and this object token can be used with other Oracle COM Automation feature APIs.
This procedure returns a 0 when successful or a non-zero HRESULT when an error occurs.
xpos binary_integer; ypos binary_integer; width binary_integer; height binary_integer; WorkSheetToken binary_integer:=-1; ChartObjectToken binary_integer:=-1; ChartObject binary_integer:=-1; hresult binary_integer; /*WorkSheetToken has been initialized with a valid OLE Automation object */ /* Executes a method that takes 0 arguments */ ORDCOM.InitArg(); i:=ORDCOM.Invoke(WorkSheetToken, 'ChartObjects', 0, ChartObjectToken); /* Executes a method that takes 4 arguments */ ORDCOM.InitArg(); ORDCOM.SetArg(xpos,'I2'); ORDCOM.SetArg(ypos,'I2'); ORDCOM.SetArg(width,'I2'); ORDCOM.SetArg(height,'I2'); i:=ORDCOM.Invoke(ChartObjectToken, 'Add', 4, ChartObject);
The following is a list of Oracle COM Automation errors and their common causes.
COM-0001: Not a boolean type
Action: Ensure that the variable is of the appropriate datatype.
COM-0002: Invalid Token or no interface for token
Action: Ensure that the interface exists.
COM-0003: Maximum Objects reached
Action: Make sure that objects are destroyed after they are used by calling DestroyObject.
COM-0004: The registered CLSID for the ProgID is invalid.
Action: Check that the COM component of the specified ProgID is registered.
COM-0005: An error occurred writing the CLSID to the registry.
Action: Make sure your registry can be written to and is not corrupted.
COM-0006: A specified class is not registered in the registration database
Action: Make sure the class is registered.
COM-0007: Failed to initialize OLE Automation object
Action: Make sure the object is registered as a COM Automation object.
COM-0008: No interface is supported
Action: Check that the interface specified is valid.
COM-0009: Failed to get type info count
Action: Check that the object is properly registered.
COM-0010: Does not support type info implementation
Action: Check that the object is properly registered.
COM-0011: Failed to get type information
Action: Check that the object is properly registered.
COM-0012: Failed to get type attributes.
Action: Check that the object is properly registered.
COM-0013: Failed to get function description at index.
Action: Check that the object is properly registered.
COM-0014: Failure to invoke
Action: Check that the method name is valid for the object.
COM-0015: Bad parameter count
Action: Make sure the number of parameters for a method is equal to the count.
COM-0016: One of the arguments in rgvarg is not a valid variant type.
Action: Check that the object is properly registered.
COM-0017: The application needs to raise an exception. The structure passed in pexcepinfo should be filled in.
Action: Make sure structure in pexcepinfo is initialized.
COM-0018: The requested member does not exist, or the call to Invoke tried to set the value of a read-only property.
Action: Make sure the property value can be written to or the member exists.
COM-0019: This implementation of IDispatch does not support named arguments.
Action: Do not use named arguments. Use standard parameter passing.
COM-0020: One of the arguments in rgvarg could not be coerced to the specified type.
Action: Make sure that the coerced arguments are of compatible data types.
COM-0021: One of the parameter dispatch IDs does not correspond to a parameter on the method.
Action: Make sure the arguments are passed in correctly.
COM-0022: One or more of the arguments could not be coerced.
Action: Make sure your arguments are compatible.
COM-0023: The interface ID passed in riid is not IID_NULL.
Action: Make sure the interface ID passed is IID_NULL.
COM-0024: The member being invoked interprets string arguments according to an unrecognized locale ID (LCID).
Action: Make sure your localeID is valid.
COM-0025: Not an optional parameter
Action: Make sure your argument count is correct for the number of parameters passed in.
COM-0026: Name exceeded the maximum character allowed
Action: Enter 1024 characters or less for the name.
COM-0027: This class cannot be created as part of an aggregate.
Action: Do not create this class as part of an aggregate.
The following is a list of OLE Automation errors and their common causes. Both the hexadecimal and binary error codes are listed.
(0x800401f3) (-2147221005) Invalid class string
Cause: The specified ProgID or CLSID is not registered as an OLE object in the registry of the local computer.
(0x8007007e) (-2147024770) The specified module could not be found
Cause: The specified OLE object is registered as an in-process OLE server (.DLL file), but the .DLL file could not be found or loaded.
(0x80020004) (-2147352572) Parameter not found
Cause: A named parameter was specified before a positional parameter.
Action: Ensure that all named parameters are specified after all positional parameters.
(0x80020005) (-2147352571) Type mismatch
Cause: The datatype of a PL/SQL local variable used to store a returned property value or a method return value did not match the Visual Basic data type of the property or method return value.
Action: Ensure that the local variable is of the appropriate datatype. Or, the return value of a property or a method was requested, but it does not return a value.
(0x80020006) (-2147352570) Unknown name
Cause: The specified property or method name was not found for the specified object.
(0x80020008) (-2147352568) Bad variable type
Cause: The datatype of a PL/SQL value passed as a method parameter did not match the Microsoft Visual Basic data type of the method parameter, or a NULL value was passed as a method parameter.
Action: Ensure that any local variables used as method parameters are of the appropriate datatype and are set to a value other than NULL.
(0x80080005) (-2146959355) Server execution failed
Cause: The specified OLE object is registered as a local OLE server (.EXE file), but the .EXE file could not be found or started.
|
![]() Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|