From: jt on
Being a newbie to MFC, I am having problems on copying CString to a char*

Below is my attempt but I getting this error below:
=====================================================
Debug Assertion Failed!

Program: C:\geac\GEACstatusptt.exe
File: dbgheap.c
Line: 1044

Expression: _CrtIsValidHeapPointer(pUserData)

Below is my code the way that I am doing now, look at switch 4 and
this is where I call a DLL library "McaAddPageMember"
======================================================
char cPAGER_CODE[16];

CPsnstatuspttApp::m_LogTrace.WriteLine("CPsnstatuspttView::Call_Page_Member(
)");

CTokenizer tok( cmdstr,",");
CString cs;

id=0;
while(tok.Next(cs))
{
switch (id){

case 0: iPAGER_FORMAT = (WORD) _ttoi(cs); //Feature, paging format
break;
case 1: GEAC_PAGE_ID = cs;
break;
case 2: iRID = (WORD) _ttoi(cs); // rid
break;
case 3: iFREQ = (WORD)_ttoi(cs); //frequency
break;
case 4: wsprintf(cPAGER_CODE,"%s",cs.GetBuffer(cs.GetLength())); // pager
code
bNumDigits=cs.GetLength();
break;
}
id++;
}

pEntry = NULL;

pos = g_RCtable.GetHeadPosition();
while (pos)
{
_rid *pCurrent = g_RCtable.GetNext(pos);
if (pCurrent->RC_id == iRID){

if (iFREQ==0)
iFREQ=NULL_FREQ;

CPsnstatuspttApp::m_LogTrace.WriteLine("McaAddPageMember PAGER
CODE:%s",cPAGER_CODE);

if(McaAddPageMember(pCurrent->LC_id, iFREQ, iPAGER_FORMAT, bNumDigits,
cPAGER_CODE ) )
{
CPsnstatuspttApp::m_LogTrace.WriteLine(" SUCCESSFUL API Call
McaAddPageMember");

if ( McaBeginPaging () )
{
CPsnstatuspttApp::m_LogTrace.WriteLine("SUCCESSFUL API Call
McaBeginPaging");
sock.Send(msg.GetBuffer(msg.GetLength()), msg.GetLength());
}
else
{
CPsnstatuspttApp::m_LogTrace.WriteLine(" FAILED API Call
McaBeginPaging");
sock.Send(msg.GetBuffer(msg.GetLength()), msg.GetLength());
}

}
else
{
RetMsg="FAILED API Call McaAddPageMember()";
CPsnstatuspttApp::m_LogTrace.WriteLine(RetMsg);
sock.Send(msg.GetBuffer(msg.GetLength()), msg.GetLength());
}
break;
}
pEntry = pCurrent;
}


From: Uranium-235 on
Sorry for not sifting through all your code. Copying a CString to a char can
be as simple as the following.

strcpy(szDestination, (char *) (LPCSTR) strSourceString);

I hope this helps,


"jt" wrote:

> Being a newbie to MFC, I am having problems on copying CString to a char*
>
> Below is my attempt but I getting this error below:
> =====================================================
> Debug Assertion Failed!
>
> Program: C:\geac\GEACstatusptt.exe
> File: dbgheap.c
> Line: 1044
>
> Expression: _CrtIsValidHeapPointer(pUserData)
>
> Below is my code the way that I am doing now, look at switch 4 and
> this is where I call a DLL library "McaAddPageMember"
> ======================================================
> char cPAGER_CODE[16];
>
> CPsnstatuspttApp::m_LogTrace.WriteLine("CPsnstatuspttView::Call_Page_Member(
> )");
>
> CTokenizer tok( cmdstr,",");
> CString cs;
>
> id=0;
> while(tok.Next(cs))
> {
> switch (id){
>
> case 0: iPAGER_FORMAT = (WORD) _ttoi(cs); //Feature, paging format
> break;
> case 1: GEAC_PAGE_ID = cs;
> break;
> case 2: iRID = (WORD) _ttoi(cs); // rid
> break;
> case 3: iFREQ = (WORD)_ttoi(cs); //frequency
> break;
> case 4: wsprintf(cPAGER_CODE,"%s",cs.GetBuffer(cs.GetLength())); // pager
> code
> bNumDigits=cs.GetLength();
> break;
> }
> id++;
> }
>
> pEntry = NULL;
>
> pos = g_RCtable.GetHeadPosition();
> while (pos)
> {
> _rid *pCurrent = g_RCtable.GetNext(pos);
> if (pCurrent->RC_id == iRID){
>
> if (iFREQ==0)
> iFREQ=NULL_FREQ;
>
> CPsnstatuspttApp::m_LogTrace.WriteLine("McaAddPageMember PAGER
> CODE:%s",cPAGER_CODE);
>
> if(McaAddPageMember(pCurrent->LC_id, iFREQ, iPAGER_FORMAT, bNumDigits,
> cPAGER_CODE ) )
> {
> CPsnstatuspttApp::m_LogTrace.WriteLine(" SUCCESSFUL API Call
> McaAddPageMember");
>
> if ( McaBeginPaging () )
> {
> CPsnstatuspttApp::m_LogTrace.WriteLine("SUCCESSFUL API Call
> McaBeginPaging");
> sock.Send(msg.GetBuffer(msg.GetLength()), msg.GetLength());
> }
> else
> {
> CPsnstatuspttApp::m_LogTrace.WriteLine(" FAILED API Call
> McaBeginPaging");
> sock.Send(msg.GetBuffer(msg.GetLength()), msg.GetLength());
> }
>
> }
> else
> {
> RetMsg="FAILED API Call McaAddPageMember()";
> CPsnstatuspttApp::m_LogTrace.WriteLine(RetMsg);
> sock.Send(msg.GetBuffer(msg.GetLength()), msg.GetLength());
> }
> break;
> }
> pEntry = pCurrent;
> }
>
>
>
From: Tom Serface on
Try subtracting 1 from the length (remembering it is 0 based as an index).
You can also just cast the string (actually it does this on it's own most of
the time) so long as it is not going to be modified (LPCTSTR).

Tom

"jt" <jtsoft(a)hotmail.com> wrote in message
news:cKn_d.195801$JF2.189408(a)tornado.tampabay.rr.com...
> Being a newbie to MFC, I am having problems on copying CString to a char*
>
> Below is my attempt but I getting this error below:
> =====================================================
> Debug Assertion Failed!
>
> Program: C:\geac\GEACstatusptt.exe
> File: dbgheap.c
> Line: 1044
>
> Expression: _CrtIsValidHeapPointer(pUserData)
>
> Below is my code the way that I am doing now, look at switch 4 and
> this is where I call a DLL library "McaAddPageMember"
> ======================================================
> char cPAGER_CODE[16];
>
> CPsnstatuspttApp::m_LogTrace.WriteLine("CPsnstatuspttView::Call_Page_Member(
> )");
>
> CTokenizer tok( cmdstr,",");
> CString cs;
>
> id=0;
> while(tok.Next(cs))
> {
> switch (id){
>
> case 0: iPAGER_FORMAT = (WORD) _ttoi(cs); //Feature, paging format
> break;
> case 1: GEAC_PAGE_ID = cs;
> break;
> case 2: iRID = (WORD) _ttoi(cs); // rid
> break;
> case 3: iFREQ = (WORD)_ttoi(cs); //frequency
> break;
> case 4: wsprintf(cPAGER_CODE,"%s",cs.GetBuffer(cs.GetLength())); // pager
> code
> bNumDigits=cs.GetLength();
> break;
> }
> id++;
> }
>
> pEntry = NULL;
>
> pos = g_RCtable.GetHeadPosition();
> while (pos)
> {
> _rid *pCurrent = g_RCtable.GetNext(pos);
> if (pCurrent->RC_id == iRID){
>
> if (iFREQ==0)
> iFREQ=NULL_FREQ;
>
> CPsnstatuspttApp::m_LogTrace.WriteLine("McaAddPageMember PAGER
> CODE:%s",cPAGER_CODE);
>
> if(McaAddPageMember(pCurrent->LC_id, iFREQ, iPAGER_FORMAT, bNumDigits,
> cPAGER_CODE ) )
> {
> CPsnstatuspttApp::m_LogTrace.WriteLine(" SUCCESSFUL API Call
> McaAddPageMember");
>
> if ( McaBeginPaging () )
> {
> CPsnstatuspttApp::m_LogTrace.WriteLine("SUCCESSFUL API Call
> McaBeginPaging");
> sock.Send(msg.GetBuffer(msg.GetLength()), msg.GetLength());
> }
> else
> {
> CPsnstatuspttApp::m_LogTrace.WriteLine(" FAILED API Call
> McaBeginPaging");
> sock.Send(msg.GetBuffer(msg.GetLength()), msg.GetLength());
> }
>
> }
> else
> {
> RetMsg="FAILED API Call McaAddPageMember()";
> CPsnstatuspttApp::m_LogTrace.WriteLine(RetMsg);
> sock.Send(msg.GetBuffer(msg.GetLength()), msg.GetLength());
> }
> break;
> }
> pEntry = pCurrent;
> }
>
>


From: Craig S. on
A call to CString::GetBuffer() has to be followed by
CString::ReleaseBuffer() before you do anything else with the CString
object.


From: Tom Serface on
Hi Craig,

I think you only need to do this if you actually modify the string and need
to resync it. So far as I know you don't have to do this if you pass the
string as a const. That's why CString can do the automatic cast to LPCTSTR.
However, that said, I don't think it hurts either so long as you don't
change the length of the string...

Tom

"Craig S." <xxxx(a)ms.com> wrote in message
news:u65B%2322KFHA.1280(a)TK2MSFTNGP09.phx.gbl...
>A call to CString::GetBuffer() has to be followed by
>CString::ReleaseBuffer() before you do anything else with the CString
>object.
>