473,372 Members | 878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Forcing value to bool (performance warning)

In the code below, ALL the lines that start with PPTfile << are getting the
following error at build time.

"//i:\C++\C++Code\FileOperations\Form1.h(127) : warning C4800:
'System::String __gc *' : forcing value to bool 'true' or 'false'
(performance warning)"

I get the following output, which is obviously wrong:

Process ID = 0x1
ref count (c) = 0x1
Thread Count = 1
parent process ID = 0x1
Priority Base = 1
Process ID = 0x1
ref count (c) = 0x1
Thread Count = 1
Process ID = 0x1
Priority Base = 1
1
1
1
1
1
-------------------------------------------------
DWORD x = 23;
LONG aa = 16;

String *buf;
ofstream PPTfile("PPT.txt", ios::out);
PPTfile << " Process ID = 0x" << x.ToString("X8") << endl;
PPTfile << " ref count (c) = 0x" << x.ToString("X4") << endl;
PPTfile << " Thread Count = " << x.ToString() << endl;
PPTfile << " parent process ID = 0x" << x.ToString("X8") << endl;
PPTfile << " Priority Base = " << aa.ToString() << endl;

buf = x.ToString("X8");
PPTfile << " Process ID = 0x" << buf << endl;
buf = x.ToString("X4");
PPTfile << " ref count (c) = 0x" << buf << endl;
buf = x.ToString();
PPTfile << " Thread Count = " << buf << endl;
buf = x.ToString("X8");
PPTfile << " Process ID = 0x" << buf << endl;
buf = aa.ToString();
PPTfile << " Priority Base = " << buf << endl;

buf = String::Concat(S" Process ID = 0x", x.ToString("X8"));
PPTfile << buf << endl;
buf = String::Concat(S" ref count (c) = 0x", x.ToString("X4"));
PPTfile << buf << endl;
buf=String::Concat(S" Thread Count = ", x.ToString());
PPTfile << buf << endl;
buf=String::Concat(S" parent process ID = 0x", x.ToString("X8"));
PPTfile << buf << endl;
buf =String::Concat(S" Priority Base = ", aa.ToString());
PPTfile << buf << endl;
PPTfile.close();
Nov 17 '05 #1
12 3224
Howard Kaikow wrote:
In the code below, ALL the lines that start with PPTfile << are getting the
following error at build time.

"//i:\C++\C++Code\FileOperations\Form1.h(127) : warning C4800:
'System::String __gc *' : forcing value to bool 'true' or 'false'
(performance warning)"

I get the following output, which is obviously wrong:


ToString returns a .NET object handle, which native C++ can't handle,
and therefore it tries to convert it implicitly to bool. Thus the
warning. You have to use the following function (as an example) to
convert managed strings to unmanaged ones:

std::wstring ToUnmanagedString(System::String* str)
{
const wchar_t __pin* c_str = PtrToStringChars(str);
return std::wstring(c_str);
}

In addition, you shouldn't use endl, you should use "\n" instead. endl
flushes the stream, which is a serious performance penalty.

Tom
Nov 17 '05 #2
Thanx.

--
http://www.standards.com/; See Howard Kaikow's web site.
"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
Howard Kaikow wrote:
In the code below, ALL the lines that start with PPTfile << are getting the following error at build time.

"//i:\C++\C++Code\FileOperations\Form1.h(127) : warning C4800:
'System::String __gc *' : forcing value to bool 'true' or 'false'
(performance warning)"

I get the following output, which is obviously wrong:


ToString returns a .NET object handle, which native C++ can't handle,
and therefore it tries to convert it implicitly to bool. Thus the
warning. You have to use the following function (as an example) to
convert managed strings to unmanaged ones:

std::wstring ToUnmanagedString(System::String* str)
{
const wchar_t __pin* c_str = PtrToStringChars(str);
return std::wstring(c_str);
}

In addition, you shouldn't use endl, you should use "\n" instead. endl
flushes the stream, which is a serious performance penalty.

Tom

Nov 17 '05 #3
"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
ToString returns a .NET object handle, which native C++ can't handle,
and therefore it tries to convert it implicitly to bool. Thus the
warning. You have to use the following function (as an example) to
convert managed strings to unmanaged ones:

std::wstring ToUnmanagedString(System::String* str)
{
const wchar_t __pin* c_str = PtrToStringChars(str);
return std::wstring(c_str);
}


Using the above, I got the error:

i:\C++\C++Code\FileOperations\Form1.h(101): error C2230: 'ToUnmanagedString'
: a member function of a managed class cannot return a non-managed class or
struct 'std::basic_string<_Elem,_Traits,_Ax>' unless it is an aggregate
with
[
_Elem=wchar_t,
_Traits=std::char_traits<wchar_t>,
_Ax=std::allocator<wchar_t>
]

What does that mean?
Nov 17 '05 #4
Howard Kaikow wrote:
"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
ToString returns a .NET object handle, which native C++ can't handle,
and therefore it tries to convert it implicitly to bool. Thus the
warning. You have to use the following function (as an example) to
convert managed strings to unmanaged ones:

std::wstring ToUnmanagedString(System::String* str)
{
const wchar_t __pin* c_str = PtrToStringChars(str);
return std::wstring(c_str);
}

Using the above, I got the error:

i:\C++\C++Code\FileOperations\Form1.h(101): error C2230: 'ToUnmanagedString'
: a member function of a managed class cannot return a non-managed class or
struct 'std::basic_string<_Elem,_Traits,_Ax>' unless it is an aggregate
with
[
_Elem=wchar_t,
_Traits=std::char_traits<wchar_t>,
_Ax=std::allocator<wchar_t>
]

What does that mean?


The supplied function should not be a member function - make it a global
or namespace scope function.

Tom
Nov 17 '05 #5
"Tom Widmer" <to********@hotmail.com> wrote in message
news:OA**************@TK2MSFTNGP14.phx.gbl...
The supplied function should not be a member function - make it a global
or namespace scope function.


Thanx.

Now, I'm getting the error

i:\C++\C++Code\FileOperations\Form1.h(162): error C2679: binary '<<' : no
operator found which takes a right-hand operand of type 'std::wstring' (or
there is no acceptable conversion)

in the following code:

DWORD x = 23;
String *buf;
ofstream PPTfile("PPT.txt", ios::out);

buf = x.ToString("X8");
PPTfile << " Process ID = 0x" << ToUnmanagedString(buf)<< "\n";

buf = String::Concat(S" Process ID = 0x", x.ToString("X8"));
PPTfile << ToUnmanagedString(buf) << "\n";

PPTfile.close();

I included the following in stdafx,h

#include <vcclr.h>

and I included the folowing at the top of the namespace, right after
using namespace System::Drawing;

std::wstring ToUnmanagedString(System::String* str)
{
const wchar_t __pin* c_str = PtrToStringChars(str);
return std::wstring(c_str);
//return std::wstring((wchar_t __pin*) PtrToStringChars(str));
}
Nov 17 '05 #6
I stumbled upon MSFT KB artice 311259, which suggests 3 solutions, including
the one discussed in this thread.
Alas, none of the 3 solutions appear to solve the problem.

The solutions listed are:

PtrToStringChars, as discussed in this thread.
StringToHGlobalAnsi
CString
Nov 17 '05 #7
Howard Kaikow wrote:
"Tom Widmer" <to********@hotmail.com> wrote in message
news:OA**************@TK2MSFTNGP14.phx.gbl...
The supplied function should not be a member function - make it a
global or namespace scope function.


Thanx.

Now, I'm getting the error

i:\C++\C++Code\FileOperations\Form1.h(162): error C2679: binary '<<'
: no operator found which takes a right-hand operand of type
'std::wstring' (or there is no acceptable conversion)


You're using a wide-character string (std::wstring) with a narrow-character
stream (std::ofstream). Unfortunately, that doesn't work.

What you really want is to use a wide-character stream (std::wofstream) with
a narrowing facet (assuming you want ASCII/ANSI text in your outpout file
and not Unicode). Unfortunately, VC++ does not supply a suitable facet
(although the Dinkumware Unabridged library does include one).

What you can do instead is use the .NET framework to do the narrowing
conversion for you:

<untested code>
std::string ToUnmanagedNarrowString(System::String* str)
{
HGLOBAL hg =
(HGLOBAL)System::Runtime::InteropServices::Marshal ::StringToHGlobalAnsi(str);
const char* pc = static_cast<const char*>GlobalLock(hg);
std::string ret(pc,pc+str->get_Length());
GlobalFree(hg);
return ret;
}
</untested code>

Note that if Unicode output is acceptable (for example, that ofstream
actually goes to the console), simply changing your ofstream to a wofstream
should be sufficient.

-cd
Nov 17 '05 #8
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:up****************@TK2MSFTNGP14.phx.gbl...
You're using a wide-character string (std::wstring) with a narrow-character stream (std::ofstream). Unfortunately, that doesn't work.

What you really want is to use a wide-character stream (std::wofstream) with a narrowing facet (assuming you want ASCII/ANSI text in your outpout file
and not Unicode). Unfortunately, VC++ does not supply a suitable facet
(although the Dinkumware Unabridged library does include one).

What you can do instead is use the .NET framework to do the narrowing
conversion for you:

<untested code>
std::string ToUnmanagedNarrowString(System::String* str)
{
HGLOBAL hg =
(HGLOBAL)System::Runtime::InteropServices::Marshal ::StringToHGlobalAnsi(str)
; const char* pc = static_cast<const char*>GlobalLock(hg);
std::string ret(pc,pc+str->get_Length());
GlobalFree(hg);
return ret;
}
</untested code>

Note that if Unicode output is acceptable (for example, that ofstream
actually goes to the console), simply changing your ofstream to a wofstream should be sufficient.


I an writing to a file.

I already display the info in a listbox, I'm just trying to get the stuff
from the listbox into a file, so I can print the file.
Perhaps there's a better way to do that?
Nov 17 '05 #9
Carl Daniel [VC++ MVP] wrote:
<untested code>
std::string ToUnmanagedNarrowString(System::String* str)
{
HGLOBAL hg =
(HGLOBAL)System::Runtime::InteropServices::Marshal ::StringToHGlobalAnsi(str);
const char* pc = static_cast<const char*>GlobalLock(hg);
const char* pc = static_cast<const char*>(GlobalLock(hg));
std::string ret(pc,pc+str->get_Length());
GlobalFree(hg);
return ret;
}
</untested code>


-cd
Nov 17 '05 #10
Howard Kaikow wrote:
"Carl Daniel [VC++ MVP]"
Note that if Unicode output is acceptable (for example, that ofstream
actually goes to the console), simply changing your ofstream to a

wofstream
should be sufficient.


I an writing to a file.

I already display the info in a listbox, I'm just trying to get the
stuff from the listbox into a file, so I can print the file.
Perhaps there's a better way to do that?


No, not really. If you're using .NET for your UI (and hence Unicode) and
youi want to write out a narrow-character file, then you have to explicitly
do the narrowing conversion somewhere.

-cd
Nov 17 '05 #11
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:ul**************@TK2MSFTNGP15.phx.gbl...
No, not really. If you're using .NET for your UI (and hence Unicode) and
youi want to write out a narrow-character file, then you have to explicitly do the narrowing conversion somewhere.


I have no problem using the same expressions when adding to a LIstBox
control.
So, I expect that I should byte the bullet and use the System::IO
namespace.
Nov 17 '05 #12
Thanx to those who helped in this thread.

I yelled Uncle!
I've used StreamWriter from the System::IO namespace to eliminate the
problems.
Nov 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
12
by: tarmat | last post by:
sorry for this silly little question, but whats the function to grab the sign of a value?
9
by: Java script Dude | last post by:
In many languages, it is necessary to string together multiple strings into one string for use over multiple lines of code. Which one is the most efficient from the interpreters perspective: ...
40
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what...
8
by: Marc | last post by:
Hi! I'm calling a web service using C# and a wrapper class generated by wsdl tool. The web service specification contains some nillable parameters which types are Value Types in .NET (long, int,...
2
by: tim | last post by:
We've started to use a coding standards checker at work. The following code results in a warning: 38: CPlainText::CPlainText(const char *szPath,bool bTimeStamp,bool bSaveLast) 39: ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
4
by: Macneed | last post by:
i am a newbie, i remember i read a book talking about when u declare a array variable using float ABC = new float; the whole array element in ABC ( ABC to ABC ) will automatic initialize to 0...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.