Windows Search C++(MFC) OLE DB or ADO group search - mfc

I have to run a group query like this :
GROUP ON System.ItemName OVER ( SELECT TOP 20 System.ItemName FROM SystemIndex )
But when I run it using C++ ADO Recordset I get an exception at Open ( when I search the SELECT TOP 20 System.ItemName FROM SystemIndex query it ruturns just fine).
so... how to perform a group search like that ?
ADO Code:
_ConnectionPtr connection = NULL;
hr = connection.CreateInstance(__uuidof(Connection));
_RecordsetPtr recordset = NULL;
hr = recordset.CreateInstance(__uuidof(Recordset));
hr = recordset->Open("GROUP ON System.Kind AGGREGATE COUNT() as 'Total' OVER ( SELECT TOP 20 System.ItemName, System.ItemUrl FROM SystemIndex )",
connection.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText);
After trying for a long time I decide to run into OLE DB directly , but it seems dificult to retreive the search data
OLE DB code:
CDataSource cDataSource;
hr = cDataSource.OpenFromInitializationString(connectionstring);
if (SUCCEEDED(hr))
{
CSession cSession;
if (SUCCEEDED(hr))
{
CCommand<CDynamicAccessor, CRowset> cCommand;
hr = cCommand.Open(cSession, L"GROUP ON System.ItemName OVER ( SELECT TOP 20 System.ItemName, System.Itemurl FROM SystemIndex )");
if (SUCCEEDED(hr))
{
for (hr = cCommand.MoveFirst(); S_OK == hr; hr = cCommand.MoveNext())
{
DBORDINAL columns= cCommand.GetColumnCount();
for (DBORDINAL i = 1; i <= columns; i++)
{
PCWSTR pszName = cCommand.GetColumnName(i);
//HOW TO GET DATA TO A sdt::string ?
}
}
cCommand.Close();
}
}
}

Related

SQL query returns DB_E_NOTABLE

