Solved

sharing a file pointer with a dll

Posted on 1998-12-08
8
360 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 107
wait notify demo infinite loop 3 81
EvenOdd challenge 10 83
Line meaning 9 77
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

747 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