Pro*C/C++ Precompiler Getting Started 
Release 8.0.5 for Windows NT and Windows 95/98 
A64423-01
 
Library
 
Product
 
Contents
 
Index
 

Prev Next

3
Building Pro*C/C++ Applications

This chapter describes how to build Oracle database applications with Pro*C/C++ using the sample programs that are included with this release, and provides an overview of how to build multi-threaded applications.

Specific topics discussed are:

Building and Running Sample Programs

When you install Pro*C/C++, Oracle Installer copies a set of sample programs to the ORACLE_HOME\PRO80\C\SAMPLES directory. Oracle recommends that you build and run these sample programs to verify that Pro*C/C++ has been installed successfully and operates correctly. You can delete the programs after you use them.


Note: 

The Pro*C/C++ Precompiler Programmer's Guide indicates that the sample programs are located in a directory named DEMO. In Pro*C/C++ for Windows NT and Windows 95/98, the sample programs are located in the directory named SAMPLES. 
 


Directory  Sample Program  Sample Pro*C/C++ Project  Sampler Compiler Project  Description 

COLDEMO1 

COLDEMO1.PC 
COLDEMO1.SQL 

COLDEMO1.PRE 

COLDEMO1.DSP 
COLDEMO1.IDE 

Pro*C/C++ application that uses the Object Type Translator (OTT).  

Note: Run COLDEMO1.SQL and OTT before building COLDEMO1. 

CPPDEMO1 

CPPDEMO1.PC 

CPPDEMO1.PRE 

CPPDEMO1.DSP 
CPPDEMO1.IDE 

Pro*C/C++ application in C++. 

CPPDEMO2 

CPPDEMO2.PC 

CPPDEMO2.PRE 

CPPDEMO2.DSP 
CPPDEMO2.IDE 

Pro*C/C++ application in C++. 

CPPDEMO3 

CPPDEMO3.PC 

CPPDEMO3.PRE 

CPPDEMO3.DSP 
CPPDEMO3.IDE 

Pro*C/C++ application in C++. 

CVDEMO 

CV_DEMO.PC 
CV_DEMO.SQL 

CV_DEMO.PRE 

CV_DEMO.DSP 
CV_DEMO.IDE 

Pro*C/C++ application using cursor variable with PL/SQL stored procedure.  

Note: Run CV_DEMO.SQL before building CV_DEMO. 

LOBDEMO1 

LOBDEMO1.PC 
LOBDEMO1.SQL 

LOBDEMO1.PRE 

LOBDEMO1.DSP 
LOBDEMO1.IDE 

Pro*C/C++ application that uses large objects (LOBs).  

Note: Run LOBDEMO1.SQL before building LOBDEMO1. 

MLTTHRD1 

MLTTHRD1.PC 
MLTTHRD1.SQL 

MLTTHRD1.PRE 

MLTTHRD1.DSP 
MLTTHRD1.IDE 

Multi-threaded Pro*C/C++ application.  

Note: Run MLTTHRD1.SQL before building MLTTHRD1. 

NAVDEMO1 

NAVDEMO1.PC 
NAVDEMO1.SQL 

NAVDEMO1.PRE 

NAVDEMO1.DSP 
NAVDEMO1.IDE 

Pro*C/C++ application that demonstrates navigational access to objects in the object cache.  

Note: Run NAVDEMO1.SQL and OTT before building NAVDEMO1. 

OBJDEMO1 

OBJDEMO1.PC 
OBJDEMO1.SQL 

OBJDEMO1.PRE 

OBJDEMO1.DSP 
OBJDEMO1.IDE 

Pro*C/C++ application that uses OTT.  

Note: Run OBJDEMO1.SQL and OTT before building OBJDEMO1. 

ORACA 

ORACA.PC 
ORACATST.SQL 

ORACA.PRE 

ORACA.DSP 
ORACA.IDE 

Pro*C/C++ application using the ORACA to determine various performance parameters at runtime.  

Note: Run ORACATST.SQL before building ORACA. 

PLSSAM 

PLSSAM.PC 

PLSSAM.PRE 

PLSSAM.DSP 
PLSSAM.IDE 

Pro*C/C++ application with embedded PL/SQL blocks. 

SAMPLE 

SAMPLE.PC 

SAMPLE.PRE 

SAMPLE.DSP 
SAMPLE.IDE 

Pro*C/C++ application using EXEC SQL statements. 

SAMPLE1 

SAMPLE1.PC 

SAMPLE1.PRE 

SAMPLE1.DSP 
SAMPLE1.IDE 

Pro*C/C++ application using EXEC SQL statements. 

SAMPLE2 

SAMPLE2.PC 

SAMPLE2.PRE 

