sharing a file pointer with a dll

I'd like to open and close a file in a dll from an application. The aim is that the aplication and the dll-routines write into the same file.
After trying unsuccessfully to export the file pointer from the dll to the application, I wrote the open and close file routines in the dll. Doesn't work better...
I wrote then:
in dll header file:
#define VD_DLL __declspec (dllexport)
extern VD_DLL FILE *openSFile(char *fileName)
extern VD_DLL void  closeSFile(FILE *filePointer);

in dll code:
VD_DLL FILE *openSFile (char *fileName)
{
  return (fopen (fileName,"w"));
}
VD_DLL void closeSFile (FILE *filePointer)
{
  fclose (filePointer);
}

in exe header:
#define VD_DLL __declspec (dllexport)
extern VD_DLL FILE *openSFile(char *fileName)
extern VD_DLL void  closeSFile(FILE *filePointer);

in exe code:
main()
{
  FILE *fp;

  openSFile ("file.txt");
  closeSFile (fp);
}

This program causes an assertion failure. How could it work?
jpf080698Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Tommy HuiConnect With a Mentor EngineerCommented:
Check to make sure the DLL and the EXE are both using the C runtime library as a DLL and not statically linked.
0
 
plarocheCommented:
have you tried assigning the file pointer?

main()
{
  FILE *fp;

  fp = openSFile ("file.txt");
  closeSFile (fp);
}


0
 
jpf080698Author Commented:
Of course, but I just forgot to write it in my question...
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
plarocheCommented:
and where does the assertion failure come at?  did you step through the code?
0
 
jpf080698Author Commented:
it comes at the fclose(filePointer) in closeSFile. I hope it helps.
0
 
MirkwoodCommented:
Let me guess. The assertion is something like memory is not allocated but freed.
This is because the memory is allocated in the DLL but freed in the EXE. It's not an error but not very nice also.
0
 
jkrCommented:
Regarding the fact that this is the MFC area - is your DLL a MFC DLL? And if so, do you use 'AFX_MANAGE_STATE()' in the functions exported by the DLL?

If not, (and following Mirkwoods idea about memory, which could indeed be the case), try

VD_DLL FILE *openSFile (char *fileName)
{
FILE* fp = fopen (fileName,"w");

setvbuf ( fp, NULL, _IOFBF, 4096); // 4096 is just an example ;-)

return (fp );
}

0
 
jpf080698Author Commented:
Thanks a lot thui!
0
All Courses

From novice to tech pro — start learning today.