Sign in to follow this  

About output string to console from dll

This topic is 2548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I got problem on output string to stdout of a console application.
The detail situation is like following,

in console application,

int main(void){
//...
fprintf(stdout,"OK"); //
}

The output is ok, but if I use something like this, it crashes with terrible shame,

//In dll,
void COutputStream::Output(FILE* out,const char* strMsg)
{
fprintf(out,strMsg); //The application crashes here.
}

//In console applciation.
COutputStream out;
out.Output(stdout,"ok");

And the call stack is

-> Debug.dll@COutputStream::Output //crashed
test.exe@main

How I can make use of stdout of a console application as variable and pass it to other modules?

Thanks in advance.

[Edited by - kimryo on December 23, 2010 2:19:12 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
Why are you using fprintf with stdout in a c++ app?


fptinf(out,strMsg); //The application crashes here.


Looks like a typo.


Sorry for the typo, I've already corrected it.

I'm just picked up a way to output string to console, and there is
no rule says it is illegal to using fprintf and stdout in a c++ app...

Share this post


Link to post
Share on other sites
It isn't strictly illegal, but it isn't a good idea, either.

fprintf() is not type safe, and can introduce certain classes of security problems and nasty bugs if not used with extreme care. It exists in C++ solely for compatibility with legacy C code; just because it's there doesn't mean you should use it.


That said: your code as posted is fine. Something else must be going on that's causing a crash.

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
It isn't strictly illegal, but it isn't a good idea, either.

fprintf() is not type safe, and can introduce certain classes of security problems and nasty bugs if not used with extreme care. It exists in C++ solely for compatibility with legacy C code; just because it's there doesn't mean you should use it.


That said: your code as posted is fine. Something else must be going on that's causing a crash.


Ok, I understand the risk of using fprintf, for this example I just using it to output simple text strings, I don't think rare unsafe case been involved.

And cause the main exe and dll are both very simple projects, elsewhere caused this bug also not likely possible. And cause the bug only appears when the fprintf call happens in another module other than exe itself, I just wonder if it's because the module change makes CRT lost some kind of environment(Maybe input/output redirections or something like that) which allows fprintf been called normally.

Share this post


Link to post
Share on other sites
Passing the stdout file handle across DLL boundaries is probably the cause of your problem. In general, sharing resources across DLL/EXE divisions is very finicky and difficult to get right. You're better off either having the DLL return a string that the EXE prints, or having the EXE tell the DLL to print and the DLL does its own internal access to stdout.

Share this post


Link to post
Share on other sites

This topic is 2548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this