[SQLServerCE2.0] Create an encrypted database (source code)

Source: Internet
Author: User
HRESULThrNOERROR; DBPROPSETdbpropset [2]; DBPROPdbprop [1]; response. DBPROPsscedbprop [2]; response; IDBDataSourceAdmin * pIDBDataSourceAdminNULL; IUnknow

HRESULThr = NOERROR; DBPROPSETdbpropset [2]; DBPROPdbprop [1]; // Property array to initialize the provider. DBPROPsscedbprop [2]; // Property array for SSCE security properties INTi = 0; IDBDataSourceAdmin * pIDBDataSourceAdmin = NULL; IUnknow

HRESULT hr = NOERROR;
DBPROPSET dbpropset [2];
DBPROP dbprop [1]; // Property array to initialize the provider.
DBPROP sscedbprop [2]; // Property array for SSCE security properties
INT I = 0;
IDBDataSourceAdmin * pIDBDataSourceAdmin = NULL;
IUnknown * pIUnknownSession = NULL;
IDBInitialize * pIDBInitialize = NULL;

// Create an instance of the ole db provider.
Hr = CoCreateInstance (CLSID_SQLSERVERCE_2_0, 0, CLSCTX_INPROC_SERVER,
IID_IDBInitialize, (void **) & pIDBInitialize );
If (FAILED (hr ))
{
RETAILMSG (1, (TEXT ("1 = CoCreateInstance: % d/r/n"), GetLastError ()));
Goto CleanExit;
}

// Initialize property structures.
VariantInit (& dbprop [0]. vValue );
For (I = 0; I <sizeof (sscedbprop)/sizeof (sscedbprop [0]); I ++)
{
VariantInit (& sscedbprop [I]. vValue );
}

// Leo: To create a new database, you must specify the DBPROP_INIT_DATASOURCE property
// Specify a name for the database.
Dbprop [0]. dwPropertyID = DBPROP_INIT_DATASOURCE;
Dbprop [0]. dwOptions = DBPROPOPTIONS_REQUIRED;
Dbprop [0]. vValue. vt = VT_BSTR;
Dbprop [0]. vValue. bstrVal = SysAllocString (L "Encrypted. sdf ");
If (NULL = dbprop [0]. vValue. bstrVal)
{
Hr = E_OUTOFMEMORY;
Goto CleanExit;
}

// Specify the property for encryption.
Sscedbprop [0]. dwPropertyID = DBPROP_SSCE_ENCRYPTDATABASE;
Sscedbprop [0]. dwOptions = DBPROPOPTIONS_REQUIRED;
Sscedbprop [0]. vValue. vt = VT_BOOL;
Sscedbprop [0]. vValue. boolVal = VARIANT_TRUE;

// Specify the password.
Sscedbprop [1]. dwPropertyID = DBPROP_SSCE_DBPASSWORD;
Sscedbprop [1]. dwOptions = DBPROPOPTIONS_REQUIRED;
Sscedbprop [1]. vValue. vt = VT_BSTR;
Sscedbprop [1]. vValue. bstrVal = SysAllocString (L "123456"); // Password
If (NULL = sscedbprop [1]. vValue. bstrVal)
{
Hr = E_OUTOFMEMORY;
Goto CleanExit;
}

// Initialize the property sets.
Dbpropset [0]. guidPropertySet = DBPROPSET_DBINIT;
Dbpropset [0]. rgProperties = dbprop;
Dbpropset [0]. cProperties = sizeof (dbprop)/sizeof (dbprop [0]);

Dbpropset [1]. guidPropertySet = DBPROPSET_SSCE_DBINIT;
Dbpropset [1]. rgProperties = sscedbprop;
Dbpropset [1]. cProperties = sizeof (sscedbprop)/sizeof (sscedbprop [0]);

Hr = pIDBInitialize-> QueryInterface (IID_IDBDataSourceAdmin, (void **) & pIDBDataSourceAdmin );
If (FAILED (hr ))
{
RETAILMSG (1, (TEXT ("1 = pIDBInitialize-> QueryInterface: % d/r/n"), hr ));
Goto CleanExit;
}

// Create and initialize the database.
Hr = pIDBDataSourceAdmin-> CreateDataSource (sizeof (dbpropset)/sizeof (dbpropset [0]),
Dbpropset, NULL, IID_IUnknown, & pIUnknownSession );
If (FAILED (hr ))
{
RETAILMSG (1, (TEXT ("1 = pIDBDataSourceAdmin-> CreateDataSource: % d/r/n"), hr ));
Goto CleanExit;
}
// At this point, the new encrypted database is created.

LeanExit:
VariantClear (& dbprop [0]. vValue );
SysFreeString (dbprop [0]. vValue. bstrVal );

For (I = 0; I <sizeof (sscedbprop)/sizeof (sscedbprop [0]); I ++)
{
VariantClear (& sscedbprop [I]. vValue );
}

// Do cleanup tasks here.
If (NULL! = PIDBDataSourceAdmin)
{
PIDBDataSourceAdmin-> Release ();
Pidbceceadmin = NULL;
}
If (NULL! = PIDBInitialize)
{
PIDBInitialize-> Release ();
PIDBInitialize = NULL;
}
If (NULL! = PIUnknownSession)
{
PIUnknownSession-> Release ();
PIUnknownSession = NULL;
}

Return;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.