Unmanage string convert into manage string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi Guys,

I have read a string from Unmanaged code, however, when I convert ptr to
string.
It also include "\r\n" in the string, how to avoid this issue:

here is my source code:
IntPtr m_pMessge = new IntPtr(0);

// this call native code [dll file]
ReplayForm(ref m_pMessge,this.E_User,this.E_Password);
//convert pointer to object string
string strMessage = Marshal.PtrToStringBSTR(m_pMessge);
/*****
after it has been converted, it holds \r\n ""
*/
strMessage = strMessage.Replace(@"=\r\n", @"\r\n");

Thanks
 
here is some code:

DLLEXPORT bool ReplayForm(BSTR* bt,LPTSTR szUser,LPTSTR szPassword)
{
CLogger::Record(L"ReplayForm");
if(!SyncService(szUser,szPassword))
return false;
int i = 0;
while(!TrackMessage(bt))
{
i++;
if(!SyncService(szUser,szPassword))
break;
Sleep(5000);
if(i > 120)
break;
};
return true;
}

bool TrackMessage(BSTR* bt)
{
//here I have not put my actual code, since it is too complicated.
//in fact, save all the string to CString instance, and then
CString strMessage ="some long string which hold return";
*bt = ::SysAllocString(strMessage.GetBuffer(strMessage.GetLength()));
return true;
}

Thanks.



I'd want to see the native code that it calls.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--




jeff said:
Hi Guys,

I have read a string from Unmanaged code, however, when I convert ptr to
string.
It also include "\r\n" in the string, how to avoid this issue:

here is my source code:
IntPtr m_pMessge = new IntPtr(0);

// this call native code [dll file]
ReplayForm(ref m_pMessge,this.E_User,this.E_Password);
//convert pointer to object string
string strMessage = Marshal.PtrToStringBSTR(m_pMessge);
/*****
after it has been converted, it holds \r\n ""
*/
strMessage = strMessage.Replace(@"=\r\n", @"\r\n");

Thanks
 
All I can say is to check that your TrackMessage isn't putting in newlines
or returns. The system surely doesn't instert them automatically.

And where exactly is the SysFreeString mate to your SysAllocString?
Furthermore why use a BSTR anyway? Are you also interoping with COM? If
not, get rid of it.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--




jeff said:
here is some code:

DLLEXPORT bool ReplayForm(BSTR* bt,LPTSTR szUser,LPTSTR szPassword)
{
CLogger::Record(L"ReplayForm");
if(!SyncService(szUser,szPassword))
return false;
int i = 0;
while(!TrackMessage(bt))
{
i++;
if(!SyncService(szUser,szPassword))
break;
Sleep(5000);
if(i > 120)
break;
};
return true;
}

bool TrackMessage(BSTR* bt)
{
//here I have not put my actual code, since it is too complicated.
//in fact, save all the string to CString instance, and then
CString strMessage ="some long string which hold return";
*bt = ::SysAllocString(strMessage.GetBuffer(strMessage.GetLength()));
return true;
}

Thanks.



I'd want to see the native code that it calls.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--




jeff said:
Hi Guys,

I have read a string from Unmanaged code, however, when I convert ptr
to
string.
It also include "\r\n" in the string, how to avoid this issue:

here is my source code:
IntPtr m_pMessge = new IntPtr(0);

// this call native code [dll file]
ReplayForm(ref m_pMessge,this.E_User,this.E_Password);
//convert pointer to object string
string strMessage = Marshal.PtrToStringBSTR(m_pMessge);
/*****
after it has been converted, it holds \r\n ""
*/
strMessage = strMessage.Replace(@"=\r\n", @"\r\n");

Thanks
 
Hi Chris,

Thanks for your kind help.

However, Since I am pretty new to NET and C++;

Would you mind telling how to read native code CString into NET string?

because I read some message via CEMAPI to get Mail body.
and pass them to NET managment code.
when reading message from email, it holds some "returnkey"!
and other specifically character, so I think when I Allocate to heap.
It read one byte by byte. so the there are something is wrong.

Thanks



All I can say is to check that your TrackMessage isn't putting in newlines
or returns. The system surely doesn't instert them automatically.

And where exactly is the SysFreeString mate to your SysAllocString?
Furthermore why use a BSTR anyway? Are you also interoping with COM? If
not, get rid of it.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--




jeff said:
here is some code:

DLLEXPORT bool ReplayForm(BSTR* bt,LPTSTR szUser,LPTSTR szPassword)
{
CLogger::Record(L"ReplayForm");
if(!SyncService(szUser,szPassword))
return false;
int i = 0;
while(!TrackMessage(bt))
{
i++;
if(!SyncService(szUser,szPassword))
break;
Sleep(5000);
if(i > 120)
break;
};
return true;
}

bool TrackMessage(BSTR* bt)
{
//here I have not put my actual code, since it is too complicated.
//in fact, save all the string to CString instance, and then
CString strMessage ="some long string which hold return";
*bt = ::SysAllocString(strMessage.GetBuffer(strMessage.GetLength()));
return true;
}

Thanks.



I'd want to see the native code that it calls.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--




Hi Guys,

I have read a string from Unmanaged code, however, when I convert ptr
to
string.
It also include "\r\n" in the string, how to avoid this issue:

here is my source code:
IntPtr m_pMessge = new IntPtr(0);

// this call native code [dll file]
ReplayForm(ref m_pMessge,this.E_User,this.E_Password);
//convert pointer to object string
string strMessage = Marshal.PtrToStringBSTR(m_pMessge);
/*****
after it has been converted, it holds \r\n ""
*/
strMessage = strMessage.Replace(@"=\r\n", @"\r\n");

Thanks
 
Back
Top