I'm building my first MFC app. I'm successfully connecting to the database, but when I try to execute a query, hResult becomes DB_E_NOTABLE.
This is my code:
class CCitiesTable : public CTable<CAccessor<CCitiesTableAccessor> >
{
public:
bool SelectAll(CCitiesArray& oCitiesArray)
{
CDataSource oDataSource;
CSession oSession;
CCitiesTable oCitiesTable;
try
{
HRESULT hResult = CoInitialize(0);
if (FAILED(hResult))
{
//Message( _T("Unable to CoInitialize COM Interface.") );
return FALSE;
}
CDBPropSet oDBPropSet(DBPROPSET_DBINIT);
oDBPropSet.AddProperty(DBPROP_INIT_DATASOURCE, _T("LENOVO2\\SQL2008") ); // сървър
oDBPropSet.AddProperty(DBPROP_AUTH_USERID, _T("sa") );
oDBPropSet.AddProperty(DBPROP_AUTH_PASSWORD, _T("massive") );
oDBPropSet.AddProperty(DBPROP_INIT_CATALOG, _T("PhoneBookDB" ));
oDBPropSet.AddProperty(DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false);
oDBPropSet.AddProperty(DBPROP_INIT_LCID, 1033L);
oDBPropSet.AddProperty(DBPROP_INIT_PROMPT, static_cast<short>(4));
// Свързваме се към базата данни
hResult = oDataSource.Open(_T("SQLOLEDB.1"), &oDBPropSet);
if (FAILED(hResult))
{
//Message(_T("Unable to connect to SQL Server database. Error: %d"), hResult);
return FALSE;
}
//Open session
hResult = oSession.Open(oDataSource);
if (FAILED(hResult))
{
//Message(_T("Unable to open session. Error: %d"), hResult);
oDataSource.Close();
return FALSE;
}
//Make query
CString strQuery;
strQuery = _T("SELECT * FROM dbo.CITIES");
// Execute query
hResult = oCitiesTable.Open(oSession, strQuery);
After executing the code above hResult is DB_E_NOTABLE. Except that the table is there. Database: PhoneBookDB Schema: dbo Table: CITIES. The query works just fine in SQL Server Management Studio.
According to the documentation for CTable::Open, CTable::Open expects a table name, not a select query. The error you are getting is saying that "SELECT * FROM dbo.CITIES" is not a table name (which it is not).
szTableName
[in] The name of the table to open, passed as an ANSI string.
You need to use:
CString strTable = _T("CITIES");
// Open Table
hResult = oCitiesTable.Open(oSession, strTable);

how Fetch Users from Active Directory with a lot of users

I want to know how Fetch Users from Active Directory with a lot of users?
i use this code
hr = ADsGetObject(L"WinNT://localhost",
IID_IADsContainer, (void**) &pCont );
if ( !SUCCEEDED(hr) )
{
return hr;
}
_variant_t var;
IEnumVARIANTPtr pEnum;
ADsBuildEnumerator (pCont,&pEnum);
int cnt=0;
ULONG ulFetched = 0L;
_variant_t vChild;
while((SUCCEEDED(ADsEnumerateNext(pEnum, 1, &vChild, &ulFetched)) && ulFetched==1))
{
///////////
}
but it takes a long time for get users from server(40k users)

Obtaining an image/scan with WIA

I'm new to WIA. I somehow managed to get a device listing, select my device and enumerate this device for an image item. But now, when I try to transfer an image, I'm stuck. If I use the root item for data transfer pWiaDataTransfer->idtGetData returns a HRESULT of 0x8000FFFF (E_UNEXPECTED), if I use the image item (there are only two items 'root' and 'top' on my device) it fails when I obtain the storage interface. I thought maybe I needed to obtain the storage interface from 'root' and the data transfer interface from 'top' but that also fails (when I try to obtain the data transfer interface). I then get 0x80004002 (E_NOINTERFACE - No such interface supported.). Here is the code:
HRESULT TransferWiaItem( IWiaItem *pWiaItem, IWiaItem *pWiaItem2 )
{
IWiaPropertyStorage *pWiaPropertyStorage = NULL;
HRESULT hr = pWiaItem->QueryInterface( IID_IWiaPropertyStorage, (void**)&pWiaPropertyStorage );
if( SUCCEEDED( hr ) )
{
PROPSPEC PropSpec[2] = {0};
PROPVARIANT PropVariant[2] = {0};
const ULONG c_nPropCount = sizeof(PropVariant)/sizeof(PropVariant[0]);
GUID guidOutputFormat = WiaImgFmt_BMP;
PropSpec[0].ulKind = PRSPEC_PROPID;
PropSpec[0].propid = WIA_IPA_FORMAT;
PropSpec[1].ulKind = PRSPEC_PROPID;
PropSpec[1].propid = WIA_IPA_TYMED;
PropVariant[0].vt = VT_CLSID;
PropVariant[0].puuid = &guidOutputFormat;
PropVariant[1].vt = VT_I4;
PropVariant[1].lVal = TYMED_FILE;
hr = pWiaPropertyStorage->WriteMultiple( c_nPropCount, PropSpec, PropVariant, WIA_IPA_FIRST );
if( SUCCEEDED( hr ) )
{
IWiaDataTransfer *pWiaDataTransfer = NULL;
hr = pWiaItem2->QueryInterface( IID_IWiaDataTransfer, (void**)&pWiaDataTransfer );
if( SUCCEEDED( hr ) )
{
CWiaDataCallback *pCallback = new CWiaDataCallback;
if( pCallback )
{
IWiaDataCallback *pWiaDataCallback = NULL;
hr = pCallback->QueryInterface( IID_IWiaDataCallback, (void**)&pWiaDataCallback );
if( SUCCEEDED( hr ) )
{
STGMEDIUM stgMedium = {0};
stgMedium.tymed = TYMED_FILE;
hr = pWiaDataTransfer->idtGetData( &stgMedium, pWiaDataCallback );
...
Where pWiaItem is the 'root' item and pWiaItem2 is the 'top' item.
Anybody have a clue what is going on here?
The solution is to enumerate the items of the 'root' WiaItem in the local context again. I did this in an init method and stored the pointer to the 'top', somehow it seemed so that it got invalidated and the HRESULT did not reflect that. After I changed the code, so that the 'top' WiaItem gets identified right before I use it, everything went fine.

C++ LDAP Query to locate memberOf

I am currently trying to perform an LDAP query in c++ to get the memberOf attribute of a given user. I have a function written that successfully gets the attribute if they are only in one group. The problem is that when they are in more than one group it only returns the first one. When I look at the user in Active Directory browser I can see that it says they have 2 entries for memberOf, but when I use my function I only get the first. Is there a way to modify my function to pull back all the entries or am I completely on the wrong path?
bool FindADMembership(CStringArray* pUserArray,IDirectorySearch *pContainerToSearch, CString sAMAccountName){
if(pContainerToSearch==NULL||pUserArray==NULL)
return false;
CString strSearchFilter;
strSearchFilter.Format("(&(objectClass=user)(objectCategory=person)(sAMAccountName=%s))", sAMAccountName);
BSTR b = strSearchFilter.AllocSysString();
LPOLESTR pszSearchFilter = b;
ADS_SEARCHPREF_INFO SearchPrefs;
SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;
DWORD dwNumPrefs = 1;
LPOLESTR pszColumn = NULL;
ADS_SEARCH_COLUMN col;
HRESULT hr;
IADs *pObj = NULL;
IADs * pIADs = NULL;
ADS_SEARCH_HANDLE hSearch = NULL;
hr = pContainerToSearch->SetSearchPreference( &SearchPrefs, dwNumPrefs);
if (FAILED(hr))
return false;
LPOLESTR pszNonVerboseList[] = {L"memberOf"};
LPOLESTR szName = new OLECHAR[MAX_PATH];
int iCount = 0;
hr = pContainerToSearch->ExecuteSearch(pszSearchFilter, pszNonVerboseList,sizeof(pszNonVerboseList)/sizeof(LPOLESTR),&hSearch);
if ( SUCCEEDED(hr) )
{
hr = pContainerToSearch->GetFirstRow( hSearch);
if (SUCCEEDED(hr))
{
while( hr != S_ADS_NOMORE_ROWS )
{
iCount++;
while( pContainerToSearch->GetNextColumnName( hSearch, &pszColumn ) != S_ADS_NOMORE_COLUMNS )
{
hr = pContainerToSearch->GetColumn( hSearch, pszColumn, &col );
if ( SUCCEEDED(hr) )
{
if (0==wcscmp(L"memberOf", pszColumn))
{
wcscpy(szName,col.pADsValues->CaseIgnoreString);
pUserArray->Add(szName);
}
pContainerToSearch->FreeColumn( &col );
}
FreeADsMem( pszColumn );
}
hr = pContainerToSearch->GetNextRow( hSearch);
}
}
else
return false;
pContainerToSearch->CloseSearchHandle(hSearch);
}
else
return false;
return true; }
Edit:
After working on this here is the code to iterate through the results
if (0==wcscmp(L"memberOf", pszColumn)) {
for(int i = 0; i < col.dwNumValues; i++)
{
wcscpy(szName, col.pADsValues[i].CaseIgnoreString);
pUserArray->Add(szName);
}
}
You need to check the number of values and loop over them to get each in turn - pADSValues is an array that could contain > 1 string. The count is returned in dwNumValues.
if (0==wcscmp(L"memberOf", pszColumn))
{
// This code should actually be a loop for i = 0 to dwNumValues - 1
// with each loop checking the next array entry in the list pADsValues
wcscpy(szName,col.pADsValues->CaseIgnoreString);
pUserArray->Add(szName);
}
See docs here.

ITuner::put_TuneRequest() call ignored

I have a DirectShow graph with a "Microsoft DVBT Network Provider", "AVerMedia BDA DVBT Tuner", "AVerMEdia BDA Digital Capture", "Sample Grabber" and "NULL Renderer".
These filters are connected.
Beside that I also have an "MPEG-2 Demultiplexer" and a "BDA MPEG2 Transport Information Filter", but these two filters are NOT connected! It seems like they have to be here in order to run the graph.
When I start the graph, I'm receiving TS data, but no matter what I do, I'm not able to put the tuning request. I can only capture the MUX data from the last tuned frequency with some other application like Windows Media Center.
Here is the code for putting the tune request:
// creating tuning space
CComPtr<IDVBTuningSpace> pDVBTuningSpace;<br>
hr = pDVBTuningSpace.CoCreateInstance( __uuidof( DVBTuningSpace ) );
WCHAR szFriendlyName[ 64 ] = L"Local DVB-T Digital Antenna";<br> BSTR bstrFriendlyName = SysAllocString( szFriendlyName );
hr = pDVBTuningSpace->put_UniqueName( bstrFriendlyName );<br>
hr = pDVBTuningSpace->put_FriendlyName( bstrFriendlyName );
SysFreeString( bstrFriendlyName );
CComBSTR clsid_dvbt = ("{216C62DF-6D7F-4e9a-8571-05F14EDB766A}");<br>
hr = pDVBTuningSpace->put_NetworkType( clsid_dvbt );<br>
hr = pDVBTuningSpace->put_SystemType( DVB_Terrestrial );<br>
// creating tune request<br>
CComPtr<ITuneRequest> pTuneRequest;
hr = pDVBTuningSpace->CreateTuneRequest( &pTuneRequest );
CComQIPtr<IDVBTuneRequest> pDVBTuneRequest( pTuneRequest );
hr = pDVBTuneRequest->put_ONID( -1 );<br>
hr = pDVBTuneRequest->put_TSID( -1 );<br>
hr = pDVBTuneRequest->put_SID( -1 );
// locator<br>
CComPtr<IDVBTLocator> pDVBTLocator;
hr = pDVBTLocator.CoCreateInstance( __uuidof( DVBTLocator ) );<br>
hr = pDVBTLocator->put_Bandwidth( 8 );<br>
hr = pDVBTLocator->put_CarrierFrequency( 506000 );
hr = pDVBTuneRequest->put_Locator( pDVBTLocator );
CComQIPtr<ITuner> pTuner( pNetworkProvider_ );
hr = pTuner->put_TuneRequest( pDVBTuneRequest );
This is executed immediately after adding the "Microsoft DVBT Network Provider" filter in the graph.
All "hr" values from the above code are S_OK.
What am I doing wrong? Or, did I miss something big in this "tune request" thing.
(Bandwidth and frequency values are correct)
I think put_Bandwidth( 8 ) is wrong, it should be a bandwidth in Hz. Anyway, I show you some code I use. Maybe it helps.
HRESULT hr;
CComBSTR TuningName;
hr = pDVBTuningSpace2.CoCreateInstance(CLSID_DVBTuningSpace);
hr = pDVBTuningSpace2->put_SystemType(DVB_Terrestrial);
TuningName = L"My DVB-T";
hr = pDVBTuningSpace2->put__NetworkType(CLSID_DVBTNetworkProvider);
CComPtr <IDVBTLocator> pDVBTLocator;
hr = pDVBTLocator.CoCreateInstance(CLSID_DVBTLocator);
hr = pDVBTLocator->put_CarrierFrequency(config->GetFreq());
hr = pDVBTLocator->put_Bandwidth(config->GetSymbolRate());
hr = pDVBTuningSpace2->put_DefaultLocator(pDVBTLocator);
hr = pDVBTuningSpace2->put_UniqueName(TuningName);
hr = pDVBTuningSpace2->put_FriendlyName(TuningName);
hr = pDVBTuningSpace2->put_FrequencyMapping(L"");
CComPtr <ITuningSpaceContainer> pTuningSpaceContainer;
hr = pTuningSpaceContainer.CoCreateInstance(CLSID_SystemTuningSpaces);
VARIANT tiIndex;
hr = pTuningSpaceContainer->Add(pDVBTuningSpace2,&tiIndex);
if (!SUCCEEDED(hr)) {
// Get the enumerator for the collection.
CComPtr<IEnumTuningSpaces> pTuningSpaceEnum;
hr = pTuningSpaceContainer->get_EnumTuningSpaces(&pTuningSpaceEnum);
if (SUCCEEDED(hr)) {
// Loop through the collection.
CComPtr<ITuningSpace> pTuningSpace;
//ITuningSpace *pTuningSpace;
tiIndex.intVal=0;
while (S_OK == pTuningSpaceEnum->Next(1, &pTuningSpace, NULL)) {
USES_CONVERSION;
BSTR Name;
hr = pTuningSpace->get_UniqueName(&Name);
if (SUCCEEDED(hr)) {
if (wcscmp(OLE2W(Name), TuningName) == 0) {
hr = pTuningSpaceContainer->put_Item(tiIndex,pDVBTuningSpace2);
}
SysFreeString(Name);
}
tiIndex.intVal++;
//pTuningSpace->Release();
pTuningSpace.Release();
}
}
}
CComPtr<ITuneRequest> pTuneRequest;
hr = pDVBTuningSpace2->CreateTuneRequest(&pTuneRequest);
CComQIPtr<IDVBTuneRequest> pDVBTuneRequest(pTuneRequest);
if(pDVBTuneRequest) {
hr = pDVBTuneRequest->put_SID(config->GetSid());
hr = pDVBTuneRequest->put_TSID(config->GetTsid());
hr = pDVBTuneRequest->put_ONID(config->GetOnid());
}
GUID CLSIDNetworkType;
hr = pDVBTuningSpace2->get__NetworkType(&CLSIDNetworkType);
hr = CoCreateInstance(CLSIDNetworkType, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void **) &pNetworkProvider);
hr = graph->AddFilter(pNetworkProvider,L"Network Provider");
// Query for ITuner.
CComQIPtr<ITuner> pTuner(pNetworkProvider);
if (pTuner) {
// Submit the tune request to the network provider.
hr = pTuner->put_TuneRequest(pTuneRequest);
}
hr = graph->AddFilter(pBdaNetworkTuner,L"BDA Source");
hr = ConnectFilters(pNetworkProvider,pBdaNetworkTuner);
CComPtr<IBaseFilter> pBdaReceiver;
hr = FindDevice(KSCATEGORY_BDA_RECEIVER_COMPONENT, &pBdaReceiver, 0, 0, 0);
hr = graph->AddFilter(pBdaReceiver,L"BDA Receiver");
hr = ConnectFilters(pBdaNetworkTuner,pBdaReceiver);
CComPtr<IBaseFilter> pMpegDemux;
hr = pMpegDemux.CoCreateInstance(CLSID_MPEG2Demultiplexer);
hr = graph->AddFilter(pMpegDemux,L"MPEG Demux");
hr = ConnectFilters(pBdaReceiver,pMpegDemux);
You are doing some things in a different order, but I'm not sure if it matters.