|
PankajB wrote: catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
instead you can try
e.Description()
it returns _bstr_t that might explain what is causing exception.
Regards,
Sandip.
|
|
|
|
|
PankajB wrote: I am able to test the connection using Visual Studio's Server Explorer option.
If server explorer is able to connect using your connection string, that implies your connection string is valid.
PankajB wrote: m_pConnection is of _ConnectionPtr type
PankajB wrote: I am getting an error "Invalid Connection"
Looking at this article[^], you need to create the connection like this before opening it:
_ConnectionPtr m_pConn;
m_pConn.CreateInstance (__uuidof(Connection));
Maybe that's missing?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I already have that in the CTor Stuart.
|
|
|
|
|
Let me share full code with you.
ado2.h
#if !defined(AFX_ADO2_H_INCLUDED_)
#define AFX_ADO2_H_INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#include <afx.h>
#include <afxdisp.h>
#include <math.h>
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
CString IntToStr(int nVal);
CString LongToStr(long lVal);
CString ULongToStr(unsigned long ulVal);
CString DblToStr(double dblVal, int ndigits = 20);
CString DblToStr(float fltVal);
class CDataAccessManager
{
public:
CDataAccessManager()
{
::CoInitialize(NULL);
m_pConnection = NULL;
m_strConnection = _T("");
m_pConnection.CreateInstance(__uuidof(Connection));
}
virtual ~CDataAccessManager()
{
CloseDB();
m_pConnection.Release();
m_pConnection = NULL;
m_strConnection = _T("");
::CoUninitialize();
}
BOOL OpenDB(LPCTSTR lpstrConnection = _T(""), LPCTSTR lpstrUserID = _T(""), LPCTSTR lpstrPassword = _T(""));
_ConnectionPtr GetActiveConnection()
{return m_pConnection;};
BOOL Execute(LPCTSTR lpstrExec);
BOOL IsOpenDB();
void CloseDB();
void SetConnectionString(LPCTSTR lpstrConnection)
{m_strConnection = lpstrConnection;};
CString GetConnectionString()
{return m_strConnection;};
protected:
void dump_com_error(_com_error &e);
public:
_ConnectionPtr m_pConnection;
protected:
CString m_strConnection;
};
#endif //AFX_ADO2_H_INCLUDED_
ado2.cpp
#include "ado2.h"
CString IntToStr(int nVal)
{
CString strRet;
char buff[10];
itoa(nVal, buff, 10);
strRet = buff;
return strRet;
}
CString LongToStr(long lVal)
{
CString strRet;
char buff[20];
ltoa(lVal, buff, 10);
strRet = buff;
return strRet;
}
CString ULongToStr(unsigned long ulVal)
{
CString strRet;
char buff[20];
ultoa(ulVal, buff, 10);
strRet = buff;
return strRet;
}
CString DblToStr(double dblVal, int ndigits)
{
CString strRet;
char buff[50];
_gcvt(dblVal, ndigits, buff);
strRet = buff;
return strRet;
}
CString DblToStr(float fltVal)
{
CString strRet;
char buff[50];
_gcvt(fltVal, 10, buff);
strRet = buff;
return strRet;
}
BOOL CDataAccessManager::OpenDB(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
HRESULT hr = S_OK;
if(IsOpenDB())
CloseDB();
if(lstrcmp(lpstrConnection, _T("")) != 0)
m_strConnection = lpstrConnection;
ASSERT(!m_strConnection.IsEmpty());
try
{
hr = m_pConnection->Open(L"DRIVER={sql server};SERVER=ASHISH-DB\ULXDBSERVER;Database=eDiscoveryDEV;" L"UID=edm; PWD=Password;", L"", L"", adOpenUnspecified);
return hr == S_OK;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CDataAccessManager::Execute(LPCTSTR lpstrExec)
{
ASSERT(m_pConnection != NULL);
ASSERT(lstrcmp(lpstrExec, _T("")) != 0);
_variant_t vRecords;
try
{
m_pConnection->CursorLocation = adUseClient;
m_pConnection->Execute(_bstr_t(lpstrExec), &vRecords, adExecuteNoRecords);
return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CDataAccessManager::IsOpenDB()
{
if(m_pConnection )
return m_pConnection->GetState() != adStateClosed;
return FALSE;
}
void CDataAccessManager::CloseDB()
{
if(IsOpenDB())
m_pConnection->Close();
}
void CDataAccessManager::dump_com_error(_com_error &e)
{
}
DAL.cpp
#include "stdafx.h"
#include "ado2.h"
int _tmain(int argc, _TCHAR* argv[])
{
CDataAccessManager objDAL;
bool bSuccess = objDAL.OpenDB(_T("DRIVER={sql server};SERVER=ASHISH-DB\ULXDBSERVER;Database=LexCoreDB"), _T("edm"), _T("Password"));
return 0;
}
|
|
|
|
|
Any updates? Please provide some pointers to me so that I can proceed further. Thanks.
|
|
|
|
|
Hi all,
I have made a dialog based application in vc2008, i want to enable some keyboard shortcuts in my application like i have displayed a message box on button click event and if i press b that same dialog box should be displayed....
how can i do this???
Thanks in advance
|
|
|
|
|
To handle key pressings in dialogs, one should override PreTranslateMessage. It's MSG argument has the necessary information about pressed keys.
--
Arman
|
|
|
|
|
Add the ampersand [&] symbol before the desired letter in the button caption. For e.g for &Click, if you press Alt + C will run the code for Click button.
|
|
|
|
|
|
Hello,
I write the command to my device, the device responses immediately...
----
the code:
int begin;
While(1)
{
begin=GetTickCount();
WriteFile(...);
ReadFile(...);
TRACE(_T("The Tx-Rx Time is %d millisecond \r\n"), GetTickCount()-begin);
}
----
the output:
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 15 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 16 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 15 millisecond
------
It's good when spend 0 millisecond one loop...
But, as you can see above, every 3rd or 4th loop, the time spend is about 15 MS, it should be 0 MS, the ReadFile can't not get buffer data immediately...
Anyone met this problem before??
I need no delay in the loop...
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
|
|
|
|
|
That's simply because windows is not a real-time operating system, so, it is possible that inside your loop another thread becomes active (which means that your thread won't be processed for a while, ending up with the delays).
The only way to "fix the problem" (which is not a problem in fact) is to use a real-time operating system (e.g. windows CE).
|
|
|
|
|
Dear Cédric Moonen:
It seems you are right, but the delay is in ReadFile (I tested it)... to wait buffer coming. If you are right, the delay will be in WriteFile or ReadFile. The context switch doesn't spend 15 MS (too long).
Thanks for you reply
|
|
|
|
|
|
Dear Cédric Moonen:
Good job. You are right. I write a test program in window XP, it actually spends about 15 milliseconds when context-switch occurs.
Thank you very much!
------
UINT beginTicks,testData=0,MsTimePerLoop;
while(1)
{
beginTicks=GetTickCount();
for(int i=0;i<10000;i++)
testData= (testData+100)*20-300*20+20;
MsTimePerLoop=GetTickCount()-beginTicks;
if(MsTimePerLoop>=5)
{
printf("MsTimePerLoop=%d\n",MsTimePerLoop);
Sleep(1000);
}
else
printf("MsTimePerLoop=%d\r\n",MsTimePerLoop);
}
|
|
|
|
|
yctsai wrote: The context switch doesn't spend 15 MS (too long).
Maybe not, but the context switch PLUS the time slice given to a different process will easily take 15ms. Remember, you're not the only code running on your PC.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
It may be that your thread is preempted as Cédric suggested.
But, it may also be that your call to ::ReadFile() doesn't return immediately due to how you have set up your port and whether you're using overlapped I/O or not.
So; I have two questions:
- What values have you assigned the members of the
COMMTIMEOUTS struct when calling ::SetCommTimeouts() ?
This is essential since your call may block until it receives something on the port. - Are you using overlapped I/O?
My point is that the delay you're experiencing may be caused by the device connected to your port as well as any context switches.
I also suggest you have a look at this article[^] about serial communication.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
While you can do nothing about the 15ms delay, see if this article is of any help with serial ports in general.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hello everyone,
i am trying to compile my program where i am using the functions like sqrt pow and fabs. I do have math.h included but for some reason i get errors like:
error C2668: 'fabs' : ambiguous call to overloaded function
same for the rest of the functions
i have this included:
#include "stdafx.h"
#include "math.h"
i tried including <cmath> but still same errors.
Does anyone know why they are not being recognized?
my file is .cpp not .c but it is an MFC project.
thanks
|
|
|
|
|
It's because those functions are overloaded for several types: float, double and long double. Thus, if you pass in an integer, the compiler doesn't which one to choose. An easy fix is to simply pass a double (multiply what you pass in by 1.0), this should fix the problem.
int result = (int)fabs(value*1.0);
|
|
|
|
|
Cedric Moonen wrote: int result = (int)fabs(value*1.0);
Better could be
int result = (int)fabs((double)value);
Because it avoids a multiplication. Am I right?
- ns ami -
|
|
|
|
|
Yes you're right. It's just because I am used to multiply by 1.0 because in general I use that in calculation (where I don't want to put a cast inside because it looks ugly)
|
|
|
|
|
ns ami wrote: Am I right?
Only if the code were in a huge loop would the difference in time even be noticeable.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
it won't matter at all. the multiplication will be optimized away, and the two different calls will end with exactly the same object code.
|
|
|
|
|
nevertheless it's a bad style as many people won't understand it.
Don't try it, just do it!
|
|
|
|
|
the compiler will optimize that multiplication away.
int value = rand();
int result1 = (int)fabs(value*1.0);
printf("%d, %d\n", result1, result1);
VC6, release mode gives the following assembly:
; 10 : int value = rand();
call _rand
; 11 : int result1 = (int)fabs(value*1.0);
mov DWORD PTR -4+[esp+4], eax
fild DWORD PTR -4+[esp+4]
fabs
call __ftol
; 12 : printf("%d, %d\n", result1, result1);
push eax
push eax
push OFFSET FLAT:??_C@_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; `string'
call _printf
no fmul.
if you do this:
int value = rand();
int result1 = (int)fabs((double)value);
printf("%d, %d\n", result1, result1);
you get this:
; 10 : int value = rand();
call _rand
; 11 : int result1 = (int)fabs((double)value);
mov DWORD PTR -4+[esp+4], eax
fild DWORD PTR -4+[esp+4]
fabs
call __ftol
; 12 : printf("%d, %d\n", result1, result1);
push eax
push eax
push OFFSET FLAT:??_C@_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; `string'
call _printf
exactly the same.
|
|
|
|