Jump to content
  • Advertisement
Sign in to follow this  
kimryo

About output string to console from dll

This topic is 2852 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
Advertisement
Why are you using fprintf with stdout in a c++ app?


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


Looks like a typo.

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
Thanks for reply. I'm just continue on seeking for the exact reason.

Merry Christmas and happy new year.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!