|
Hello,
I'm trying to redirect text, so that all text printed with printf would go to an edit control. I've seen a couple of examples (one by Microsoft and another by Codeguru) but those are done in a slightly different way.
What I want is to redirect text in my application, but those two examples use another application, whose printf commads they redirect. If I understood it right, an important thing in these applications is that they open a new window (well, sometimes they open it hidden) of the other application as the main application's child process.
Here's something that I've tried:
SECURITY_ATTRIBUTES sa;
::ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
sa.nLength= sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HANDLE hStdOutOld = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hStdOutReadTmp;
if ( !::CreatePipe(&hStdOutReadTmp, &m_hStdOut, &sa, 0) )
{
MessageBox("Error: CreatePipe");
return;
}
if( !::SetStdHandle(STD_OUTPUT_HANDLE, m_hStdOut) )
{
::CloseHandle( hStdOutReadTmp );
::CloseHandle( m_hStdOut );
MessageBox("Error: SetStdHandle");
return;
}
BOOL fSuccess = DuplicateHandle(GetCurrentProcess(), hStdOutReadTmp,
GetCurrentProcess(), &m_hStdOutRead, 0, FALSE, DUPLICATE_SAME_ACCESS);
if ( !fSuccess )
{
MessageBox("Error: DuplicateHandle");
return;
}
CloseHandle(hStdOutReadTmp);
After this there should be the child process creation. But do I have to make it? There's also another thing that I don't understand: how do I connect the other end of the pipe to the edit control?
I tried to connect the edit control and stdout straight without a pipe like:
CEdit *pEdit = (CEdit*)(this->GetDlgItem(IDC_EDIT));
m_hStdOut = (HANDLE)pEdit->GetSafeHwnd();
if( !::SetStdHandle(STD_OUTPUT_HANDLE, m_hStdOut) )
{
::CloseHandle( m_hStdOut );
MessageBox("Error: SetStdHandle");
return;
}
The result is that nothing happens.
I also thought of doing it in the same way as in examples, but before creating windows. The problem is, that I have a console in the beginning (the starting point of this app is main() and in there the app is started using InitInstance() and Run()). If I understood those examples right all that creating pipes and so on needs to be done before creating windows. Does console count as a window?
What would be the best way to go?
-Janetta
|
|
|
|
|
Forget printf ( unless you have no choice ), your best bet is to define your own unbuffered iostream, and pass the text to the edit control from there.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
I do have a choice, but if I don't use printf, I have a very long way to go... I think that redirecting would be an easier way.
-Janetta
|
|
|
|
|
Janetta wrote:
I do have a choice, but if I don't use printf, I have a very long way to go...
How so ?
Janetta wrote:
I think that redirecting would be an easier way.
I dunno - I reckon the stream is about a half hours work. I've been meaning to do an article on writing your own streams anyhow - how soon do you need this ?
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
Christian Graus wrote:
Janetta wrote:
I do have a choice, but if I don't use printf, I have a very long way to go...
How so ?
Actually I think that it would't be that hard after all. There is some parts of the source code, that I can't change, but I think that there is no printing in those parts. Would I be able to use fprintf with my own stream?
Those examples just seemed pretty easy, but maybe it's not that simple, especially in this case.
Christian Graus wrote:
I've been meaning to do an article on writing your own streams anyhow - how soon do you need this ?
In a couple of days, but that kind of article would propably be useful anyway!
-Janetta
|
|
|
|
|
Janetta wrote:
Would I be able to use fprintf with my own stream?
No, a stream is safer and more elegant than that. cout is a stream. You can do this
MyEditStream << "This text will go to my window along with the number " << 7 << " and any other type I choose to define an operator for";
streams *rule*
Janetta wrote:
In a couple of days, but that kind of article would propably be useful anyway!
If I post the article 24 hours from when this reply went up, will that do you ?
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
Christian Graus wrote:
No, a stream is safer and more elegant than that. cout is a stream.
Actually I thought that the first parameter of fprintf was stream, but I guess it's not. I know more about streams, but these printf:s and so on I don't. Part of this program has to be written in C, so...
Christian Graus wrote:
If I post the article 24 hours from when this reply went up, will that do you ?
You are fast. That sounds fine!
-Janetta
|
|
|
|
|
Janetta wrote:
Part of this program has to be written in C, so...
Streams are c++ only, does that make it not useful to you ?
Janetta wrote:
You are fast. That sounds fine!
Like I said, about a half hour coding. Add an hour for the text, I'll knock it over tomorrow night as a warmup to the work I want to do for myself.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
Christian Graus wrote:
Streams are c++ only, does that make it not useful to you ?
No. I have to separate some parts anyway with #ifdef WIN32. I prefer C++, so I'll use it as often as possible.
Christian Graus wrote:
Like I said, about a half hour coding. Add an hour for the text, I'll knock it over tomorrow night as a warmup to the work I want to do for myself.
Thanks a lot!
-Janetta
|
|
|
|
|
I think I didn't make my deadline, but the article is up. It contains a header file that you can include, and the example code shows how to use it to stream into an edit box. The project uses MFC, but the stream itself does not ( I did not know if you used MFC, so I did not ).
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
Christian Graus wrote:
I think I didn't make my deadline, but the article is up.
Well, it's a good thing that we're not at school and I'm not your teacher
I read the article and now I'll go through the code. The article seemed very useful, not only for this case, but also for the future. Thank you once again!
-Janetta
|
|
|
|
|
To redirect printf is not that easy. I tried it the time I was writing the article
CEditLog - fast logging into an edit control with cout (which should also be quite useful to you), but failed. The internal structure of CRT streams (FILE*) is undocumented and to get it out of the sources is quite a long way.
However, If you have all sources available, the solution could be quite easy:
1) Write a printf-compatible function that prints out the output into the edit:
int printf_edit( const char* pszFormat, ... )
{
...
}
2) Add the following definitions to your stdafx.h (or in front of every .cpp file):
#undef printf
#define printf printf_edit
and recompile the whole thing.
--
Daniel Lohmann
http://www.losoft.de
|
|
|
|
|
Your article was very useful! I think I'm able to solve the problem now.
Thanks!
-Janetta
|
|
|
|
|
Console is application , but application can create windows (:
there are ( on this site ) some exapmles, how to create windows from console (:
----------------------------
my eng is bad, so am i .. (:
|
|
|
|
|
I want to program a simple spreadsheet program, where can I find any resource, like source code and data format. Especially the format, what are the basic elements should a spreadsheet file contains?
|
|
|
|
|
Stingray offer a product which is essentially MS Excel 2000. You get the source code and can compile to lib or ctrl.
Alternative you need to start of with a Grid Clickerty and you'll need a parse and expression engine and if you want to support VB script you'll need a VB Scripting engine.
Normski. - the next bit of code is self modifying ... jmp 0xCODE
|
|
|
|
|
I have a ISAPI extension, which I suppose run in security account
of IUSR_MACHINENAME. For accessing a database located on
another server, I must change the user to an account which has rights in database.
Is this possible? I know is possible to "create a process as user"
but I don't know about changing the user during process running.
If I'm on a wrong way of thinking, which are the possibilities?
Regards,
Don
|
|
|
|
|
It is not possible to change the security context of a running process. However, you could use impersonation to change the context of your current thread, which is actually what you want to do
For this you have to get a proper impersonation token from somewhere. You can get such a token via some IPC mechanisms (like NamedPipes, RPC, DCOM) or by calling LogonUser(). However, I doubt that IUSR_MACHINENAME has the necessary privileges to call LogonUser().
--
Daniel Lohmann
http://www.losoft.de
|
|
|
|
|
Thanks Daniel,
You give me some good ideeas, with LogonUser(). If IUSR_... has not enough ritghts, I'll include it in Administrators group, and maybe
will work.
The other ideea, to
Daniel Lohmann wrote:
get such a token via some IPC mechanisms
I simply don't understand: from where to request the token?? With who
shall I communicate via IPC?
... and what is this "Impersonation" means???
Thanks again.
|
|
|
|
|
Don Miguel wrote:
You give me some good ideeas, with LogonUser(). If IUSR_... has not enough ritghts, I'll include it in Administrators group, and maybe
will work.
Uh, oh - I would double think about that. It's the sense of special IUSR_xxx accounts that they are low privileged and therefore your Inet apps do open a big security hole. I don't have much knowledge about all this IIS stuff, but maybe you could configure it to run your ISAPI in another user context than IUSR_xxx. That would be much better, because no other extensions are affected.
--
Daniel Lohmann
http://www.losoft.de
|
|
|
|
|
I've a COM object in VC++ that has setData(PBYE* pdata, int nsize) function. What's the equivalent of it in IDL, so in VB it can pass the data to the COM.
Moreover, what's the way VB pass the data to it ?
|
|
|
|
|
what is PBYE ? If you want to pass a string into COM it will need to be a BSTR. VB will convert a string literal or value for you, AFAIK.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
Sorry, it should be PBYTE (in C++, a pointer to byte). I'm not going to pass String values, I want to pass binary data to an COM object.
|
|
|
|
|
Dear friends, How to get properties of any control ie created by any application (not window styles, extended styles) eg: Delphi.
waiting for reply
Thanks,
Raman.
|
|
|
|
|
I want to make a serial port communication program. I define a STRUCT variable
struct _COMM_PACK
{
char sID[PACK_ID_LEN];
BYTE cCmdCode;
UINT nVerifySum;
int nDataLen;
BYTE cData[MAX_DATA_LEN];
}aSendPack;
and want to send 'aSendPack'in the Mscomm control-comm like:
comm.SetOutput(aSendPack);
but the SetOutput need a VARIANT variable
how can I do that?
thanx!
|
|
|
|