MSOLEDBSQL IDbInitialize::Initialize() Return E_FAIL

Copper Contributor

Hi All,

 

I'm using the following environment for development:

  1. Server: SQL Express on Windows 10
  2. Version: 15.0.2101.7
  3. Instance Name: MYSURFACE\SQLEXPRESS
  4. OLE DB Version: 19.3

The goal is to connect to the DB from my C++ code using the aforementioned MSOLEDBSQL driver. Despite my best attempts, I'm not able to connect to the database. The returned value is always E_FAIL and the reason is "Client unable to establish connection." I have also tried several other combinations of different user IDs and password, trusted connection, etc. The result is always the same. I have also tried some wrong connection string keyword (e.g., "User ID=..." instead of "UID=..."). That results in a different error, but the minute I get the string right, it's always E_FAIL with that description. Also, on using "Server=(local)" and other combinations, the connection times out after about 10-15 seconds. The amusing thing is that anytime I have all the connection string keywords right, it fails instantly with that message.

 

Any help would be appreciated.

 

Here's the code I'm using:

 

void InitializeEnvironment()
{
HRESULT hr = S_OK;
IDBInitialize* initializer = nullptr;
IDBProperties* propertySetter = nullptr;
DBPROP props[1];
LPCWSTR lpwszProviderString = L"Server=MYSURFACE\\SQLEXPRESS;"   
L"Database=RefDb_DEV;"
L"Trusted_Connection=yes";
//L"UID=serviceusr;"
//L"PWD=myoldpassword;";
DBPROPSET* prgPropertySets = nullptr;
ULONG cPropertySets = 0;
 
hr = CoInitialize(nullptr);
hr = CoCreateInstance(CLSID_MSOLEDBSQL19, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IDBProperties), reinterpret_cast<void**>(&propertySetter));
 
// set provider string
props[0].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
props[0].dwOptions = DBPROPOPTIONS_REQUIRED;
props[0].colid = DB_NULLID;
VariantInit(&(props[0].vValue));
V_VT(&(props[0].vValue)) = VT_BSTR;
V_BSTR(&(props[0].vValue)) = SysAllocString(lpwszProviderString);
 
{ // set the property to the property set
DBPROPSET PropertySet[1] = {};
PropertySet[0].rgProperties = &props[0];
PropertySet[0].cProperties = 1;
PropertySet[0].guidPropertySet = DBPROPSET_DBINIT;
 
// set properties and connect to server
hr = propertySetter->SetProperties(sizeof(PropertySet) / sizeof(DBPROPSET), PropertySet);
}
 
hr = propertySetter->QueryInterface<IDBInitialize>(&initializer);
//********This is the call that always fails**********
hr = initializer->Initialize();
//*****************************************
if (hr != S_OK)
{
IErrorInfo* errorInfo = nullptr;
hr = GetErrorInfo(0, &errorInfo);
if (hr == S_OK)
{
BSTR errorDesc, source;
errorInfo->GetDescription(&errorDesc);
errorInfo->GetSource(&source);
}
}
 
// get properties
DBPROPIDSET rgDBPropIDSet[1] = {};
 
DBPROPID rgDBPropID[2] = {};
rgDBPropID[0] = SSPROP_INTEGRATEDAUTHENTICATIONMETHOD;
rgDBPropID[1] = SSPROP_MUTUALLYAUTHENTICATED;
 
rgDBPropIDSet[0].rgPropertyIDs = &rgDBPropID[0];
rgDBPropIDSet[0].cPropertyIDs = 2;
rgDBPropIDSet[0].guidPropertySet = DBPROPSET_SQLSERVERDATASOURCEINFO;
hr = propertySetter->GetProperties(1, rgDBPropIDSet, &cPropertySets, &prgPropertySets);
wprintf(L"Authentication method: %s\r\n", V_BSTR(&(prgPropertySets[0].rgProperties[0].vValue)));
wprintf(L"Mutually authenticated: %s\r\n",
(V_BOOL(&(prgPropertySets[0].rgProperties[1].vValue)) == VARIANT_TRUE) ? L"yes" : L"no");
}

 

0 Replies