I am trying to connect my C++ project to SQL Server using ADO.it goes well when checking first "hr" but error encounter when opening database on the second "if" statement, it says 'Microsoft C++ exception: _com_error at memory location'.
i wonder what is the problem and how can i open database? or is there another way to open database?
i have already tried to change _ConnectionPtr pConnection = NULL; into this _ConnectionPtr pConnection("ADODB.Connection"). here is my code:
HRESULT hr = S_OK;
_ConnectionPtr pConnection = NULL;
_RecordsetPtr pRecordset = NULL;
_bstr_t strCon("Data Source=...\\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True");
hr = pConnection.CreateInstance((__uuidof(Connection)));
if (FAILED(hr))
{
printf("Error instantiating Connection objectn");
}
//Open the SQL Server connection
hr = pConnection->Open(strCon, "", "", 0);
if (FAILED(hr))
{
printf("Error Opening Database object using ADO _ConnectionPtr n");
}
Related
I am using Media Foundation to create a webcam viewer.
Critical to this application is that the webcam stream is horizontally mirrored.
I am using the Video Processor MFT
to achieve this.
Here's the relevant code to add the MFT:
void tryMirror(IMFPMediaPlayer* pPlayer) {
IMFTransform* pMFT = NULL;
IMFVideoProcessorControl* pVPC = NULL;
HRESULT hr = CoCreateInstance(CLSID_VideoProcessorMFT, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pMFT));
if (FAILED(hr)) {
ShowHResultErrorMessage(L"CoCreateInstance(CLSID_VideoProcessorMFT, ...) failed", hr);
goto done;
}
hr = pMFT->QueryInterface(&pVPC);
if (FAILED(hr)) {
ShowHResultErrorMessage(L"pMFT->QueryInterface(&pVPC) failed", hr);
goto done;
}
hr = pVPC->SetMirror(MIRROR_HORIZONTAL);
if (FAILED(hr)) {
ShowHResultErrorMessage(L"pVPC->SetMirror(MIRROR_HORIZONTAL) failed", hr);
goto done;
}
hr = pPlayer->InsertEffect(pMFT, FALSE); // Not optional - critical functionality
if (FAILED(hr)) {
ShowHResultErrorMessage(L"m_pPlayer->InsertEffect(CLSID_VideoProcessorMFT) failed", hr);
goto done;
}
done:
SafeRelease(&pMFT);
SafeRelease(&pVPC);
}
// class CPreview implements IMFPMediaPlayerCallback as follows
void STDMETHODCALLTYPE CPreview::OnMediaPlayerEvent(MFP_EVENT_HEADER* pEventHeader) {
switch (pEventHeader->eEventType)
{
//...
case MFP_EVENT_TYPE_MEDIAITEM_SET:
OnMediaItemSet(MFP_GET_MEDIAITEM_SET_EVENT(pEventHeader));
break;
case MFP_EVENT_TYPE_PLAY:
OnMfpPlay(MFP_GET_PLAY_EVENT(pEventHeader));
break;
}
}
// Called after I set the webcam media source
void CPreview::OnMediaItemSet(MFP_MEDIAITEM_SET_EVENT* /*pEvent*/)
{
HRESULT hr = m_pPlayer->Play();
if (FAILED(hr)) {
ShowHResultErrorMessage(L"m_pPlayer->Play() failed", hr);
}
}
void CPreview::OnMfpPlay(MFP_PLAY_EVENT* pEvent) {
if (FAILED(pEvent->header.hrEvent)) {
ShowHResultErrorMessage(L"OnMfpPlay failed", pEvent->header.hrEvent);
WCHAR msg[1000];
HRESULT hr = StringCbPrintf(msg, sizeof(msg), L"Event type: 0x%X", pEvent->header.eEventType);
ShowErrorMessage(msg);
return;
}
}
void ShowHResultErrorMessage(PCWSTR errContext, HRESULT hrErr) {
_com_error err(hrErr);
LPCTSTR hrErrMsg = err.ErrorMessage();
WCHAR msg[1000];
HRESULT hr = StringCbPrintf(msg, sizeof(msg), L"%s (HRESULT=0x%X, %s)", errContext, hrErr, hrErrMsg);
if (SUCCEEDED(hr)) {
MessageBox(NULL, msg, L"Error", MB_ICONERROR);
}
}
On my development machine, this program runs without error, exactly as desired.
However, on a different user machine,
it fails with this error:
OnMfpPlay failed (HRESULT=0xC00D36B2, The request is invalid in the current state.)
That is,
this error comes through on the OnMediaPlayerEvent callback
of the IMFPMediaPlayerCallback object.
I do know a few things about the machine that this fails on:
The user has also run a modified version,
with the MFT set to optional, like so:
pPlayer->InsertEffect(pMFT, TRUE).
In this case, the program runs,
but the mirroring MFT has no effect.
The error is definitely caused by this MFT.
This user is running Windows 10, version 1909.
The Video Processor MFT is clearly available.
Its API claims to work - all HRESULTs are successful.
This error, "The request is invalid in the current state", could mean anything, and
I can't find any way to get more observability.
What does 'The request is invalid in the current state' mean?
Why is it generated by adding a Video Processor MFT, only on some machines?
How can I debug this with a more specific error?
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);
I'm working on a project that needs to install a series of Windows Task Scheduler tasks and for that I created a Wix project and a Custom Action that takes care of all the details. That Custom Action was created using C++ to avoid dependencies on the .NET Framework.
At first I started executing SCHTASKS.EXE from a Wix custom action but, after I managed to create the correct command line to install a task correctly, I realized I couldn't set the Action's Working Directory ('Start in' in the Task Scheduler UI) from the SCHTASKS.EXE command line, because it simply didn't have the option...
I decided then to use COM in C++ (#import <taskschd.dll> raw_interfaces_only) to get to the Task Scheduler and tweak the WorkingFolder using the API after installing the Task using SCHTASKS.EXE which was working fine besides that detail.
I managed to get to the task and read its values after installing the task correctly but when I executed put_WorkingDirectory method with the current value that didn't fail actually, but the value wasn't saved into the task.
Does anyone have a clue why I couldn't get there? This is part of the code I used to get to the ExecAction and set the value successfully. Remember this is within a Wix Custom action so some of the methods calls are Wix's.
This code is actually working and the log shows the correct path I intend to set, but the task doesn't get changed. What am I doing wrong?
HRESULT UpdateWorkingDirectory(TaskScheduler::ITaskFolderPtr rootFolder, BSTR taskName, BSTR installFolder)
{
HRESULT hr = S_OK;
TaskScheduler::IRegisteredTaskCollectionPtr taskCollection;
LONG numTasks = 0;
TaskScheduler::IRegisteredTaskPtr thisTask;
TaskScheduler::ITaskDefinitionPtr definition;
TaskScheduler::IActionCollectionPtr actions;
TaskScheduler::IActionPtr action;
TaskScheduler::IExecActionPtr execAction;
long actionCount;
hr = rootFolder->GetTasks(NULL, &taskCollection);
ExitOnFailure(hr, "Cannot get task collection pointer");
hr = taskCollection->get_Count(&numTasks);
ExitOnFailure(hr, "Cannot get task collection item count");
for (LONG taskIdx = 0; taskIdx < numTasks; taskIdx++) {
TaskScheduler::IRegisteredTaskPtr registeredTask;
bstr_t taskIdxName;
hr = taskCollection->get_Item(variant_t(taskIdx + 1), ®isteredTask);
ExitOnFailure(hr, "Cannot get task item %d", taskIdx + 1);
hr = registeredTask->get_Name(&taskIdxName.GetBSTR());
ExitOnFailure(hr, "Cannot get task name");
WcaLog(LOGMSG_STANDARD, " registered task name = %s", (LPCSTR)taskIdxName);
if (strcmp(bstr_t(taskName), taskIdxName) == 0) {
thisTask = registeredTask;
break;
}
}
if (thisTask == NULL) {
hr = E_FAIL;
ExitOnFailure(hr, "task {%S} not found", taskName);
}
hr = thisTask->get_Definition(&definition);
ExitOnFailure(hr, "error getting task definition for {%S}", taskName);
hr = definition->get_Actions(&actions);
ExitOnFailure(hr, "error getting actions for {%S}", taskName);
WcaLog(LOGMSG_STANDARD, " got actions from %S", taskName);
hr = actions->get_Count(&actionCount);
ExitOnFailure(hr, "error getting action count for {%S}", taskName);
WcaLog(LOGMSG_STANDARD, " got count = %d from {%S}", actionCount, taskName);
if (actionCount > 0) {
bstr_t actionId;
bstr_t arguments;
bstr_t path;
hr = actions->get_Item(1, &action);
ExitOnFailure(hr, "error getting action[1] for {%S}", taskName);
hr = action->QueryInterface(&execAction);
ExitOnFailure(hr, "error getting ExecAction for {%S}", taskName);
hr = execAction->get_Id(&actionId.GetBSTR());
ExitOnFailure(hr, "error getting Exec Action id for first exec action of {%S}", taskName);
WcaLog(LOGMSG_STANDARD, " first Exec Action Id is %s", (LPCSTR)actionId);
hr = execAction->get_Arguments(&arguments.GetBSTR());
ExitOnFailure(hr, "error getting Exec Action arguments for first exec action of {%S}", taskName);
WcaLog(LOGMSG_STANDARD, " first Exec Action arguments are %s", (LPCSTR)arguments);
hr = execAction->get_Path(&path.GetBSTR());
ExitOnFailure(hr, "error getting Exec Action path for first exec action of {%S}", taskName);
WcaLog(LOGMSG_STANDARD, " first Exec Action path is %s", (LPCSTR)path);
hr = execAction->put_WorkingDirectory(installFolder);
ExitOnFailure(hr, "error putting working directory for {%S}", taskName);
WcaLog(LOGMSG_STANDARD, " successful put working directory to %S", installFolder);
}
LExit:
return hr;
}
NOTE: All C++ samples I found used simple interface pointers instead of smart pointers, I preferred smart pointers to avoid taking care of release by myself
It seems to me that this is a good example:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa446854(v=vs.85).aspx
More specifically, it shows that you call ITask::Activate before changing things like the working directory, and that you call IPersistFile to save the changes. I can't see any of these in that posted code sample. I've not looked at these interfaces for a while, but that might be the issue.
I found the answer
I had to change my method signature to accommodate username and password
HRESULT UpdateWorkingDirectory(TaskScheduler::ITaskFolderPtr rootFolder, BSTR taskName, BSTR installFolder, variant_t username, variant_t password)
and add these five lines
thisTask = NULL;
hr = rootFolder->RegisterTaskDefinition(taskName, definition, TaskScheduler::_TASK_CREATION::TASK_UPDATE, username, password, TaskScheduler::_TASK_LOGON_TYPE::TASK_LOGON_PASSWORD, variant_t(), &thisTask);
ExitOnFailure(hr, "error updating task regisration for {%S}", taskName);
WcaLog(LOGMSG_STANDARD, " successful update of task regisration to %S", taskName);
after executing put_WorkingDirectory.
This updates the registration of the task definition with the updated value, that was what I wanted to do, and it worked!!!!
I am trying to connect to an SQL Server 2008 instance in a different machine in my same domain, through C++ and ADO. I am able to connect successfully through the SQL Server Management Studio. But by passing the same credentials, in my C++ program, I am getting error: "Login failed for user 'DOMAIN\MyUserID'." Here is the code that I was trying with:
HRESULT hr = -1;
CoInitialize(NULL);
try
{
_ConnectionPtr pConn1("ADODB.Connection");
_CommandPtr pCmd1("ADODB.Command");
hr = pConn1.CreateInstance("ADODB.Connection");
pConn1->Provider = "sqloledb";
hr = pConn1->Open("Data Source='DATAXXX';Initial Catalog='DATA123';Integrated Security=SSPI", "XX", "xyz123", adConnectUnspecified);
//hr value returned is : S_OK
}
catch (_com_error& ex) //ex value is: DB_SEC_E_AUTH_FAILED Authentication failed
{
CString err = ex.Description();
AfxMessageBox(err); //err = Login failed for user 'DOMAIN\\MyUserID'
}
if (hr != S_OK)
{
AfxMessageBox("ERROR: Opening Database connection");
return;
}
pCmd1->ActiveConnection = pConn1;
Please let me know what I am doing wrong here. My OS is Windows 8, and I am using VS2013
I'm tryind to run HttpCalculatorServiceExample located in MSDN. Function WsOpenServiceHost returns error code 0x803d0021. This error means
MessageId: WS_E_OTHER
Unrecognized error occurred in the Windows Web Services framework.
Printed error:
Unable to add URL to HTTP URL group.
Unrecognized error occurred in the Windows Web Services framework.
The process cannot access the file because it is being used by another process.
How to solve this problem?
int __cdecl wmain(int argc, __in_ecount(argc) wchar_t **argv)
{
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
HRESULT hr = NOERROR;
WS_SERVICE_HOST* host = NULL;
WS_SERVICE_ENDPOINT serviceEndpoint = {};
const WS_SERVICE_ENDPOINT* serviceEndpoints[1];
serviceEndpoints[0] = &serviceEndpoint;
WS_ERROR* error = NULL;
WS_SERVICE_ENDPOINT_PROPERTY serviceEndpointProperties[1];
WS_SERVICE_PROPERTY_CLOSE_CALLBACK closeCallbackProperty = {CloseChannelCallback};
serviceEndpointProperties[0].id = WS_SERVICE_ENDPOINT_PROPERTY_CLOSE_CHANNEL_CALLBACK;
serviceEndpointProperties[0].value = &closeCallbackProperty;
serviceEndpointProperties[0].valueSize = sizeof(closeCallbackProperty);
// Initialize service endpoint
serviceEndpoint.address.url.chars = L"http://+:80/example"; // address given as uri
serviceEndpoint.address.url.length = (ULONG)wcslen(serviceEndpoint.address.url.chars);
serviceEndpoint.channelBinding = WS_HTTP_CHANNEL_BINDING; // channel binding for the endpoint
serviceEndpoint.channelType = WS_CHANNEL_TYPE_REPLY; // the channel type
serviceEndpoint.contract = &calculatorContract; // the contract
serviceEndpoint.properties = serviceEndpointProperties;
serviceEndpoint.propertyCount = WsCountOf(serviceEndpointProperties);
// Create an error object for storing rich error information
hr = WsCreateError(
NULL,
0,
&error);
if (FAILED(hr))
{
goto Exit;
}
// Create Event object for closing the server
closeServer = CreateEvent(
NULL,
TRUE,
FALSE,
NULL);
if (closeServer == NULL)
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto Exit;
}
// Creating a service host
hr = WsCreateServiceHost(
serviceEndpoints,
1,
NULL,
0,
&host,
error);
if (FAILED(hr))
{
goto Exit;
}
// WsOpenServiceHost to start the listeners in the service host
hr = WsOpenServiceHost(
host,
NULL,
error);
if (FAILED(hr))
{
goto Exit;
}
WaitForSingleObject(closeServer, INFINITE);
// Close the service host
hr = WsCloseServiceHost(host, NULL, error);
if (FAILED(hr))
{
goto Exit;
}
Exit:
if (FAILED(hr))
{
// Print out the error
PrintError(hr, error);
}
fflush(
stdout);
if (host != NULL)
{
WsFreeServiceHost(host);
}
if (error != NULL)
{
WsFreeError(error);
}
if (closeServer != NULL)
{
CloseHandle(closeServer);
}
fflush(stdout);
return SUCCEEDED(hr) ? 0 : -1;
}
I had the same error "Unrecognized error occurred in the Windows Web Services framework", while tried to debug remote target (C++ project).
I've tried all possible solutions, which I've found over internet: turn-off/uninstall antivirus(I use MS Essentials), turnoff firewall, reinstall VS etc.
But it didn't help.
So, I've deleted all programms (not windows updates) which I've installed last month, reboot computer, and the bug went away.
Uninstalled software:
Foxit PhantomPDF Standard. Версия: 7.0.5.1021.
widecap
CCleaner
Uninstall Tool
just in case, I've also uninstalled Microsoft Security Essentials and after the bug went away I've installed it again and had no problems.
Hope, this helps