Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8491687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:30:04+00:00 2026-06-10T22:30:04+00:00

I have a file pointer exported from a dll, which is initialized(fopen) by the

  • 0

I have a file pointer exported from a dll, which is initialized(fopen) by the application and then used(fprintf) inside the dll.

The problem is fprintf will throw an exception.

DLLFile.c

#define BUILD_FOO
#include "API.H"

File *pFile;

void exportedFunction()
{
   fprintf(pFile,"This will result in an exception\n");//<-This print will crash
}

API.H

#ifdef BUILD_FOO
#    define FOOAPI __declspec(dllexport)
#else
#    define FOOAPI __declspec(dllimport)
#endif

FOOAPI  extern File *pFile;
FOOAPI  void exportedFunction();

APLICATION.C

#undef BUILD_FOO
#include "API.H"
void main()
{
pFile = fopen("path_to_folder","wt");
fprintf(pFile , "This print will work"); // <- This will be printed ok
exportedFunction(); 
}

1 From the debugging I’ve done, this is what I saw:

Inside the application, fopen() assigns for pFile an element from _iob[].

In the DLL when fprintf is called, it is checked that pFile is part of the _iob[], but the _iob[] from the application seems not to be the same with the one in the DLL(they have different addresses).

2 I have the same use case(with the same application) and another somewhat similar DLL, and everything works ok there(the _iob[] is at the same place in the application and DLL).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:30:06+00:00Added an answer on June 10, 2026 at 10:30 pm

    This is likely being caused by your application and your DLL disagreeing on which version of the C runtime they’re using. Unless they’re both compiled against the exact same version of the C runtime, all bets are off, and you can’t call CRT functions from one using the data from another or vice-versa.

    The safest way to avoid this problem is not to pass FILE* pointers across DLL boundaries. That way, any interaction with a FILE* will always happen using the same version of the CRT, and there’s no danger of any mismatches. So your DLL should not expose a FILE* variable; instead it should be some opaque type, and all operations on the variable need to happen in the same module.

    For example:

    // API.h
    FOOAPI void set_file(void *file);
    FOOAPI void set_fprintf_callback(int (*my_fprintf)(void *, const char *, ...));
    FOOAPI void exportedFunction();
    
    // DLLFile.c
    void *pFile;  // Not exported
    int (*fprintf_callback)(void *, const char *, ...);  // Not exported
    
    FOOAPI set_file(void *file)
    {
        pFile = file;
    }
    
    FOOAPI set_fprintf_callback(int (*my_fprintf)(void *, const char *, ...))
    {
        fprintf_callback = my_fprintf;
    }
    
    FOOAPI exportedFunction()
    {
        // Call back into the application to do the actual fprintf
        fprintf_callback(pFile, "This should not crash");
    }
    
    // Application.c
    int mydll_fprintf(void *pFile, const char *fmt, ...)
    {
        va_list ap;
        va_start(ap, fmt);
        int result = vfprintf((FILE *)pFile, fmt, ap);
        va_end(ap);
    
        return result;
    }
    
    int main()
    {
        FILE *pFile = fopen(...);
        set_file(pFile);
        set_fprintf_callback(&mydll_fprintf);
        exportedFunction();
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file pointer which I am using with fgets() to give me
I have to complete this task: I'm given a xml file which is exported
I have a DLL which exports a few functions. The source file contains a
Say I have a file pointer like this: file_ptr = fopen(test.txt, r+); and I
Working on a problem where I have to read data from a file into
I have file with name contact.php, inside I have form: <form method=post name=kontakt action=send_mail.php>
Suppose I have file which goes like this : a void measure() { a
Say I have file A, in middle of which have a tag string #INSERT_HERE#.
I wrote my program in C++ and exported it as a DLL. I have
I have a file pointer, such as the following: FILE* f = tmpfile() How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.