Click to See Complete Forum and Search --> : Run Time Dynamic Linking and ofstream prob


MonicaLearns
February 25th, 2003, 06:27 PM
Hi,
I have been stuck with a problem related to Run Time Dynamic Linking and ofstream¡¦s, for about 3 days now and see no solution. This is what I am trying to do:
Referring to the SDK Documentation: Dynamic-Link Libraries->Using Dynamic-Link Libraries->Creating a simple dynamic-link library->Using run-time dynamic linking, I tried to use the concepts described in the articles to do the following:
a)I created a dll called myputs.dll that has one exported function namely myPuts( LPTSTR) (mydll.dsp) which writes to an ofstream and creates an output file test.out
b)Used Run Time dynamic Linking to load the library and call the function myputs in a WIN-32 Console Project namely MyDllTest where I invoke the function myputs more than once. and try to output to an output file.

Problem:
a)No matter how many times I call the exported function in MyDllTest, its invoked only once after which the program crashes. This means that the output file has only one message namely ¡§First message via DLL function¡¨ instead of the 3 that I intended to be there.
b)If I don¡¦t manually delete the test.out file from the library, the contents are appended to the file the next time that the program is run and effectively I can calculate the number of runs of the application by counting the number of times the first(among 3 messages) message namely: ¡§First message via DLL function¡¨ is output to the file.

Questions:
a)I think that I would be right in saying that the ¡§Run Time Dynamic Linking¡¨ Concept is working ok in this test application. Am I?
b)How do I manage to print out the 3 messages namely
- "First message via DLL function\n"
- "Second message via DLL function\n"
- "Third message via DLL function\n"
in the output file namely test.out?
I know that the ofstream is not working properly but, I tried the MSDN examples as well and somehow I seem to have a problem with all of them, including this one: sample program in basic_filebuf::close and I don¡¦t get the same output there.
For the first two lines, I get blanks as output. I wonder why? :( I have the file basic_filebuf_close.txt with the same contents but yet I get blank spaces.Please tell me what I am doing wrong here!! :(
c)What is the way to ensure that the stream is opened only once and is written to each time that the myputs function is called? I mean, if I used another test application and used the same dll to invoke the function for another purpose and I still want that ALL the output strings should be appended to the same file, how do I ensure that?

I know I am making some BIG fundamental mistake in all this, but perhaps the confusion has resulted in me trying too many things at the same time and hencve my vision is totally clouded.
Please help.
Thanks,
Mons

Mick
February 25th, 2003, 07:02 PM
The two things that jump out are:

1. change MyDllTest to calling convention __stdcall
2. change the use run-time library on both of them to debug multithread, or single threaded debug.

Both of those settings are in project->settings->C++ tab->code generation.

I'll have to look at it more when I get time later...

PaulWendt
February 25th, 2003, 08:32 PM
I'm with Mick 2002 on this one: I've found that when there are
weird crashing problems with DLLs, a very common reason is that
the DLL was linking with a different version of the C-Runtime than
the executable.

MonicaLearns
February 25th, 2003, 10:17 PM
Thanks so much.
It works just fine now.
:)