SAMPLE2.DSP 
SAMPLE2.IDE 

Pro*C/C++ application using EXEC SQL statements. 

SAMPLE3 

SAMPLE3.PC 

SAMPLE3.PRE 

SAMPLE3.DSP 
SAMPLE3.IDE 

Pro*C/C++ application using EXEC SQL statements. 

SAMPLE4 

SAMPLE4.PC 

SAMPLE4.PRE 

SAMPLE4.DSP 
SAMPLE4.IDE 

Pro*C/C++ application that demonstrates type equivalencing. 

SAMPLE5 

SAMPLE5.PC 
EXAMPBLD.SQL 
EXAMPLOD.SQL 

SAMPLE5.PRE 

SAMPLE5.DSP 
SAMPLE5.IDE 

Pro*C/C++ application with embedded PL/SQL blocks.  

Note: Run EXAMPBLD.SQL, then run EXAMPLOD.SQL, before building SAMPLE5. 

SAMPLE6 

SAMPLE6.PC 

SAMPLE6.PRE 

SAMPLE6.DSP 
SAMPLE6.IDE 

Pro*C/C++ application using EXEC SQL statements. 

SAMPLE7 

SAMPLE7.PC 

SAMPLE7.PRE 

SAMPLE7.DSP 
SAMPLE7.IDE 

Pro*C/C++ application using EXEC SQL statements. 

SAMPLE8 

SAMPLE8.PC 

SAMPLE8.PRE 

SAMPLE8.DSP 
SAMPLE8.IDE 

Pro*C/C++ application using EXEC SQL statements. 

SAMPLE9 

SAMPLE9.PC 
CALLDEMO.SQL 

SAMPLE9.PRE 

SAMPLE9.DSP 
SAMPLE9.IDE 

Pro*C/C++ application calling a PL/SQL stored procedure.  

Note: Run CALLDEMO.SQL before building SAMPLE9. 

SAMPLE10 

SAMPLE10.PC 

SAMPLE10.PRE 

SAMPLE10.DSP 
SAMPLE10.IDE 

Pro*C/C++ application showing an implementation of Dynamic SQL Method 4. 

SAMPLE11 

SAMPLE11.PC 
SAMPLE11.SQL 

SAMPLE11.PRE 

SAMPLE11.DSP 
SAMPLE11.IDE 

Pro*C/C++ application that uses a PL/SQL stored procedure.  

Note: Run SAMPLE11.SQL before building SAMPLE11. 

SAMPLE12 

SAMPLE12.PC 

SAMPLE12.PRE 

SAMPLE12.DSP 
SAMPLE12.IDE 

Pro*C/C++ application that demonstrates Dynamic SQL Method 4. 

SQLVCP 

SQLVCP.PC 

SQLVCP.PRE 

SQLVCP.DSP 
SQLVCP.IDE 

Pro*C/C++ application that demonstrates the use of sqlvcp() and sqlgls() calls. 

WINSAM 

WINSAM.PC 

WINSAM.PRE 

RESOURCE.H 
WINSAM.DSP 
WINSAM.H 
WINSAM.ICO 
WINSAM.IDE 
WINSAM.RC 

GUI Pro*C/C++ application. 

 

.DSP and .IDE Files

.DSP and .IDE files help you build application programs.

Sample .DSP files in this release were created with Microsoft Visual C++ version 5.0. Sample .IDE files in this release were created with Borland C++ version 5.0.

Sample Pro*C/C++ Programs

When built, the sample programs that Oracle provides in Pro*C/C++ produce .EXE executables. For some sample programs, you must run the SQL script(s) in the sample directory before you precompile and run the sample program. The SQL scripts set up the correct tables and data so that the sample programs run correctly.

Also, some samples require the Object Type Translator (OTT) to be executed before precompilation. Use OTT to create header files with C-struct representations of Abstract Data Types that have been created and stored in an Oracle8 database.


Additional Information: 

For more information on OTT, see Chapter 16 of the Pro*C/C++ Precompiler Programmer's Guide
 


The sample programs are described below.

COLDEMO1.PC

Fetches census information for California counties. This program demonstrates a number of ways to navigate through collection-typed database columns.

CPPDEMO1.PC

Prompts the user for an employee number, then queries the EMP table for the employee's name, salary, and commission. It uses indicator variables (in an indicator struct) to determine whether the commission is NULL.

CPPDEMO2.PC

Retrieves the names of all employees in a given department from the EMP table (Dynamic SQL Method 3).

CPPDEMO3.PC

An example of C++ inheritance, this program finds all salespeople and prints their names and total earnings (including commissions).

CV_DEMO.PC

Fetches data from the EMP table, using a cursor variable. The cursor is opened in the stored PL/SQL procedure open_cur, in the EMP_DEMO_PKG package.

LOBDEMO1.PC

Fetches and adds crime records to the database based on the person's Social Security number. This program demonstrates the mechanisms for accessing and storing large objects (LOBs) to tables and manipulating LOBs through the stored procedures available via the DBMS_LOB package.

MLTTHRD1.PC

Shows how to use threading in conjunction with precompilers. The program creates as many sessions as there are threads.

NAVDEMO1.PC

Demonstrates navigational access to objects in the object cache.

OBJDEMO1.PC

Demonstrates the use of objects. This program manipulates the object types "person" and "address".

ORACA.PC

Demonstrates how to use the ORACA to determine various performance parameters at runtime.

PLSSAM.PC

Demonstrates the use of embedded PL/SQL blocks. This program prompts you for an employee name that already resides in a database. It then executes a PL/SQL block, which returns the results of four SELECT statements.

SAMPLE.PC

Adds new employee records to the personnel database and checks database integrity. The employee numbers in the database are automatically selected using the current maximum employee number +10.

SAMPLE1.PC

Logs on to an Oracle database, prompts the user for an employee number, queries the database for the employee's name, salary, and commission, and displays the result. The program continues until you enter 0 as the employee number.

SAMPLE2.PC

Logs on to an Oracle database, declares and opens a cursor, fetches the names, salaries, and commissions of all salespeople, displays the results, and closes the cursor.

SAMPLE3.PC

Logs on to an Oracle database, declares and opens a cursor, fetches in batches using arrays, and prints the results using the print_rows() function.

SAMPLE4.PC

Demonstrates the use of type equivalencing using the LONG RAW external datatype.

SAMPLE5.PC

Prompts the user for an account number and a debit amount. The program verifies that the account number is valid and that there are sufficient funds to cover the withdrawal before it debits the account.

SAMPLE6.PC

Creates a table, inserts a row, commits the insert, and drops the table (Dynamic SQL Method 1).

SAMPLE7.PC

Inserts two rows into the EMP table and deletes them (Dynamic SQL Method 2).

SAMPLE8.PC

Retrieves the names of all employees in a given department from the EMP table (Dynamic SQL Method 3).

SAMPLE9.PC

Connects to an Oracle database using the SCOTT/TIGER account. The program declares several host arrays and calls a PL/SQL stored procedure (GET_EMPLOYEES in the CALLDEMO package). The PL/SQL procedure returns up to ASIZE values. The program keeps calling GET_EMPLOYEES, getting ASIZE arrays each time, and printing the values, until all rows have been retrieved.

SAMPLE10.PC

Connects to an Oracle database using your user name and password and prompts for a SQL statement. You can enter any legal SQL statement, but you must use regular SQL syntax, not embedded SQL. Your statement is processed. If the statement is a query, the rows fetched are displayed (Dynamic SQL Method 4).

SAMPLE11.PC

Fetches from the EMP table, using a cursor variable. The cursor is opened in the stored PL/SQL procedure open_cur, in the EMP_DEMO_PKG package.

SAMPLE12.PC

This is a very simple program that demonstrates how to do array fetches using dynamic SQL Method 4.

SQLVCP.PC

Demonstrates how you can use the sqlvcp() function to determine the actual size of a VARCHAR struct. The size is then used as an offset to increment a pointer that steps through an array of VARCHARs.

This program also demonstrates how to use the sqlgls() function to retrieve the text of the last SQL statement that was executed.

WINSAM.PC

This GUI version of the SAMPLE.PC program adds new employee records to the personnel database and checks database integrity. You can enter as many employee names as you want and perform the SQL commands by selecting the appropriate buttons in the Employee Record box.

Creating a Sample Program

To precompile, compile, and link a sample program:

  1. Open a sample project file. For example, open WINSAM.DSP for Microsoft Visual C++ or WINSAM.IDE for Borland C++.
  1. Check the paths in the project file to ensure that they correspond to the configuration of your system. If they do not, change the paths accordingly. Your system may produce error messages if the paths to all components are not correct.

  2.  

    Note: 

    All of the sample projects were created with C as the default drive. 
     



    Attention: 

    If you are using Microsoft Visual C++ version 5.0, note that SQL*Plus, OTT, and Pro*C/C++ have been fully integrated into the Microsoft Visual C++ sample project files. (See Appendix A.) You do not have to run SQL*Plus, OTT, and Pro*C/C++ separately before compilation. Simply open the project file and choose Rebuild All from the Build menu to create the sample program using Microsoft Visual C++. The remaining steps in this procedure can then be omitted. 
     



    Attention: 

    If you are using Borland C++ version 5.0, you can precompile all of the samples using Pro*C/C++ as a tool from the Tool menu. (See Appendix B.) Continue with the remaining steps in this procedure. 
     


    If you use Borland C++, some samples require SQL scripts and OTT to be executed individually before precompilation.

  3. For samples that require SQL scripts to be executed, enter the following at the MS-DOS command prompt:
  4. sqlplus @script.sql

