PC Review


Reply
Thread Tools Rate Thread

Unmanage string convert into manage string

 
 
=?Utf-8?B?amVmZg==?=
Guest
Posts: n/a
 
      6th Mar 2007
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


 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      6th Mar 2007
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" <(E-Mail Removed)> wrote in message
news:2B5EB157-0C5D-49C0-BC09-(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
=?Utf-8?B?amVmZg==?=
Guest
Posts: n/a
 
      6th Mar 2007

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.



"<ctacke/>" wrote:

> 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" <(E-Mail Removed)> wrote in message
> news:2B5EB157-0C5D-49C0-BC09-(E-Mail Removed)...
> > 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
> >
> >

>
>
>

 
Reply With Quote
 
Guest
Posts: n/a
 
      6th Mar 2007
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" <(E-Mail Removed)> wrote in message
news:E161A2AD-A7E7-4113-A0BE-(E-Mail Removed)...
>
> 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.
>
>
>
> "<ctacke/>" wrote:
>
>> 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" <(E-Mail Removed)> wrote in message
>> news:2B5EB157-0C5D-49C0-BC09-(E-Mail Removed)...
>> > 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
>> >
>> >

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?amVmZg==?=
Guest
Posts: n/a
 
      7th Mar 2007
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



"<ctacke/>" wrote:

> 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" <(E-Mail Removed)> wrote in message
> news:E161A2AD-A7E7-4113-A0BE-(E-Mail Removed)...
> >
> > 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.
> >
> >
> >
> > "<ctacke/>" wrote:
> >
> >> 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" <(E-Mail Removed)> wrote in message
> >> news:2B5EB157-0C5D-49C0-BC09-(E-Mail Removed)...
> >> > 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
> >> >
> >> >
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)' to '1-dimensional array of String'. roidy Microsoft VB .NET 12 17th Jul 2009 11:53 AM
Convert Dictionary<string,SomeType> keys to List<string> buzzweetman@gmail.com Microsoft C# .NET 6 9th Aug 2006 04:54 PM
convert html-string to plain text-string Nedo Microsoft Dot NET 4 28th Jul 2005 10:53 AM
Connection String object Convert to String Variable Type =?Utf-8?B?TWlrZSBNb29yZQ==?= Microsoft ASP .NET 2 26th Oct 2004 04:43 PM
Convert a string to Ascii codes and then back to string again Kai Bohli Microsoft C# .NET 11 8th Jul 2004 04:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:34 AM.