-- - InstSamp.SQL 09/18/1996
GO
---------------------------------------------------------------
-- Create Database
---------------------------------------------------------------
-- Set switches for creating database
set nocount on
set dateformat mdy
-- Initially point to the master database
USE master
GO
-- Drop the VCEESamples database if it initially exists
declare @dttm varchar(55)
select @dttm=convert(varchar,getdate(),113)
raiserror('Beginning InstSamp.SQL at %s ....',1,1,@dttm) with nowait
if exists (select * from sysdatabases where name='VCEESamples')
DROP database VCEESamples
GO
-- Check for completion. (Make sure db is completely dropped b4 creating another.)
CHECKPOINT
GO
-- Create the VCEESamples database using 3 MB of space in master.
CREATE DATABASE VCEESamples
on master = 3
GO
-- Check for completion. (Make sure db is completely created b4 proceeding.)
CHECKPOINT
GO
-- Switch to the VCEESamples database.
USE VCEESamples
GO
-- Verify the existence of VCEESamples.
if db_name() = 'VCEESamples'
raiserror ('''VCEESamples'' database created, and context now in use.',
1, 1)
else
raiserror ('Error in InstSamp.SQL, ''USE VCEESamples'
' failed! Killing the SPID now.', 22, 127) with log
GO
-- Clear the log.
execute sp_dboption 'VCEESamples', 'trunc. log on chkpt.' , 'true'
GO
---------------------------------------------------------------
-- Create Tables
---------------------------------------------------------------
-- Echo to console that we are creating tables.
raiserror('Now at the create table section ....',1,1)
GO
---------------------------------------------------------------
-- Tables for Orders Samples
-- OrdersAccount
CREATE TABLE OrdersAccount
(
AccountID int NOT NULL CONSTRAINT UPKCL_ccountind PRIMARY KEY CLUSTERED,
Balance money NOT NULL,
PIN varchar(4) NOT NULL
)
GO
-- OrdersInventory
CREATE TABLE OrdersInventory
(
PartID int NOT NULL CONSTRAINT UPKCL_partidind PRIMARY KEY CLUSTERED,
InStock int NOT NULL,
Description varchar(128) NULL,
Price money NOT NULL
)
GO
-- OrdersShipping
CREATE TABLE OrdersShipping
(
OrderID int NOT NULL CONSTRAINT UPKCL_orderidind PRIMARY KEY CLUSTERED,
AccountID int NOT NULL,
PartID int NOT NULL,
Quantity int NOT NULL,
State int NOT NULL
)
GO
---------------------------------------------------------------
-- Tables for Stocks Samples
-- StocksAccount
CREATE TABLE StocksAccount
(
AccountID int NOT NULL CONSTRAINT UPKCL_accountidind PRIMARY KEY CLUSTERED,
Balance money NOT NULL,
PIN varchar(4) NOT NULL
)
GO
-- StocksShares and additional index
CREATE TABLE StocksShares
(
AccountID int NOT NULL,
Stock varchar(10) NOT NULL,
Shares int NOT NULL
)
GO
CREATE INDEX UPKCL_stockind ON StocksShares(Stock)
GO
-- StocksTrades
CREATE TABLE StocksTrades
(
AccountID int NOT NULL,
Stock varchar(10) NOT NULL,
Action int NOT NULL,
Shares int NOT NULL,
Price float NOT NULL
)
GO
---------------------------------------------------------------
-- Populate tables with data
---------------------------------------------------------------
GO
---------------------------------------------------------------
-- Tables for Orders Samples
-- OrdersAccount
raiserror('Now at the inserts to OrdersAccount ....',1,1)
insert OrdersAccount VALUES(1, 1000.00, 'xxxx')
insert OrdersAccount VALUES(2, 1000.00, 'xxxx')
insert OrdersAccount VALUES(3, 1000.00, 'xxxx')
insert OrdersAccount VALUES(4, 1000.00, 'xxxx')
insert OrdersAccount VALUES(5, 1000.00, 'xxxx')
insert OrdersAccount VALUES(6, 1000.00, 'xxxx')
GO
-- OrdersInventory
raiserror('Now at the inserts to OrdersInventory ....',1,1)
GO
insert OrdersInventory VALUES(1, 1000, 'Widgets', 5.25)
insert OrdersInventory VALUES(2, 1500, 'Nuts', 0.05)
insert OrdersInventory VALUES(3, 200, 'Bolts', 0.25)
insert OrdersInventory VALUES(4, 2000, 'Hammers', 15.75)
GO
---------------------------------------------------------------
-- Tables for Stocks Samples
-- StocksAccount
raiserror('Now at the inserts to StocksAccount ....',1,1)
GO
insert StocksAccount VALUES(1, 100000.00, 'xxxx')
insert StocksAccount VALUES(2, 25.00, 'xxxx')
insert StocksAccount VALUES(3, 5000.00, 'xxxx')
insert StocksAccount VALUES(4, 5.00, 'xxxx')
insert StocksAccount VALUES(5, -20.00, 'xxxx')
insert StocksAccount VALUES(6, 0.00, 'xxxx')
insert StocksAccount VALUES(7, 3000.00, 'xxxx')
insert StocksAccount VALUES(8, 400000.00, 'xxxx')
insert StocksAccount VALUES(9, 6795.23, 'xxxx')
insert StocksAccount VALUES(10, 600.00, 'xxxx')
insert StocksAccount VALUES(11, 50.00, 'xxxx')
insert StocksAccount VALUES(12, 1000000.00, 'xxxx')
GO
-- StocksTrades
raiserror('Now at the inserts to StocksTrades ....',1,1)
GO
insert StocksTrades VALUES(1, 'FOO', 1, 100, 25.25)
insert StocksTrades VALUES(2, 'BAR', 0, 200, 20.00)
insert StocksTrades VALUES(3, 'FOO', 1, 1000, 25.375)
insert StocksTrades VALUES(4, 'BAR', 0, 300, 20.25)
GO
-- StocksShares
raiserror('Now at the inserts to StocksShares ....',1,1)
GO
insert StocksShares VALUES(1, 'FOO', 1000)
insert StocksShares VALUES(2, 'BAR', 200)
insert StocksShares VALUES(1, 'BAR', 300)
insert StocksShares VALUES(2, 'BAZ', 500)
insert StocksShares VALUES(4, 'FOO', 2000)
insert StocksShares VALUES(5, 'BAR', 500)
GO
-- Create sp to validate account used by MTO's.
raiserror('Now creating sp_validaccount ....',1,1)
GO
CREATE PROCEDURE sp_validaccount @acct int
AS
declare @cnt int
select @cnt=count(*) from StocksAccount where StocksAccount.AccountID = @acct
if @cnt=0
begin
raiserror ('Invalid Account', 16, -1)
end
RETURN
---------------------------------------------------------------
-- Grant permissions to db objects
---------------------------------------------------------------
GO
-- SP Valid Account
raiserror('Now granting permissions for stored procedures ....',1,1)
GO
GRANT execute ON sp_validaccount TO public
GRANT CREATE PROCEDURE TO public
GO
GRANT ALL ON OrdersAccount TO public
GRANT ALL ON OrdersInventory TO public
GRANT ALL ON OrdersShipping TO public
GRANT ALL ON StocksAccount TO public
GRANT ALL ON StocksShares TO public
GRANT ALL ON StocksTrades TO public
GRANT CREATE TABLE TO public
GRANT CREATE VIEW TO public
GRANT CREATE RULE TO public
GRANT CREATE DEFAULT TO public
GRANT CREATE PROCEDURE TO public
GO
-- Refresh the database to update permissions.
UPDATE STATISTICS OrdersAccount
UPDATE STATISTICS OrdersShipping
UPDATE STATISTICS OrdersInventory
UPDATE STATISTICS StocksAccount
UPDATE STATISTICS StocksShares
UPDATE STATISTICS StocksTrades
GO
-- Ensure permissions are granted before proceeding.
CHECKPOINT
GO
---------------------------------------------------------------
-- End the script.
---------------------------------------------------------------
-- Return to the master database.
USE master
GO
-- Ensure we are in the master database before proceeding.
CHECKPOINT
GO
-- Send date and time that script completed.
declare @dttm varchar(55)
select @dttm=convert(varchar,getdate(),113)
raiserror('Ending InstSamp.SQL at %s ....',1,1,@dttm) with nowait
GO
-- -
Figure 3 Wizard-generated SQL Database Project
| File | Description |
| SQL_database.dsp | Project settings |
| SQL_database.dsw | Workspace settings |
| SQL_database.ncb | Program database |
| SQL_database.opt | Workspace options |
Figure 4 Wizard-generated C++ Component Project
| File | Description |
| CPP_component.clw | MFC Class Wizard settings. |
| CPP_component.cpp | DLL initialization and exports (DllMain and friends). Also contains instructions for merging the proxy stub into your project. |
| CPP_component.def | Linker module definitions. |
| CPP_component.dsp | Project settings. Note the new extension; in previous Visual C++ versions project file names ended in MAK. |
| CPP_component.dsw | Workspace settings. As with project settings, these also had a different extension (MDP) in earlier versions. |
| CPP_component.h | Empty header. Will later be replaced by the MIDL compiler. |
| CPP_component.idl | Interface Definition Language file translated by the MIDL compiler. |
| CPP_component.ncb | Visual C++ program database. |
| CPP_component.rc | Resource script. |
Figure 5 Wizard-generated Proxy Files
| File | Description |
| CPP_componentps.def | Linker module definitions for a separate proxy DLL (the ps in the file name stands for proxy stub). |
| CPP_componentps.mk | Proxy stub make file, in case you want to create a separate proxy DLL. |
| dlldatax.c | Wraps the file dlldata.c, which is generated by the MIDL compiler during project build. |
| dlldatax.h | Extern C declarations of dlldata.c exports. |
Figure 8 CPP_component Files
| File | Description |
| CPP_component.plg | Project build logfile. |
| CPP_component.tlb | COM Type library generated by MIDL compiler. |
| CPP_component_i.cpp | COM, CLSID, and IID definitions generated by MIDL compiler. |
| CPP_component_p.cpp | Proxy stub code. |
| dlldata.c | List of proxies, generated by MIDL compiler and wrapped by dlldatax.cpp. |
Figure 10 CPP_component
CAccount.cpp
#include "stdafx.h"
#include "CAccount.h"
#include <tchar.h>
CAccount::CAccount()
{
m_lAccountID = 0;
m_dblBalance = 0.0;
m_cbAccountID = 0;
m_cbBalance = 0;
}
CAccount::~CAccount()
{
}
BOOL CAccount::Prepare(LPCTSTR lpszQuery /* = NULL */)
{
BOOL bRet = CRow::Prepare(lpszQuery);
bRet &= BindParam(1, &m_lAccountID, &m_cbAccountID);
bRet &= BindParam(2, &m_dblBalance, &m_cbBalance);
return bRet;
}
BOOL CAccount::Execute()
{
BOOL bRet = CRow::Execute();
bRet &= Bind(1, &m_lAccountID, &m_cbAccountID);
bRet &= Bind(2, &m_dblBalance, &m_cbBalance);
return bRet;
}
BOOL CAccount::IsValidAccount(long lAccountID)
{
HSTMT hStmtValid = NULL;
RETCODE rc;
// This function uses a separate statement that shows off a sp
// call. You can use the Visual C++ Enterprise SQL Debugger to
// debug the sp.
//
// Prepare the statement
::SQLAllocStmt(m_hConnect, &hStmtValid);
if (hStmtValid == NULL)
return FALSE;
::SQLSetStmtOption(m_hStmt, SQL_CURSOR_TYPE,
SQL_CURSOR_FORWARD_ONLY);
::SQLSetStmtOption(m_hStmt, SQL_CONCURRENCY,
SQL_CONCUR_READ_ONLY);
::SQLPrepare(hStmtValid, (SQLCHAR*) "{call sp_validaccount(?)}",
SQL_NTS);
::SQLBindParameter(hStmtValid, 1, SQL_PARAM_INPUT, SQL_C_LONG,
SQL_INTEGER, 0, 0, &m_lAccountID, 0, &m_cbAccountID);
// Execute the statement if it fails, we know the account doesn't
// exist.
m_lAccountID = lAccountID;
rc = ::SQLExecute(hStmtValid);
::SQLFreeStmt(hStmtValid, SQL_DROP);
return SQLSUCCEEDED(rc);
}
CPP_component.h
/* this ALWAYS GENERATED file contains the definitions for the interfaces */
/* File created by MIDL compiler version 3.01.75 */
/* at Sat Feb 22 21:15:31 1997
*/
/* Compiler settings for CPP_component.idl:
Oicf (OptLev=i2), W1, Zp8, env=Win32, ms_ext, c_ext
error checks: none
*/
//@@MIDL_FILE_HEADING( )
#include "rpc.h"
#include "rpcndr.h"
#ifndef COM_NO_WINDOWS_H
#include "windows.h"
#include "ole2.h"
#endif /*COM_NO_WINDOWS_H*/
#ifndef __CPP_component_h__
#define __CPP_component_h__
#ifdef __cplusplus
extern "C"{
#endif
/* Forward Declarations */
#ifndef __IStock_FWD_DEFINED__
#define __IStock_FWD_DEFINED__
typedef interface IStock IStock;
#endif /* __IStock_FWD_DEFINED__ */
#ifndef __CStock_FWD_DEFINED__
#define __CStock_FWD_DEFINED__
#ifdef __cplusplus
typedef class CStock CStock;
#else
typedef struct CStock CStock;
#endif /* __cplusplus */
#endif /* __CStock_FWD_DEFINED__ */
/* header files for imported files */
#include "oaidl.h"
#include "ocidl.h"
void __RPC_FAR * __RPC_USER MIDL_user_allocate(size_t);
void __RPC_USER MIDL_user_free( void __RPC_FAR * );
#ifndef __IStock_INTERFACE_DEFINED__
#define __IStock_INTERFACE_DEFINED__
/****************************************
* Generated header for interface: IStock
* at Sat Feb 22 21:15:31 1997
* using MIDL 3.01.75
****************************************/
/* [unique][helpstring][dual][uuid][object] */
EXTERN_C const IID IID_IStock;
#if defined(__cplusplus) && !defined(CINTERFACE)
interface DECLSPEC_UUID("C8D363E5-FC5C-11CF-A288-00A0C905A457")
IStock : public IDispatch
{
public:
virtual /* [helpstring] */ HRESULT STDMETHODCALLTYPE Buy(
/* [in] */ long lAccount,
/* [in] */ BSTR bstrStock,
/* [in] */ long lShares,
/* [in] */ double dblPrice) = 0;
virtual /* [helpstring] */ HRESULT STDMETHODCALLTYPE Sell(
/* [in] */ long lAccount,
/* [in] */ BSTR bstrStock,
/* [in] */ long lShares,
/* [in] */ double dblPrice) = 0;
virtual /* [helpstring][propput] */ HRESULT STDMETHODCALLTYPE put_Commission(
/* [in] */ double dblCommission) = 0;
virtual /* [helpstring][propget] */ HRESULT STDMETHODCALLTYPE get_Commission(
/* [retval][out] */ double __RPC_FAR *retval) = 0;
};
#else /* C style interface */
typedef struct IStockVtbl
{
BEGIN_INTERFACE
HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryInterface )(
IStock __RPC_FAR * This,
/* [in] */ REFIID riid,
/* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject);
ULONG ( STDMETHODCALLTYPE __RPC_FAR *AddRef )(
IStock __RPC_FAR * This);
ULONG ( STDMETHODCALLTYPE __RPC_FAR *Release )(
IStock __RPC_FAR * This);
HRESULT ( STDMETHODCALLTYPE __RPC_FAR *GetTypeInfoCount )(
IStock __RPC_FAR * This,
/* [out] */ UINT __RPC_FAR *pctinfo);
HRESULT ( STDMETHODCALLTYPE __RPC_FAR *GetTypeInfo )(
IStock __RPC_FAR * This,
/* [in] */ UINT iTInfo,
/* [in] */ LCID lcid,
/* [out] */ ITypeInfo __RPC_FAR *__RPC_FAR *ppTInfo);
HRESULT ( STDMETHODCALLTYPE __RPC_FAR *GetIDsOfNames )(
IStock __RPC_FAR * This,
/* [in] */ REFIID riid,
/* [size_is][in] */ LPOLESTR __RPC_FAR *rgszNames,
/* [in] */ UINT cNames,
/* [in] */ LCID lcid,
/* [size_is][out] */ DISPID __RPC_FAR *rgDispId);
/* [local] */ HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Invoke )(
IStock __RPC_FAR * This,
/* [in] */ DISPID dispIdMember,
/* [in] */ REFIID riid,
/* [in] */ LCID lcid,
/* [in] */ WORD wFlags,
/* [out][in] */ DISPPARAMS __RPC_FAR *pDispParams,
/* [out] */ VARIANT __RPC_FAR *pVarResult,
/* [out] */ EXCEPINFO __RPC_FAR *pExcepInfo,
/* [out] */ UINT __RPC_FAR *puArgErr);
/* [helpstring] */ HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Buy )(
IStock __RPC_FAR * This,
/* [in] */ long lAccount,
/* [in] */ BSTR bstrStock,
/* [in] */ long lShares,
/* [in] */ double dblPrice);
/* [helpstring] */ HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Sell )(
IStock __RPC_FAR * This,
/* [in] */ long lAccount,
/* [in] */ BSTR bstrStock,
/* [in] */ long lShares,
/* [in] */ double dblPrice);
/* [helpstring][propput] */ HRESULT ( STDMETHODCALLTYPE __RPC_FAR
*put_Commission )(
IStock __RPC_FAR * This,
/* [in] */ double dblCommission);
/* [helpstring][propget] */ HRESULT ( STDMETHODCALLTYPE __RPC_FAR
*get_Commission )(
IStock __RPC_FAR * This,
/* [retval][out] */ double __RPC_FAR *retval);
END_INTERFACE
} IStockVtbl;
interface IStock
{
CONST_VTBL struct IStockVtbl __RPC_FAR *lpVtbl;
};
#ifdef COBJMACROS
#define IStock_QueryInterface(This,riid,ppvObject) \
(This)->lpVtbl -> QueryInterface(This,riid,ppvObject)
#define IStock_AddRef(This) \
(This)->lpVtbl -> AddRef(This)
#define IStock_Release(This) \
(This)->lpVtbl -> Release(This)
#define IStock_GetTypeInfoCount(This,pctinfo) \
(This)->lpVtbl -> GetTypeInfoCount(This,pctinfo)
#define IStock_GetTypeInfo(This,iTInfo,lcid,ppTInfo) \
(This)->lpVtbl -> GetTypeInfo(This,iTInfo,lcid,ppTInfo)
#define IStock_GetIDsOfNames(This,riid,rgszNames,cNames,lcid,rgDispId) \
(This)->lpVtbl -> GetIDsOfNames(This,riid,rgszNames,cNames,lcid,rgDispId)
#define IStock_Invoke(This,dispIdMember,riid,lcid,wFlags,pDispParams,pVarResult,pExcepInfo,puArgErr) \
(This)->lpVtbl -> Invoke(This,dispIdMember,riid,lcid,wFlags,pDispParams,pVarResult,pExcepInfo,puArgErr)
#define IStock_Buy(This,lAccount,bstrStock,lShares,dblPrice) \
(This)->lpVtbl -> Buy(This,lAccount,bstrStock,lShares,dblPrice)
#define IStock_Sell(This,lAccount,bstrStock,lShares,dblPrice) \
(This)->lpVtbl -> Sell(This,lAccount,bstrStock,lShares,dblPrice)
#define IStock_put_Commission(This,dblCommission) \
(This)->lpVtbl -> put_Commission(This,dblCommission)
#define IStock_get_Commission(This,retval) \
(This)->lpVtbl -> get_Commission(This,retval)
#endif /* COBJMACROS */
#endif /* C style interface */
/* [helpstring] */ HRESULT STDMETHODCALLTYPE IStock_Buy_Proxy(
IStock __RPC_FAR * This,
/* [in] */ long lAccount,
/* [in] */ BSTR bstrStock,
/* [in] */ long lShares,
/* [in] */ double dblPrice);
void __RPC_STUB IStock_Buy_Stub(
IRpcStubBuffer *This,
IRpcChannelBuffer *_pRpcChannelBuffer,
PRPC_MESSAGE _pRpcMessage,
DWORD *_pdwStubPhase);
/* [helpstring] */ HRESULT STDMETHODCALLTYPE IStock_Sell_Proxy(
IStock __RPC_FAR * This,
/* [in] */ long lAccount,
/* [in] */ BSTR bstrStock,
/* [in] */ long lShares,
/* [in] */ double dblPrice);
void __RPC_STUB IStock_Sell_Stub(
IRpcStubBuffer *This,
IRpcChannelBuffer *_pRpcChannelBuffer,
PRPC_MESSAGE _pRpcMessage,
DWORD *_pdwStubPhase);
/* [helpstring][propput] */ HRESULT STDMETHODCALLTYPE IStock_put_Commission_Proxy(
IStock __RPC_FAR * This,
/* [in] */ double dblCommission);
void __RPC_STUB IStock_put_Commission_Stub(
IRpcStubBuffer *This,
IRpcChannelBuffer *_pRpcChannelBuffer,
PRPC_MESSAGE _pRpcMessage,
DWORD *_pdwStubPhase);
/* [helpstring][propget] */ HRESULT STDMETHODCALLTYPE IStock_get_Commission_Proxy(
IStock __RPC_FAR * This,
/* [retval][out] */ double __RPC_FAR *retval);
void __RPC_STUB IStock_get_Commission_Stub(
IRpcStubBuffer *This,
IRpcChannelBuffer *_pRpcChannelBuffer,
PRPC_MESSAGE _pRpcMessage,
DWORD *_pdwStubPhase);
#endif /* __IStock_INTERFACE_DEFINED__ */
#ifndef __CPP_COMPONENTLib_LIBRARY_DEFINED__
#define __CPP_COMPONENTLib_LIBRARY_DEFINED__
/****************************************
* Generated header for library: CPP_COMPONENTLib
* at Sat Feb 22 21:15:31 1997
* using MIDL 3.01.75
****************************************/
/* [helpstring][version][uuid] */
EXTERN_C const IID LIBID_CPP_COMPONENTLib;
#ifdef __cplusplus
EXTERN_C const CLSID CLSID_CStock;
class DECLSPEC_UUID("C8D363E9-FC5C-11CF-A288-00A0C905A457")
CStock;
#endif
#endif /* __CPP_COMPONENTLib_LIBRARY_DEFINED__ */
/* Additional Prototypes for ALL interfaces */
unsigned long __RPC_USER BSTR_UserSize( unsigned long __RPC_FAR *,
unsigned long,
BSTR __RPC_FAR *);
unsigned char __RPC_FAR * __RPC_USER BSTR_UserMarshal( unsigned long __RPC_FAR *,
unsigned char __RPC_FAR *,
BSTR __RPC_FAR *);
unsigned char __RPC_FAR * __RPC_USER BSTR_UserUnmarshal(unsigned long __RPC_FAR *,
unsigned char __RPC_FAR *,
BSTR __RPC_FAR *);
void __RPC_USER BSTR_UserFree( unsigned long __RPC_FAR *,
BSTR __RPC_FAR *);
/* end of Additional Prototypes */
#ifdef __cplusplus
}
#endif
#endif
CPP_component.cpp
// CPP_component.cpp : Implementation of DLL Exports.
// Note: Proxy/Stub Information
// To merge the proxy/stub code into the object DLL, add the file
// dlldatax.c to the project. Make sure precompiled headers
// are turned off for this file, and add _MERGE_PROXYSTUB to the
// defines for the project.
//
// If you are not running WinNT4.0 or Win95 with DCOM, then you
// need to remove the following define from dlldatax.c
// #define _WIN32_WINNT 0x0400
//
// Further, if you are running MIDL without /Oicf switch, you also
// need to remove the following define from dlldatax.c.
// #define USE_STUBLESS_PROXY
//
// Modify the custom build rule for CPP_component.idl by adding the following
// files to the Outputs.
// CPP_component_p.c
// dlldata.c
// To build a separate proxy/stub DLL,
// run nmake -f CPP_componentps.mk in the project directory.
#include "stdafx.h"
#include "resource.h"
#include "initguid.h"
#include "CPP_component.h"
#include "CStock.h"
#include "dlldatax.h"
#define IID_DEFINED /** new macro definition **/
#include "CPP_component_i.c"
#ifdef _MERGE_PROXYSTUB
extern "C" HINSTANCE hProxyDll;
#endif
CComModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_CStock, CStock) /** new entry **/
END_OBJECT_MAP()
/////////////////////////////////////////////////////////////////////////////
// DLL Entry Point
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
lpReserved;
#ifdef _MERGE_PROXYSTUB
if (!PrxDllMain(hInstance, dwReason, lpReserved))
return FALSE;
#endif
if (dwReason == DLL_PROCESS_ATTACH)
{
_Module.Init(ObjectMap, hInstance);
DisableThreadLibraryCalls(hInstance);
}
else if (dwReason == DLL_PROCESS_DETACH)
_Module.Term();
return TRUE; // ok
}
/////////////////////////////////////////////////////////////////////////////
// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow(void)
{
#ifdef _MERGE_PROXYSTUB
if (PrxDllCanUnloadNow() != S_OK)
return S_FALSE;
#endif
return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
#ifdef _MERGE_PROXYSTUB
if (PrxDllGetClassObject(rclsid, riid, ppv) == S_OK)
return S_OK;
#endif
return _Module.GetClassObject(rclsid, riid, ppv);
}
/////////////////////////////////////////////////////////////////////////////
// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer(void)
{
#ifdef _MERGE_PROXYSTUB
HRESULT hRes = PrxDllRegisterServer();
if (FAILED(hRes))
return hRes;
#endif
// registers object, typelib and all interfaces in typelib
return _Module.RegisterServer(TRUE);
}
/////////////////////////////////////////////////////////////////////////////
// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
#ifdef _MERGE_PROXYSTUB
PrxDllUnregisterServer();
#endif
_Module.UnregisterServer();
return S_OK;
}
CPP_component.idl
// CPP_component.idl : IDL source for CPP_component.dll
//
// This file will be processed by the MIDL tool to
// produce the type library (CPP_component.tlb) and marshalling code.
import "oaidl.idl";
import "ocidl.idl";
/** almost all of this file's contents are new **/
[
object,
uuid(C8D363E5-FC5C-11CF-A288-00A0C905A457),
dual,
helpstring("IStock Interface"),
pointer_default(unique)
]
interface IStock : IDispatch
{
import "oaidl.idl";
[
helpstring("Buys Stock if Available")
]
HRESULT Buy([in] long lAccount, [in] BSTR bstrStock,
[in] long lShares, [in] double dblPrice);
[
helpstring("Sells Stock if Available")
]
HRESULT Sell([in] long lAccount, [in] BSTR bstrStock,
[in] long lShares, [in] double dblPrice);
[
propput,
helpstring("Sets Commission")
]
HRESULT Commission([in] double dblCommission);
[
propget,
helpstring("Gets Commission")
]
HRESULT Commission([out, retval] double* retval);
};
[
uuid(C8D363E3-FC5C-11CF-A288-00A0C905A457),
version(1.0),
helpstring("CPP_component 1.0 Type Library")
]
library CPP_COMPONENTLib
{
importlib("stdole32.tlb");
[
uuid(C8D363E9-FC5C-11CF-A288-00A0C905A457),
helpstring("Stock Class")
]
coclass CStock
{
[default] interface IStock;
};
};
CPP_component_i.c
/* this file contains the actual definitions of */
/* the IIDs and CLSIDs */
/* link this file in with the server and any clients */
/* File created by MIDL compiler version 3.01.75 */
/* at Sat Feb 22 21:15:31 1997
*/
/* Compiler settings for CPP_component.idl:
Oicf (OptLev=i2), W1, Zp8, env=Win32, ms_ext, c_ext
error checks: none
*/
//@@MIDL_FILE_HEADING( )
#ifdef __cplusplus
extern "C"{
#endif
#ifndef __IID_DEFINED__
#define __IID_DEFINED__
typedef struct _IID
{
unsigned long x;
unsigned short s1;
unsigned short s2;
unsigned char c[8];
} IID;
#endif // __IID_DEFINED__
#ifndef CLSID_DEFINED
#define CLSID_DEFINED
typedef IID CLSID;
#endif // CLSID_DEFINED
const IID IID_IStock = {0xC8D363E5,0xFC5C,0x11CF,{0xA2,0x88,0x00,0xA0,0xC9,0x05,0xA4,0x57}};
const IID LIBID_CPP_COMPONENTLib = {0xC8D363E3,0xFC5C,0x11CF,{0xA2,0x88,0x00,0xA0,0xC9,0x05,0xA4,0x57}};
const CLSID CLSID_CStock = {0xC8D363E9,0xFC5C,0x11CF,{0xA2,0x88,0x00,0xA0,0xC9,0x05,0xA4,0x57}};
#ifdef __cplusplus
}
#endif
CRow.h
#if !defined DEF_CRow_
#define DEF_CRow_
class CRow
{
// Constructors and Destructors
public:
CRow();
virtual ~CRow();
// Attributes
protected:
HENV m_hEnvironment;
HDBC m_hConnect;
HSTMT m_hStmt;
UWORD m_uRowStatus;
LPTSTR m_lpszQuery;
// Operations
public:
// Open & Close
virtual BOOL Open(DWORD dwCursor = SQL_CURSOR_FORWARD_ONLY,
DWORD dwConcur = SQL_CONCUR_READ_ONLY);
virtual BOOL Close();
// SQL Statement Helpers
virtual LPCTSTR GetDefaultSQL()
{
return NULL;
};
virtual BOOL Prepare(LPCTSTR lpszQuery = NULL);
virtual BOOL Execute();
// Column Binding Helpers
BOOL Bind(short nItem, long* plValue, SDWORD* pcbValue);
BOOL Bind(short nItem, double* pdblValue, SDWORD* pcbValue);
BOOL Bind(short nItem, BOOL* pbValue, SDWORD* pcbValue);
BOOL Bind(short nItem, LPCTSTR lpszValue, SDWORD* pcbValue,
long lLength);
// Parameter Binding Helpers
BOOL BindParam(short nItem, long* plValue, SDWORD* pcbValue);
BOOL BindParam(short nItem, double* pdblValue, SDWORD* pcbValue);
BOOL BindParam(short nItem, BOOL* pbValue, SDWORD* pcbValue);
BOOL BindParam(short nItem, LPCTSTR lpszValue, SDWORD* pcbValue,
long lLength);
// Rowset Helpers : Only forward only handled
BOOL Update(short lRow = 1);
BOOL Delete(short lRow = 1);
BOOL Add(short lRow = 1);
BOOL MoveFirst();
BOOL MoveNext();
// Implementation
protected:
BOOL ReportODBCError(SQLRETURN ret);
void DBReportODBCError();
};
#endif // !defined DEF_CRow_
CStock.h
#if !defined DEF_CStock_
#define DEF_CStock_
#include "CDBStock.h"
#include "CPP_component.h"
#include "resource.h"
class CStock :
public CComDualImpl<IStock, &IID_IStock, &LIBID_CPP_COMPONENTLib>,
public CComObjectRoot,
public CComCoClass<CStock,&CLSID_CStock>
{
public:
CStock();
virtual ~CStock();
BEGIN_COM_MAP(CStock)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IStock)
END_COM_MAP()
DECLARE_NOT_AGGREGATABLE(CStock)
DECLARE_REGISTRY(CStock, _T("Stock.Stock.1"), _T("Stock.Stock"),
IDS_PROJNAME, THREADFLAGS_APARTMENT)
// IStock
public:
STDMETHOD(Buy)(long lAccount, BSTR bstrStock, long lShares, double dblPrice);
STDMETHOD(Sell)(long lAccount, BSTR bstrStock, long lShares, double dblPrice);
STDMETHOD(put_Commission)(double dblCommission);
STDMETHOD(get_Commission)(double* retval);
// Attributes
protected:
CDBStock m_Stock;
double m_dblCommission;
};
#endif // !defined DEF_CStock_
CStock.cpp
#include "stdafx.h"
#include "CStock.h"
#include "CDBStock.h"
#include <mtx.h>
CStock::CStock()
{
m_dblCommission = 0.0;
}
CStock::~CStock()
{
}
STDMETHODIMP CStock::Buy
(long lAccount, BSTR bstrStock, long lShares, double dblPrice)
{
USES_CONVERSION;
HRESULT hr;
IObjectContext* pContext = NULL;
double dblCommission = 0.0;
// Get Context Reference
hr = GetObjectContext(&pContext);
if (hr != S_OK)
return hr;
// Validate Account
get_Commission(&dblCommission);
if (!m_Stock.IsValidAccount(lAccount))
{
pContext->SetAbort();
return E_FAIL;
}
if (!m_Stock.BuyStock(lAccount, OLE2T(bstrStock), lShares,
dblPrice, dblCommission))
{
pContext->SetAbort();
return E_FAIL;
}
pContext->SetComplete();
return S_OK;
}
STDMETHODIMP CStock::Sell
(long lAccount, BSTR bstrStock, long lShares, double dblPrice)
{
USES_CONVERSION;
HRESULT hr;
IObjectContext* pContext = NULL;
double dblCommission = 0.0;
hr = GetObjectContext(&pContext);
if (hr != S_OK)
return hr;
// Validate Account
if (!m_Stock.IsValidAccount(lAccount))
{
pContext->SetAbort();
return E_FAIL;
}
get_Commission(&dblCommission);
if (!m_Stock.SellStock(lAccount, OLE2T(bstrStock), lShares,
dblPrice, dblCommission))
{
pContext->SetAbort();
return E_FAIL;
}
pContext->SetComplete();
return S_OK;
}
STDMETHODIMP CStock::put_Commission(double dblPrice)
{
m_dblCommission = dblPrice;
return S_OK;
}
STDMETHODIMP CStock::get_Commission(double* retval)
{
if (retval == NULL)
return E_POINTER;
*retval = m_dblCommission;
return S_OK;
}
StdAfx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently
#if !defined(AFX_STDAFX_H__AA2B5095_89FC_11D0_AA8A_00A0C9055E55__INCLUDED_)
#define AFX_STDAFX_H__AA2B5095_89FC_11D0_AA8A_00A0C9055E55__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#define STRICT
#define _WIN32_WINNT 0x0400
#define _ATL_APARTMENT_THREADED
#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override
//something, but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>
/** next 4 lines are new **/
#include <sql.h>
#include <sqlext.h>
#define REPORTERROR(x) ReportODBCError(x)
#define SQLSUCCEEDED(x) (!((x)>>1))
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations
// immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__AA2B5095_89FC_11D0_AA8A_00A0C9055E55__INCLUDED)
Figure 13 VB_container Files After Build
|
File |
Description |
|
VB_container.frm |
Visual Basic form. |
|
VB_container.frx |
FoxPro report. |
|
VB_container.exe |
Container application executable. |
|
VB_container.pdb |
Program database, holding symbolic debugging information. Same format as CPP_component.pdb. |
|
VB_container.vbp |
Project file, analogous to CPP_component.dsp. |
|
VB_container.vbw |
Workspace file, analogous to CPP_component.dsw. |
Figure 15 VB_container.frm
VERSION 5.00
Object = "{BDC217C8-ED16-11CD-956C-0000C04E4C0A}#1.1#0"; "TABCTL32.OCX"
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.1#0"; "COMCTL32.OCX"
Begin VB.Form VB_container_
Caption = "VB_container"
ClientHeight = 5340
ClientLeft = 60
ClientTop = 345
ClientWidth = 7245
LinkTopic = "Form1"
ScaleHeight = 5340
ScaleWidth = 7245
StartUpPosition = 3 'Windows Default
Begin ComctlLib.ProgressBar ProgressBar1
Align = 2 'Align Bottom
Height = 240
Left = 0
TabIndex = 22
Top = 5100
Width = 7245
_ExtentX = 12779
_ExtentY = 423
_Version = 327680
Appearance = 1
End
Begin TabDlg.SSTab SSTab1
Height = 4215
Left = 120
TabIndex = 10
Top = 240
Width = 7095
_ExtentX = 12515
_ExtentY = 7435
_Version = 327680
TabHeight = 520
TabCaption(0) = "Purchase"
TabPicture(0) = "VB_container.frx":0000
Tab(0).ControlCount= 9
Tab(0).ControlEnabled= -1 'True
Tab(0).Control(0)= "Purchase_Price_label"
Tab(0).Control(0).Enabled= 0 'False
Tab(0).Control(1)= "Purchase_Shares_label"
Tab(0).Control(1).Enabled= 0 'False
Tab(0).Control(2)= "Purchase_Stock_label"
Tab(0).Control(2).Enabled= 0 'False
Tab(0).Control(3)= "Purchase_Account_label"
Tab(0).Control(3).Enabled= 0 'False
Tab(0).Control(4)= "Purchase_Price"
Tab(0).Control(4).Enabled= 0 'False
Tab(0).Control(5)= "Purchase_Shares"
Tab(0).Control(5).Enabled= 0 'False
Tab(0).Control(6)= "Purchase_Stock"
Tab(0).Control(6).Enabled= 0 'False
Tab(0).Control(7)= "Purchase_Account"
Tab(0).Control(7).Enabled= 0 'False
Tab(0).Control(8)= "Purchase"
Tab(0).Control(8).Enabled= 0 'False
TabCaption(1) = "Sell"
TabPicture(1) = "VB_container.frx":001C
Tab(1).ControlCount= 9
Tab(1).ControlEnabled= 0 'False
Tab(1).Control(0)= "Sell"
Tab(1).Control(0).Enabled= -1 'True
Tab(1).Control(1)= "Sell_Account"
Tab(1).Control(1).Enabled= -1 'True
Tab(1).Control(2)= "Sell_Stock"
Tab(1).Control(2).Enabled= -1 'True
Tab(1).Control(3)= "Sell_Shares"
Tab(1).Control(3).Enabled= -1 'True
Tab(1).Control(4)= "Sell_Price"
Tab(1).Control(4).Enabled= -1 'True
Tab(1).Control(5)= "Sell_Account_label"
Tab(1).Control(5).Enabled= 0 'False
Tab(1).Control(6)= "Sell_Stock_label"
Tab(1).Control(6).Enabled= 0 'False
Tab(1).Control(7)= "Sell_Shares_label"
Tab(1).Control(7).Enabled= 0 'False
Tab(1).Control(8)= "Sell_Price_label"
Tab(1).Control(8).Enabled= 0 'False
TabCaption(2) = "Commission"
TabPicture(2) = "VB_container.frx":0038
Tab(2).ControlCount= 3
Tab(2).ControlEnabled= 0 'False
Tab(2).Control(0)= "Commission"
Tab(2).Control(0).Enabled= -1 'True
Tab(2).Control(1)= "Get_Commission"
Tab(2).Control(1).Enabled= -1 'True
Tab(2).Control(2)= "Commission_label"
Tab(2).Control(2).Enabled= 0 'False
Begin VB.TextBox Commission
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 405
Left = -72600
TabIndex = 20
Top = 720
Width = 3015
End
Begin VB.CommandButton Get_Commission
Caption = "Get Commission"
Height = 495
Left = -72600
TabIndex = 19
Top = 3120
Width = 1815
End
Begin VB.CommandButton Sell
Caption = "Sell"
Height = 495
Left = -72600
TabIndex = 9
Top = 3120
Width = 1815
End
Begin VB.TextBox Sell_Account
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 405
Left = -72600
TabIndex = 5
Top = 720
Width = 3015
End
Begin VB.TextBox Sell_Stock
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 405
Left = -72600
TabIndex = 6
Top = 1200
Width = 3015
End
Begin VB.TextBox Sell_Shares
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 405
Left = -72600
TabIndex = 7
Top = 1680
Width = 3015
End
Begin VB.TextBox Sell_Price
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 405
Left = -72600
TabIndex = 8
Top = 2160
Width = 3015
End
Begin VB.CommandButton Purchase
Caption = "Purchase"
Height = 495
Left = 2400
TabIndex = 4
Top = 3120
Width = 1815
End
Begin VB.TextBox Purchase_Account
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 405
Left = 2400
TabIndex = 0
Top = 720
Width = 3015
End
Begin VB.TextBox Purchase_Stock
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 405
Left = 2400
TabIndex = 1
Top = 1200
Width = 3015
End
Begin VB.TextBox Purchase_Shares
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 405
Left = 2400
TabIndex = 2
Top = 1680
Width = 3015
End
Begin VB.TextBox Purchase_Price
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 405
Left = 2400
TabIndex = 3
Top = 2160
Width = 3015
End
Begin VB.Label Commission_label
Caption = "Commission"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = -74760
TabIndex = 21
Top = 720
Width = 2055
End
Begin VB.Label Sell_Account_label
Caption = "Account Number"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = -74760
TabIndex = 18
Top = 720
Width = 2055
End
Begin VB.Label Sell_Stock_label
Caption = "Stock"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = -74760
TabIndex = 17
Top = 1200
Width = 2055
End
Begin VB.Label Sell_Shares_label
Caption = "Shares"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = -74760
TabIndex = 16
Top = 1680
Width = 2055
End
Begin VB.Label Sell_Price_label
Caption = "Price"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = -74760
TabIndex = 15
Top = 2160
Width = 2055
End
Begin VB.Label Purchase_Account_label
Caption = "Account Number"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 240
TabIndex = 14
Top = 720
Width = 2055
End
Begin VB.Label Purchase_Stock_label
Caption = "Stock"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 240
TabIndex = 13
Top = 1200
Width = 2055
End
Begin VB.Label Purchase_Shares_label
Caption = "Shares"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 240
TabIndex = 12
Top = 1680
Width = 2055
End
Begin VB.Label Purchase_Price_label
Caption = "Price"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 240
TabIndex = 11
Top = 2160
Width = 2055
End
End
End
Attribute VB_Name = "VB_container_"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private CPP_component As CPP_COMPONENTLib.CStock
Private Sub Purchase_Click()
Dim account As Long
Dim stock As String
Dim shares As Long
Dim price As Double
Dim message
account = CLng(Purchase_Account.Text)
stock = CStr(Purchase_Stock.Text)
shares = CLng(Purchase_Shares.Text)
price = CDbl(Purchase_Price.Text)
ProgressBar1.Value = 20
' Call C++ component's IStock::Buy interface
' implemented by CStock::Buy
CPP_component.Buy account, stock, shares, price
ProgressBar1.Value = 60
ProgressBar1.Value = 100
message = MsgBox("Purchase Complete", vbExclamation, "Stock Transactions")
ProgressBar1.Value = 0
End Sub
Private Sub Sell_Click()
Dim account As Long
Dim stock As String
Dim shares As Long
Dim price As Double
Dim message
account = CLng(Sell_Account.Text)
stock = CStr(Sell_Stock.Text)
shares = CLng(Sell_Shares.Text)
price = CDbl(Sell_Price.Text)
ProgressBar1.Value = 20
' Call C++ component's IStock::Sell interface
' implemented by CStock::Sell
CPP_component.Sell account, stock, shares, price
ProgressBar1.Value = 60
ProgressBar1.Value = 100
message = MsgBox("Sale Complete", vbExclamation, "Stock Transactions")
ProgressBar1.Value = 0
End Sub
Private Sub Get_Commission_Click()
' Call C++ component's IStock::Commission interface
' implemented by CStock::get_Commission
Commission = CPP_component.Commission
End Sub
Private Sub Form_Load()
' Call C++ component's CStock constructor.
Set CPP_component = New CPP_COMPONENTLib.CStock
End Sub
Private Sub SSTab1_DblClick()
End Sub