|
How can I write my own stream?
I need to write a stream class and redirect cerr to it. Based on the class settings I will log it to a file or a database.
I need to use the class like below
#include <iostream>
#include <fstream>
int main()
{
std::ofstream logFile("out.txt");
std::streambuf *outbuf = std::cout.rdbuf(logFile.rdbuf());
std::streambuf *errbuf = std::cerr.rdbuf(logFile.rdbuf());
// do the actual work of the program;
// GUI code and event loop would go here
std::cout << "This would normally go to cout but goes to the log file\n";
std::cerr << "This would normally go to cerr but goes to the log file \n";
logFile << "This goes to the log file\n";
// end of program body
// restore the buffers
std::cout.rdbuf(outbuf);
std::cerr.rdbuf(errbuf);
}
But my class will log to the disk or log to a DBMS.
Regards,
Xavier
|
|
|
|
|
Here's one I wrote that uses WM_COPYDATA to send a line when it finds a \r
template <class chart,="" class="" traits="">
class DebugStr : public std::basic_streambuf<chart, traits="">
{
public:
DebugStr()
{
m_ParenthWnd = m_DebughWnd = NULL;
};
HWND m_ParenthWnd, m_DebughWnd;
protected:
std::string m_string;
int_type overflow(int_type c = traits_type::eof())
{
if (c != traits_type::eof())
{
if ('\n' == c)
{
// We check to make sure we have the HWND and it's still valid.
if (m_DebughWnd && ::IsWindow(m_DebughWnd))
{
COPYDATASTRUCT cds;
cds.cbData = m_string.size();
cds.lpData = (void*)(LPCSTR)m_string.c_str();
::SendMessage(m_DebughWnd, WM_COPYDATA, (WPARAM)m_ParenthWnd, (LPARAM)&cds);
}
m_string = "";
}
else
{
m_string += traits_type::to_char_type(c);
// If we have the parent window, but not the debug window, or it's become invalid,
// we call Init again and attempt tp find it.
if (m_ParenthWnd && (!m_DebughWnd || !::IsWindow(m_DebughWnd)))
{
SDebug::Init(m_ParenthWnd);
}
}
return c;
}
return traits_type::not_eof(c);
};
};
Through the details of my implimentation you should find a skeleton there you can use to create a stream.
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
I wanna play multiple AVIs,but a interval of silence occured when file1 finished and file2 is rendering.How can i avoid that ?
Any Pointer or hint will be appreciated !
Thanks a lot!!
|
|
|
|
|
Hi,
I am embedding MSWord In my application, using OLE automation.
Code As Follows
/*******************************************************************
This method encapsulates the process of embedding a Word document
in a View object and automating that document to add text.
*******************************************************************/
//Change the cursor so the user knows something exciting is going
//on.
BeginWaitCursor();
CWordContainerCntrItem* pItem = NULL;
TRY
{
//Get the document associated with this view, and be sure it's
//valid.
CWordContainerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Create a new item associated with this document, and be sure
//it's valid.
pItem = new CWordContainerCntrItem(pDoc);
ASSERT_VALID(pItem);
// Get Class ID for Word document.
// This is used in creation.
CLSID clsid;
if(FAILED(::CLSIDFromProgID(L"Word.Document",&clsid)))
//Any exception will do. You just need to break out of the
//TRY statement.
AfxThrowMemoryException();
// Create the Word embedded item.
if(!pItem->CreateNewItem(clsid))
//Any exception will do. You just need to break out of the
//TRY statement.
AfxThrowMemoryException();
//Make sure the new CContainerItem is valid.
ASSERT_VALID(pItem);
// Launch the server to edit the item.
pItem->DoVerb(OLEIVERB_SHOW, this);
// As an arbitrary user interface design, this sets the
// selection to the last item inserted.
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL);
//Query for the dispatch pointer for the embedded object. In
//this case, this is the Word document.
LPDISPATCH lpDisp;
lpDisp = pItem->GetIDispatch();
//Add text to the first line of the document
_Document doc;
_Document ActiveDoc;
Selection selection;
_Application app;
PageSetup pagesetup;
_Font font;
//set _Document doc to use lpDisp, the IDispatch* of the
//actual document.
doc.AttachDispatch(lpDisp);
//Then get the document's application object reference.
app = doc.GetApplication();
// From there, get a Selection object for the insertion point.
selection = app.GetSelection();
selection.SetText("This is a good place to say \"Hello World\"");
// Automate setting the values for various properties.
font = selection.GetFont();
font.SetName(_T("Tahoma"));
font.SetSize(12);
selection.SetFont(font);
But SaveAs method is not available ......
_Document oActiveDoc;
oActive = app.GetActiveDocument();
oActive.SaveAs(.....................);
--> Exception Error~!!!
|
|
|
|
|
I am trying to build several sample apps that let you use the MS Activex scripting engine in your apps and I get the following linker error with all of them:
ScriptEngineFactory.obj : error LNK2001: unresolved external symbol _IID_IActiveScriptParse
I assume this is quite a basic problem but I haven't been able to work out how to resolve it so far.
For an example of one such app see: Hosting VBScript in your own Application
I've just worked out this is happening on XP, but not on W98.
Any assistance much appreciated.
Neville Franks, Author of ED for Windows. www.getsoft.com
|
|
|
|
|
Hi all.
In my application (MFC), I need to develop a network White Board that will be able to open and share MS Office documents.
I didn't find any other way to open and use a Powerpoint document but to use Automation. I do not want (and it is not possible anyway) to install Office on any of the machine the software will run on. I found Powerpoint Viewer and derived the classes from Msppt8vr.olb (using ClassWizard) to be included in my application. I would prefer to be able to read the Powerpoint file format instead of using Automation, but it looks like I have no other choice unless MS decide to release the specs of their format or (and this is what I hope) someone already managed to read and work with this format.
Anyhow. After spending days looking for infos over the web (most of the MS documentation is for VB) I found the right way to use Automation for Powerpoint Viewer (which is different than Powerpoint). But still, when I create the object through OLE Automation, the CreateDispatch function failed and I have no clue why. Here's my code:
[CODE]
void CCtipptDlg::OnButtonPpttest()
{
_Application app;
SlideShowView ctippt;
COleException e;
CLSID clsid;
// Create the object that we'll drive through OLE automation.
if (CLSIDFromProgID(OLESTR("PowerPointViewer.Application"), &clsid) != NOERROR)
{
AfxMessageBox("CLSIDFromProgID Error!");
return;
}
if (!app.CreateDispatch(clsid, &e))
{
AfxMessageBox("CreateDispatch Error!");
return;
}
}
[/CODE]
The first IF statement (CLSIDFromProgID) returns with no errors, meaning that it's finding the CLSID just fine in the MS registry (I also doubled checked manually in the registry and its fine).
But the second IF statement (CreateDispatch) fails and the thing doesn't create the object!
If anyone has a clue about why it doesn't work, let me know.
Thanks a lot in advance, guys...
|
|
|
|
|
You might want to start off by looking at e.m_strDescription, which should contain a description of what is going wrong. Of course it'll likely be something like "Error 123xyz: Method Failed"
Pete
|
|
|
|
|
Thanks Pete. I'll have a look at it right away.
I'll post something as soon as I get some result.
|
|
|
|
|
Thanks a lot. You pointed me in the right direction, though I didn't use (because I didn't find it) m_strDescription. Instead, I did this:
void CCtipptDlg::OnButtonPpttest()
{
_Application app;
SlideShowView ctippt;
COleException e;
CLSID clsid;
TCHAR szCause[255];
// Create the object that we'll drive through OLE automation.
if (CLSIDFromProgID(OLESTR("PowerPointViewer.Application"), &clsid) != NOERROR)
{
AfxMessageBox("CLSIDFromProgID Error!");
return;
}
CoInitialize(NULL);
if (!app.CreateDispatch(clsid, &e))
{
e.GetErrorMessage(szCause, 255);
AfxMessageBox(szCause);
return;
}
}
Inside the second IF statement, I ran GetErrorMessage from my Exception handler (e) and it returned the message "CoInitialize not called"!!! There's no way in hell you will find anything in the MS documentation about CoInitialize in the Office Automation docs and I know what I'm talking about after spending Days on the MSDN documentation.
Anyhow, if anyone want to run an OLE automation client, here's what you'll need to do.
Thanks again Pete.
|
|
|
|
|
No Problem. I have to agree about the automation docs, I ran into similar problems trying to get some Excel automation running. In fact, I think it was exactly the same problem
|
|
|
|
|
As soon as I have some time (I seriously don't know when) I'll write some tutorials for my web site on all the tough problems I ran into in COM/MFC/OLE/ActiveX programming in the past few months.
My web site contains several tutorials on how to develop 3D video games using the Genesis3D engine but I discovered that more and more programmers are looking for infos and tutorials just about everything.
Anyhow, thanks again. This was really critical for me.
|
|
|
|
|
Does anyone know why this code:
void Cfoobar::Serialize( CArchive &ar )
{
int int_val;
bool bool_val;
...
...
ar >> int_val;
ar >> bool_val;
ar >> (int)bool_val;
...
...
}
throws up compilation errors for the bool variable, even if I cast it into int?
The errors read:
... error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'bool' (or there is no acceptable conversion)
... error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
What obvious point have I missed?
TIA,
Pete
|
|
|
|
|
int nBoolVal;<br />
bool bRealBoolVal;<br />
<br />
ar >> nBoolVal;<br />
bRealBoolVal = (nBoolVal != 0);
As for why? Probably because MFC pre-dates the C++ bool type, so everything is done with BOOL s.
--Mike--
My really out-of-date homepage
"Hey, you wanna go to the Espresso Pump and get sugared up on mochas?"
-- Willow Rosenberg
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan.
|
|
|
|
|
Doh!
I guess that's what they call "not seeing the woods for the trees"
I still think it's wierd that >> fails even when you cast the bool. I was under the impression that casting /always/ created the desired type, but apparently not. Maybe it's a bug in VC++
Another wierd thing is that
ar << bool_var;
works fine. Hmm.
Anyway, Thanks a lot,
Pete
|
|
|
|
|
moredip wrote:
I still think it's wierd that >> fails even when you cast the bool. I was under the impression that casting /always/ created the desired type, but apparently not.
The right side of CArchive::operator >> must be an l-value, that is, something that can be assigned to. A cast is not an l-value. (int) boolval does indeed create an object of type int , however that is not an l-value, thus you get the compile error.
And yes, you could probably try casting to an int& , which would make an l-value, but since the underlying object is smaller than an int , the reulting code will blow up or corrupt the stack or something equally Bad.
Moral: Casting is dangerous when you don't understand what happens under the hood. Just because you get rid of the compiler error doesn't mean your code is correct.
--Mike--
My really out-of-date homepage
"Hey, you wanna go to the Espresso Pump and get sugared up on mochas?"
-- Willow Rosenberg
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan.
|
|
|
|
|
Ok, that makes sense. I guess I still have a lot to learn!
Thanks a lot for the explaination, I appreciate you taking the time to point out something which is probably trivial to you!
Pete
|
|
|
|
|
Hi group, here's my question.
I have a VFP DLL sending an XML string back to my C program. The XML has an inline schema (which of course means datatypes, widths, fieldnames, etc).
I need to convert this huge string into a multi-dimensional array.
I've tried using the MSXml SDK, and some other cute toys, but nothing is even close to what I need.
If you know a way to extract the information from the schema, as well as the data itself, and throw it into the appropriate array columns (since some columns are left out in the original XML due to NULL values), PLEASE let me know.
Chris Zangarine
Bass-Trigon Software Development Team
|
|
|
|
|
In W2K if you unplug the network cable the tray icon changes to reflect the condition. Anyone have any idea how I can monitor this in my app as well? I tried searching for some info but I must be off target.
|
|
|
|
|
Hey, this article I found on the Usenet may be of help to you.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
|
Just wondering if i could get an opinion for the best case scenerio.
I'm writting a CWnd derived text editor...i know theres many out there already, but it's a challenge and none quite do what i want. Some flicker, others don't even implement shift selections.
I've got most of the drawing code complete, with caret movement etc...however the buffer implementation is what made me think twice about it's deisgn.
I currently use CStringArray each element is it's own line, blah blah...easy enough right?
However I noticed mapping the virtual buffer to the physical output would be a bit of a problem, mouse selctions seem more work than nessecary. So i was wondering
Would a char buff[lines*255] array work better...?
Although, if a line isn't exactly 255 bytes it would waste space and i suppose this is efficient, however it seems to be more easily implemented to the the physical screen...?
To avoid the pit falls of lost mem, i also thought of use one long flat array say 10 000 000 bytes and just adding the CRLF on each return, however for this to work i would have to iterate the entire array each key stroke to keep track of lines right...?
Any suggestions...?
My appologies for being so wordy
TIA
"An expert is someone who has made all the mistakes in thier field" - Niels Bohr
|
|
|
|
|
Why don't you replace CString s with something like, say CInfiniteString designed along the following lines:
class CInfiniteString
{
public:
CString& GetString(){return str_;};
TCHAR operator[](int n)const
{
if(n<str_.GetSize())return str_[n];
else return _T(" ");
}
private:
CString str_;
}; The idea is that CInfiniteString is as efficient as the encapsulated CString and you can use its operator [] for rendering operations (where it behaves as if having an infinite trail of blanks). Got the idea?
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
You could have a separate fixed buffer for the line you are currently
editing, eg 1024 bytes.
And when changing lines simply save what is currently in the buffer.
..but why are you doing this in the first place?
why not use the existing edit controls?
|
|
|
|
|
Why am i doing this...?
Cuz I am not a professional programmer I'm someone who simply enjoys the challenges computers give me. Over the years i've self studied many langs, like basic,pascal,assembly,c++,JScript,perl(and other c derivitives...?) and even once had a quick fix for cobol and lisp. I dropped outta<grins> high school in Gr. 10 and found myself pumping gas and soon realized it sucked. basically i'm a VERY un-convetional programmer and somes times like to do things the hard way, just for experience. Money or time is not an issue for my projects, efficiency in code is everything. Sometimes this means re-inventing the wheel and making it better like BASF<smiles> I'm not saying existing controls aren't great, but they don't do what i want.
I'm no expert, but i've looked for ways of using existing windows edit control(CEdit) but couldn't quickly figure out how to increase it's buffer size so i gave up and decided to write my own.
Sorry for the verbose life story, but i'm often asked why re-invent the wheel or don't fix it if it ain't broke...my answer. What fun is that.
Ciao
"An expert is someone who has made all the mistakes in thier field" - Niels Bohr
|
|
|
|
|
I am seeking information regarding memory allocation. Specifically, about the default C++ memory allocation (that comes with the compiler), how well it works in a multithreaded environment -- or on a server.
I expect that this memory allocation was written 'more for' a client application, where huge numbers of allocations are not necessarily done.
Does anyone out there have good experience with this? What good alternatives are there? I am looking at an advertisement for "SmartHeap from MicroQuill", and their ad says that the compiler-supplied memory management is SLOW SLOW SLOW, and even slower in a multi-processor and multi-threaded environment....
I've had this feeling for a long time that 'just using' the default compiler libraries for memory management is not a good long-term solution. And do any of these have good tools for debugging memory problems?
Any comments out there?
|
|
|
|
|