where script is the name of a SQL script for a sample. SQL scripts for samples can be found in the corresponding sample directory. See the table earlier in this chapter for the name of the SQL script that needs to be executed for a particular sample, if any.

  1. For the samples that require OTT to be executed, enter the following at the command line to produce .H and intype files for Pro*C/C++:
  2. ott intype=demo.typ outtype=demo_o.typ hfile=demo.h code=c userid=scott/tiger

where demo is objdemo1, for example. When precompiling samples that require OTT, be sure to set intype=demo_o.typ, parse=full, and include directory to the location of demo.h.


Additional Information: 

See Chapter 16 of the Pro*C/C++ Precompiler Programmer's Guide for more information about OTT. 
 


  1. Choose Pro*C/C++ from the Tool menu.
  2. The Pro*C/C++ precompile environment appears with the project file (for example, WINSAM.PRE) already loaded.

  3. Click Precompile on the Pro*C/C++ toolbar to precompile the sample file.
  4. Once the precompile process is successfully completed, go back to your development environment and build the sample application.

Running a Sample Program

To run a sample program (in this case, WINSAM) after building it:

  1. From your development environment, run WINSAM.EXE.
  1. Choose Connect from the Oracle menu.
  2. Enter SCOTT in the User ID box and TIGER in the Password box (or the connect string if connecting remotely).
  3. Click OK.
  4. A dialog box appears notifying you whether the connection is successful.

  5. To enter employee data, choose Employees from the Oracle menu.
  6. An Employee Record dialog box appears. You can use SQL buttons such as SELECT, FETCH, or INSERT to manipulate the sample tables.

  7. Choose Exit from the Employee Record dialog box. Then, choose Disconnect from the Oracle menu.
  8. A dialog box notifies you whether the disconnect is successful.

  9. To quit the program, choose Exit.

  10.  

    Note: 

    All sample programs are written to be run on the local database. To connect to a remote database, use the LOCAL variable in the registry. 
     


Building the Demonstration Tables

To run the Pro*C/C++ sample programs, you must have a database account with the user name SCOTT and the password TIGER and a database with the demonstration tables EMP and DEPT. This account is included in the seed database (named ORACLE) that is included with your Oracle server. If no such account exists on your database, install one before running the sample programs.


Additional Information: 

See your Oracle database or server documentation. If your database does not contain these tables, use the DEMOBLD.SQL script to create them. To run DEMOBLD, first use SQL*Plus or Server Manager to connect to the Oracle database. 
 


Dynamic Link Libraries (DLLs)

Pro*C/C++ application program interface (API) calls are implemented in DLL files provided with your Pro*C/C++ software. To use the DLLs, you must link your application with the import libraries (.LIB files) that correspond to the Pro*C/C++ DLLs. Also, you must ensure that the DLL files are installed on the workstation that is running your Pro*C/C++ application.

Applications linked with DLLs provide the following benefits over static libraries:

Microsoft provides you with three libraries: LIBC.LIB, LIBCMT.LIB, and MSVCRT.LIB. The Oracle DLLs use the MSVCRT.LIB runtime library. You must link with MSVCRT.LIB rather than the other two Microsoft libraries.

Building Multi-Threaded Applications

Build multi-threaded applications if you are planning to perform concurrent database operations.

Windows NT and Windows 95/98 schedule and allocate threads belonging to processes. A thread is a path of a program's execution. It consists of a kernel stack, the state of the CPU registers, a thread environment block, and a users stack. Each thread shares the resources of a process. Multi-threaded applications use the resources of a process to coordinate the activities of individual threads.

Additional Information: 

See the Pro*C/C++ Precompiler Programmer's Guide for additional information on how to write multi-threaded applications with Pro*C/C++. 
 

When building a multi-threaded application, make sure that your C/C++ code is reentrant. This means that access to static or global data must be restricted to one thread at a time. If you mix multi-threaded and non-reentrant functions, one thread can modify information that is required by another thread.

The Pro*C/C++ precompiler automatically creates variables on the local stack of the thread. This ensures that each thread using the Pro*C/C++ function has access to a unique set of variables and is reentrant.



 
Prev
 
Next
 
Oracle 
Copyright © 1998 Oracle Corporation. 
All Rights Reserved. 
 
Library
 
Product
 
Contents
 
Index