|
#include <algorithm>
#include <fstream>
#include <sstream>
int main()
{
std::fstream fs("d:/test.txt");
std::stringstream ss;
ss << "hello" << std::endl;
std::copy(std::istream_iterator<char>(ss),
std::istream_iterator<char>(),
std::ostream_iterator<char>(fs));
fs.close();
return 0;
}
The code not work, I have some questions:
1. What does this mean: std::istream_iterator<char>() ?
2. Is the position of the stream iterator depent on the current postion of the stream itselft?
Thanks.
3. How to make the code work?
modified on Wednesday, November 4, 2009 9:13 AM
|
|
|
|
|
Firstly - a CP/HTML thing - you need to escape the < characters in the template parameter spec of the iterators (looking at the source of the page, they should be <char> , yes?).
Try declaring fs as a std::ofstream or making sure that the destination file exists - with that change, the code works fine.
std::istream_iterator<char>() is a sentinel value that represents the end of any input stream. Once an istream_iterator<char> steps past the end of the file, it has the same value as std::istream_iterator<char>() .
A stream iterator interacts with the stream position. It's a bad idea to manipulate the stream while there are active stream iterators.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
|
Hello everybody!
Now I installed the WTL8,and clicked "D:\AppWiz\setup80.js" to setup.
I can only use WTL Wizard in VS2005,but I can't see the WTL wizard when I create a new project in VC6.0 ?
thanks for your reply !
|
|
|
|
|
You need to use the Wizard that came with the WTL 7.1[^] - 7.5 and later only support VS2002 or later.
I think you may still be able to use WTL 8, by using the WTL8 include directory in VS6 rather than the WTL 7.1 directory.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
|
I have created an in-process COM object (DLL) using ATL. Note that this is an object and not a control (so has no window or user-interface.) My problem is that I am trying to fire an event from a second thread and I am getting a Catastrophic failure (0x8000FFFF). If I fire the event from my main thread, then I don't get the error. The second thread is calling CoInitializeEx but this makes no difference. I am using the Apartment threading model but switching to Free Threaded doesn't appear to help.
The fact I am trying to do this from a second thread is obviously crucial. Is there an easy way to do this?
For example, in my main object's source file:
STDMETHODIMP MyObject::SomeMethod(...)
{
CreateThread(NULL, 0, ThreadProc, this, 0, NULL);
FireEvent(L"Hello, world!");
return S_OK;
}
DWORD WINAPI ThreadProc(LPVOID param)
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
MyObject* comObject = reinterpret_cast<MyObject*>(param);
comObject->FireEvent(L"Hello, world!");
}
void MyObject::FireEvent(BSTR str)
{
...
pConnection->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, NULL, NULL, NULL);
}
One idea would be for the main COM object class to create a hidden window and pass the window handle to the thread. The thread could then post a message to the window which would then fire the event. This would ensure the right thread is firing the event. The only problem with this is I don't think there is a message pump running in the case of a simple COM object like this (although I might be wrong) and I have no ideas how to implement one (DLLMain? no ideas.)
|
|
|
|
|
Hello Rob,
The problem is that the client object's event interface pointer (e.g. pConnection in your code snippets) is not transferred to your thread properly. It must be marshaled to the thread function via one of the standard marshaling APIs. The simplest one to use is the IGlobalInterfaceTable interface methods.
I'll follow up with further responses with sample codes that you can use.
Bio.
|
|
|
|
|
As Lim Bio Liong already said, the problem is that the source interface, i.e. the interface that is called from your secondary thread, is not properly marshalled when crossing apartment boundaries.
Like you suggested yourself, you can post a message to the main thread and call the source interface from there.
But there are two other common ways when firing COM events from other threads:
1. Using ::CoMarshalInterThreadInterfaceInStream() [^] and ::CoGetInterfaceAndReleaseStream()
2. Using the Global Interface Table (GIT)
When the interface to be marshalled is a source interface, alternative #1 is useful if you know that all calls will be made from the thread that you've marshalled the interface into.
Alternative #2 is useful when you will be able to call the interface from any thread, or don't know what thread will make the call. But the downside is that it requires a little overhead compared to alternative #1 since you need to "get" the interface for every call.
Michael Lindig has written a very nice ATL solution that I've used for years which uses the GIT.
You'll find it here[^].
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Hello Rob,
I've written some utility functions that can help you with working with the Global Interface Table. Email me : bio_lim_2004@yahoo.com
I'll send them to you.
Bio.
|
|
|
|
|
I have an interesting problem.
I have a free threading service (controller) that receives messages from a client application.
One of these messages is to instantiate a COM server that is controlling hardware resources. (Not a device driver).
The difference to the normal model is that the customer requires resilliance in the form that each instantiation invokes a new process and then maintains the RPC communication with it.
I have done this before, however am having some difficulties now. Essentially it uses the same process and does not kick another one off. I almost need a kind of singleton COM object, that will force process startup on every instantiation.
Compiler .NET 2005, operating system Windows XP SP2 and the language is C++.
Any ideas?
|
|
|
|
|
First, i have a Interface name IA, i implement it in class CA.
Second, i want to use a Interface IB driving from IA. So, I want to implement IB in class CB. Since IB drove from IB, I don't want to copy methods implemented by CA to CB.
I use ATL wizard to add a new simple ATL object (IB) to the project, and modify the IB's base Interface from IUnkown to IA. But when i compile the project, VS would give me an error says there are some abstract methods had not been implemented in CB otherwise i copy all the implemented methods of CA to CB.
If no atl, i think i can do it correctly. but i have no idea in atl.
What should i do?
|
|
|
|
|
If you derive IB from IA , you have to re-implement whatever you did in CA .
You could however derive IB from CA and implement only the methods of IB in CB .
|
|
|
|
|
What a pity! You know, if IA already was a big interface, that is, CA had a plenty of code, i would have to copy the same code from CA to CB.
|
|
|
|
|
Err, hold on a second, kcynic. You do not absolutely need to copy the code from CA to CB.
Here are some alternatives :
1. You could simply have only the CA object and have it implement IB. To do this :
1.1 Derive CA from IB, e.g.
class ATL_NO_VTABLE CA :
public ...
public IB...
1.2 Then in your COM map, declare that CA implements IB :
BEGIN_COM_MAP(CA)
...
// COM_INTERFACE_ENTRY(IA) // Make sure this is commented out.
COM_INTERFACE_ENTRY(IB)
COM_INTERFACE_ENTRY2(IA, IB) // Indicates that IA interface is gotten through IB.
...
END_COM_MAP()
1.3 You then implement methods of IB in CA. Essentially, CA is derived from both IA and IB.
2. If you absolutely need to have 2 objects, CA and CB, then you could simply contain the whole of CA's implementation inside CB. You do this via COM techniques of containment or aggregation. You can read these up in MSDN. This way, you can avoid making a duplicate of CA's implementation which is problematic for maintenance.
- Bio.
|
|
|
|
|
Cool. This is really the perfect solution! I have read the concept on the book but couldn't take them into practice. I remember your idea.
And, btw, are you usually working with ATL/WTL/COM? Im a beginner of COM programming and only know the basic knowledge of com, atl/wtl too. Im reading the book 'ATLs Internals Second Edition Working with ATL8'.
Regards.
|
|
|
|
|
|
Thanks, I will.
im learning WTL too. because, its independent with MFC. I think wtl(atl too) as perfect practice of C++ concepts. And have you read the book i said before, if so, i hope i can ask you some puzzling questions about that book.
|
|
|
|
|
I have a book titled "ATL Internals" but I'm not sure if it is the same one you have. What is/are the name/s of the author/s ?
Bio.
|
|
|
|
|
Book Name:
ATL Internals: Working with ATL 8
Book Authors:
Christopher Tavares
Kirk Fertitta
Brent Rector
Chris Sells
maybe yours is the first edition
|
|
|
|
|
Yes, I think I have the book at home. Would be most glad to help if you need any explanation.
- Bio.
|
|
|
|
|
OK. Its very kind of you.
|
|
|
|
|
I'm planning to port my Firefox addon to IE. I'm wondering if there's any code library I could use to make my task easier.
|
|
|
|
|
Don't know of any such library but the ATL Wizard will help you here.
When you add a COM Simple Object using the Add Class wizard, there is an option to choose if IObjectWithSite[^] needs to be supported. Check that and you're ready to go.
Read more about BHOs here -
Browser Helper Objects: The Browser the Way You Want It[^]
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)
|
|
|
|
|
Hi, yet again I'm having a problem with some C++/WTL, this time it's the toolbar. I want to change the background color of the rebar it's placed in. The rebar is created using CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE); and the toolbar is added using AddSimpleReBarBand(hWndToolBar); but how do i chance the color of the rebar/toolbar/menu?
|
|
|
|