Solved

sharing a file pointer with a dll

Posted on 1998-12-08
8
363 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:jpf080698
ID: 1325899
it comes at the fclose(filePointer) in closeSFile. I hope it helps.
0
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Smart Camera scanning and reading information 3 114
post4 challenge 28 104
MaxSpan challenge 9 106
Line meaning 9 88
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. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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