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;
}
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
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 have a c++ program that I'm trying to port from VS98 to VS2003 (incremental steps). One error that occurs throughout is "Error 2275"
For instance: k:\RR\chart\chartdlg.cpp(2025): error C2475: 'CRrDoc::cFldFilter' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
The offending code is shown below:
void CDataPage::OnBtnLabelField()
{
FLDID fid ;
LPMFFIELD f ;
CRrApp *pApp = (CRrApp *)AfxGetApp();
CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
CRrDoc *pDoc = (CRrDoc *)pFrame->GetActiveDocument();
CSelectFieldDlg dlg;
//**************************************************
//BOOL CRrDoc::*zcFldFilter = &CRrDoc::cFldFilter;
//dlg.ck = CRrDoc->*zcFldFilter;
//**************************************************
dlg.ck = pDoc->cFldFilter ;
dlg.TitleTextID = IDS_2676;
fid = (FLDID)dlg.DoModal();
if (fid != NOID)
{
f = pDoc->m_pComposite->mfbyndx(fid);
// find index
int i, iCount;
iCount = m_lboxLabel.GetCount();
for (i = 0; i < iCount; i++)
{
if(fid == m_lboxLabel.GetItemData(i))
{
m_lboxLabel.SetCurSel(i);
OnSelchangeComboLabel();
}
}
}
}
I tried handling it according to a Microsoft page: But that just generated a set of other problems (the commented code between the asterisks). Note that I also commented out the following line:
dlg.ck = pDoc->cFldFilter
Unfortunately, this leads to a new error: k:\RR\chart\chartdlg.cpp(2022): error C2440: 'initializing' : cannot convert from 'BOOL (__cdecl )(LPMFFIELD)' to 'BOOL CRrDoc:: '
The definition in the .H file looks like:
public:
static BOOL cFldFilter(LPMFFIELD f);
Any ideas how to handle the pointer-to-member issue?
since you have:
static BOOL CRrDoc::cFldFilter(LPMFFIELD f);
its type is not a member variable but a function:
//BOOL CRrDoc::*zcFldFilter = &CRrDoc::cFldFilter; // doesn't work
BOOL (*zcFldFilter)(LPMFFIELD) = &CRrDoc::cFldFilter; // works
Since dlg.ck is of a correct type, you should do
dlg.ck = &CRrDoc::cFldFilter;
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);