ITPub博客

首页 > Linux操作系统 > Linux操作系统 > ADOCE 的 Pure C++ 用法(转)

ADOCE 的 Pure C++ 用法(转)

原创 Linux操作系统 作者:jcszjswkzhou 时间:2019-05-10 13:39:04 0 删除 编辑

Using ADOCE with Pure C++…

Most applications these days require some form of data storage, and Microsoft recommends using Microsoft® ActiveX® Data Objects for the Windows® CE operating system (ADOCE). Most samples on ADOCE are written for Microsoft Visual Basic® developers, and there are not many samples out there showing how to use ADOCE with C++. This article takes you through the minimal steps necessary to access ADOCE from Microsoft Visual C++®.

What You Need
  • Microsoft eMbedded Visual Tools.
  • Microsoft eMbedded Visual C++.

Gotchas

ADOCE 2.0, ADOCE 3.0, and ADOCE 3.1 have different ProgIDs you have to watch for when you use the sample below. If you are not sure what ADOCE version is running on your device, you can try to create an ADOCE 3.1 connection object first. If that fails, try to create an ADOCE 3.0 connection object. If your device does not support 3.0 or 3.1, then you should install the latest version for the platform rather then use ADOCE 2.0. ADOCE 2.0 is not fully compatible to ADOCE 3.0 and your code might not work correctly. You can download the latest version of ADOCE 3.1.

Languages Supported

All languages

The Includes…

To include the ADOCE smart pointers, add the following code to the top of your source file:
#include 
namespace MSADOCE
{
#include 
}
#include 

The “namespace” argument will enable the Com Smart Pointer and define the “MSADOCE” namespace used later in the code.

The Code…

The following code snipplet will create an ADOCE connection and an ADOCE recordset, set the connection to the recordset, create a new table, insert a record, and read that record.

First we have to do some COM Smart Pointer declarations:

MSADOCE::IADOCEConnection *iADOCEConn     = NULL; //Connection Object
MSADOCE::IADOCERecordset  *iADOCERS       = NULL; //Recordset Object
MSADOCE::IADOCEFields     *iADOCEFields   = NULL; //Fields Object

The ADOCE.H does not define the two GUIDs (globally unique identifiers) correctly for the Recordset and the Connection interfaces. Therefore we declare both of them here:

IID IID__Recordset = 
{0x113033F6,0xF682,0x11D2,{0xBB,0x62,0x00,0xC0,0x4F,0x68,0x0A,0xCC}};
IID IID__Connection = 
{0x113033DE,0xF682,0x11D2,{0xBB,0x62,0x00,0xC0,0x4F,0x68,0x0A,0xCC}};

The following two lines declare the ADOCE connection and Recordset ProgID.

TCHAR tsADOCE30ConnProgID[]= TEXT("ADOCE.Connection.3.0");
TCHAR tsADOCE30RSProgID[]= TEXT("ADOCE.Recordset.3.0");

Now we can start filling the objects with life:

CLSID tClsid;
HRESULT hr;
VARIANT varConn1,varTSQL,varEmpty;

hr = CoInitializeEx(NULL,COINIT_MULTITHREADED);
if(!SUCCEEDED(hr)) return 0;

VariantInit(&varConn1);
VariantInit(&varTSQL);
VariantInit(&varEmpty);

hr = CLSIDFromProgID( tsADOCE30ConnProgID, &tClsid );
if (FAILED(hr)) return 0; // Error handling here: ADOCE3.0 not found

hr = CoCreateInstance (tClsid, NULL,
                      CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
                      IID__Connection, (LPVOID *)&iADOCEConn);
if(iADOCEConn && !FAILED(hr))
{

The following lines will create an ADOCE connection object for the Microsoft Windows CE Object store. If you want to open a Pocket Access database (.CDB), you can enter the file name of the .CDB file as the first parameter of the “Open” method.

hr=iADOCEConn->put_Provider(TEXT("cedb"));
hr=iADOCEConn->Open(TEXT(""),TEXT(""),
                    TEXT(""),MSADOCE::adOpenUnspecified);

hr=CLSIDFromProgID( tsADOCE30RSProgID, &tClsid );
hr = CoCreateInstance (tClsid, NULL,
                  CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
                  IID__Recordset,(LPVOID *)&iADOCERS);
if(iADOCEConn && !FAILED(hr))
{

The next three lines are very important. I get a lot of questions about how to attach the Connection object to the Recordset correctly. Here is how:

		varConn1.pdispVal=iADOCEConn;
		varConn1.vt=VT_DISPATCH;
		hr=iADOCERS->put_ActiveConnection(varConn1);

We create a new table named “Book841” with integer, date, string, and long varbinary fields.

		varTSQL.bstrVal=SysAllocString(
			TEXT("CREATE TABLE Book841 (MyInt integer, MyDate,
		datetime, MyString varchar(200), MyBlob long varbinary)"));
		varTSQL.vt=VT_BSTR;
		hr=iADOCERS->Open(varTSQL,varEmpty,
				MSADOCE::adOpenForwardOnly,
				MSADOCE:: adLockReadOnly, 
				MSADOCE::adCmdUnknown);
		if (FAILED(hr)) return FALSE;	// Error handling
		SysFreeString(varTSQL.bstrVal);

And now if we want to add a row to the table, we have to open it with a dynamic, updateable cursor.

		varTSQL.bstrVal=SysAllocString(
				TEXT("select * from Book842"));
		varTSQL.vt=VT_BSTR;
		hr=iADOCERS->Open(varTSQL,varEmpty,
				MSADOCE::adOpenDynamic ,
				MSADOCE::adLockOptimistic , 
				MSADOCE::adCmdUnknown);
		if (FAILED(hr)) return FALSE;	// Error handling
		SysFreeString(varTSQL.bstrVal);

		hr=iADOCERS->AddNew(varEmpty,varEmpty);
		hr=iADOCERS->get_Fields(&iADOCEFields);
	}
}

Before you can compile and link the sample, add the OLE32.LIB and OLEAUT32.LIB to the list of object/library modules in the project settings of your project.

What Storage Engine Should I Use?

ADOCE lets you access the Object Store, Microsoft Pocket Access files, and any other storage engine that supports the OLEDB layer for Windows CE. Microsoft recently released Microsoft SQL Server 2000 Windows CE Edition (SQL Server CE). It ships with the developer"s edition of SQL Server 2000 and is a great storage engine for all kind of applications. Since it is about 10 times faster then Pocket Access, this is the storage engine I recommend for any serious developer. And best of all—the runtime of the SQL Server CE is free.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-126774/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-08-29

  • 博文量
    3488
  • 访问量
    2601730