Solved

sharing a file pointer with a dll

Posted on 1998-12-08
8
361 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:jpf080698
8 Comments
 
LVL 3

Expert Comment

by:plaroche
ID: 1325896
have you tried assigning the file pointer?

main()
{
  FILE *fp;

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


0
 

Author Comment

by:jpf080698
ID: 1325897
Of course, but I just forgot to write it in my question...
0
 
LVL 3

Expert Comment

by:plaroche
ID: 1325898
and where does the assertion failure come at?  did you step through the code?
0
 

Author Comment

by:jpf080698
ID: 1325899
it comes at the fclose(filePointer) in closeSFile. I hope it helps.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:Mirkwood
ID: 1325900
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
 
LVL 86

Expert Comment

by:jkr
ID: 1325901
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
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 200 total points
ID: 1325902
Check to make sure the DLL and the EXE are both using the C runtime library as a DLL and not statically linked.
0
 

Author Comment

by:jpf080698
ID: 1325903
Thanks a lot thui!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now