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(); 12 3226
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
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
"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?
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
"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));
}
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
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
"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?
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
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
"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.
Thanx to those who helped in this thread.
I yelled Uncle!
I've used StreamWriter from the System::IO namespace to eliminate the
problems. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: tarmat |
last post by:
sorry for this silly little question, but whats the function to grab
the sign of a value?
|
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:
...
|
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...
|
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,...
|
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: ...
|
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...
|
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)...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |