CString str = "something";
CDaoDatabase db;
//db open
CDaoRecordset rs(&db);
rs.Open(AFX_DAO_USE_DEFAULT_TYPE,"select * from mydb");
COleVariant searched(str);
BOOL bFound = rs.Seek("=",searched);
For this statement VS2008 returns error C2664: 'BOOL CDaoRecordset::Seek(LPCTSTR,COleVariant *,COleVariant *,COleVariant *)' : cannot convert parameter 2 from 'COleVariant' to 'COleVariant *'.
How can i convert COleVariant to a pointer?
Use the address operator '&'
BOOL bFound = rs.Seek("=",&searched);
Related
I am getting the below error when compiling the C++ project.
Error C2664 'BOOL CryptBinaryToStringW(const BYTE *,DWORD,DWORD,LPWSTR,DWORD *)': cannot convert argument 4 from 'std::unique_ptr>' to 'LPWSTR'
at the below line of code:
CryptBinaryToString(reinterpret_cast<const BYTE*>(strData.c_str()), dwSize,
dwOptions, pwszBuffer, &dwLength);
And also I am getting the below error:
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'std::unique_ptr>' (or there is no acceptable conversion)
at the below line:
sBase64 = pwszBuffer;
Below is the complete code:
bool EMRReader::EncodeBase64(DWORD dwSize, const std::string& strData, wstring& sBase64)
{
DWORD dwOptions = CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF;
DWORD dwLength = 0;
BOOL bRet = CryptBinaryToString(reinterpret_cast<const BYTE*>(strData.c_str()), dwSize,
dwOptions, 0, &dwLength);
if (!bRet)
return bRet;
std::unique_ptr<std::wstring> pwszBuffer = std::make_unique<std::wstring>(dwLength + 1);
if (!pwszBuffer)
return FALSE;
SecureZeroMemory(pwszBuffer.get(), (dwLength + 1) * sizeof(wchar_t));
CryptBinaryToString(reinterpret_cast<const BYTE*>(strData.c_str()), dwSize,
dwOptions, pwszBuffer, &dwLength);
sBase64 = pwszBuffer;
return TRUE;
}
Could anyone please help me to resolve these errors?
You're assigning a std::unique_ptr<wstring> object to a variable of type wstring that is not allowed. If you want to assign the value of pwszBuffer to a variable of type wstring, you should get the unique_ptr's value and then assign it to the variable.
You can get the value of std::unique_ptr by calling its * operator:
sBase64 = *pwszBuffer;
Edit: If you want to pass a std::unique_ptr to a function, you have two ways:
Pass it by reference:
void func(std::unique_ptr<std::wstring>& input_ptr) {
// Do something...
}
and then use it simply:
std::unique_ptr<std::wstring> function_input;
func(function_input);
Or 2. If you want to pass it by value, move it:
void func(std::unique_ptr<std::wstring>&
input_ptr) {
// Do something...
}
and then pass it with std::move:
std::unique_ptr<std::wstring> function_input;
func(std::move(function_input));
You should be aware that in this case after moving the function_input, It owns nothing and holds a nullptr and you shouldn't use it out of the func.
related
Hello I'm using Visual Studio c++ 2010
I'm having a problem with this code ( it's taken from C language code ) :
MEMBLOCK* create_memblock (HANDLE hProc, MEMORY_BASIC_INFORMATION *meminfo)
{
MEMBLOCK *mb = malloc(sizeof(MEMBLOCK));
if (mb)
{
mb->hProc = hProc;
mb->addr = meminfo->BaseAddress;
mb->size = meminfo->RegionSize;
mb->buffer = malloc(meminfo->RegionSize);
mb->next = NULL;
}
return mb;
}
I'm having these errors :
error C2440: 'initializing' : cannot convert from 'void *' to 'MEMBLOCK *'
error C2440: '=' : cannot convert from 'PVOID' to 'unsigned char *'
error C2440: '=' : cannot convert from 'void *' to 'unsigned char *'
I'm kinda newbie. Can you please provide a converted code for this that actually works with c++.
Thank you
Since you're programming in C++, you should not use the old C function malloc. Instead I would recommend that you use the C++ new construct:
MEMBLOCK *mb = new MEMBLOCK;
In C++ You may not assign a pointer of type void * to a pointer of some other type. So for example instead of writing
MEMBLOCK *mb = malloc(sizeof(MEMBLOCK));
You have to write
MEMBLOCK *mb = ( MEMBLOCK * )malloc(sizeof(MEMBLOCK));
Also you have to change other statements where there is the same problem. It seems these statements are
mb->addr = ( unsigned char * )meminfo->BaseAddress;
mb->buffer = ( unsigned char * )malloc(meminfo->RegionSize);
It is a good example of that you always should use an explicit casting even in C. That makes the code more safe and clear.
malloc() returns void*, and C++ does not automatically cast void* to a different pointer type. So you have to cast the return value:
MEMBLOCK *mb = (MEMBLOCK*) malloc(sizeof(MEMBLOCK));
Try:
MEMBLOCK* create_memblock (HANDLE hProc, MEMORY_BASIC_INFORMATION *meminfo)
{
MEMBLOCK *mb = (MEMBLOCK*)malloc(sizeof(MEMBLOCK));
if (mb)
{
mb->hProc = hProc;
mb->addr = meminfo->BaseAddress;
mb->size = meminfo->RegionSize;
mb->buffer = malloc(meminfo->RegionSize);
mb->next = NULL;
}
return mb;
}
How to convert this way properly?
VARIANT varIndex;
CString csIndex;
//Index BSTR
csIndex = (LPCSTR)(_bstr_t)vtIndex;
csIndex.MakeUpper();
if (csIndex.Left(3) == PROCESSUS_TABLE)
{
lIndex = atoi((LPCSTR)csIndex.Mid(3));
if ((unsigned long)lIndex<0)
return E_INVALIDARG;
}
Error message:
C2664: 'int atoi(const char *)' : cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *'
I couldn't find how to fix this, any good idea, please?
The variable 'csIndex' is a unicoce string(wchar_t), while the macro LPCSTR is for the ansi string (char).
So you should use Unicode functions, the code will be:
lIndex = _wtoi((LPCWSTR)csIndex.Mid(3));
There's no problem with this line:
csIndex = (LPCSTR)(_bstr_t)vtIndex;
It is because that the smart pointer type _bstr_t can handle char*/wchar_t* conversion automatically.
CComVariant::ChangeType makes it available to you through .bstrVal member, _ttoi instead of atoi:
VARIANT vtIndex;
// ...
CComVariant vStringIndex;
HRESULT nResult = vStringIndex.ChangeType(VT_BSTR, &vtIndex);
if(FAILED(nResult))
; // TODO: Handle error
CString csIndex(vStringIndex.bstrVal);
csIndex.MakeUpper();
if (csIndex.Left(3) == PROCESSUS_TABLE)
{
lIndex = _ttoi(csIndex.Mid(3));
// ...
I have a piece of code which worked fine on a previous projet. A part of it was even copied from a working demo project and I don't know how I can have this error on a new project now.
When compiling, I have the following error :
1>d:\visual studio 2012\netsdk_poc\mfc_netsdk2\mfc_netsdk2\netsdkfunctions.cpp(33): error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'fDisConnect'
The code :
class CNetSDKFunctions{
void __stdcall DisConnectBackCallFunc(LONG lLoginID, char *pchDVRIP,
LONG nDVRPort, DWORD dwUser)
{
CNetSDKFunctions* pThis = (CNetSDKFunctions*)dwUser;
if (pThis == NULL)
{
ASSERT( FALSE );
return ;
}
//pThis->ReConnect(lLoginID, pchDVRIP, nDVRPort);
}
BOOL CNetSDKFunctions::InitSDK()
{
long m_PlayerHandle;
H264_DVR_GetLastError();
H264_DVR_Init(NULL, 0);
//Here it is :
BOOL logResult = H264_DVR_Init( (fDisConnect) DisConnectBackCallFunc, (DWORD) this );
....
}
And fDisConnect definition :
typedef void (CALL_METHOD *fDisConnect)(long lLoginID, char *pchDVRIP, long nDVRPort, unsigned long dwUser);
I am using the MArgList class from the Maya API to retrieve arguments entered in the Maya command line. According to the class reference MArgList::get should be able to take an int or double as its second argument but it seems to be expecting a bool only and so throws a conversion error during compiling. The following is the code section and the errors generated. Any thoughts on what might be causing this would be much appreciated. The code was typed straight out of a tutorial on Maya plugin development, so it is a mystery why it is not working.
const int nPosts = 5;
const double radius = 0.5;
const double height = 5.0;
unsigned index;
index = args.flagIndex( "n", "number" );
if( MArgList::kInvalidArgIndex != index )
args.get( index + 1, nPosts );
unsigned index;
index = args.flagIndex( "r", "radius" );
if( MArgList::kInvalidArgIndex != index )
args.get( index + 1, radius );
unsigned index;
index = args.flagIndex( "h", "height" );
if( MArgList::kInvalidArgIndex != index )
args.get( index + 1, height );
1>Posts1Cmd.cpp(37): error C2664: 'MStatus MArgList::get(unsigned int,bool &) const' : cannot convert parameter 2 from 'const int' to 'bool &'
1>Posts1Cmd.cpp(39): error C2086: 'unsigned int index' : redefinition
1> Posts1Cmd.cpp(34) : see declaration of 'index'
1>Posts1Cmd.cpp(42): error C2664: 'MStatus MArgList::get(unsigned int,bool &) const' : cannot convert parameter 2 from 'const double' to 'bool &'
1>Posts1Cmd.cpp(44): error C2086: 'unsigned int index' : redefinition
1> Posts1Cmd.cpp(34) : see declaration of 'index'
1>Posts1Cmd.cpp(47): error C2664: 'MStatus MArgList::get(unsigned int,bool &) const' : cannot convert parameter 2 from 'const double' to 'bool &'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If you are going to get new values from the get function, you cannot have the target variables const.
Try
int nPosts = 5;
double radius = 0.5;
double height = 5.0;
Also, you should not declare a new index variable for each call. Just declare it once and reuse it.