Fatal error when using FILE* in Windows from DLL

Posted by AlannY on Stack Overflow See other posts from Stack Overflow or by AlannY
Published on 2010-05-07T13:11:32Z Indexed on 2010/05/07 13:38 UTC
Read the original article Hit count: 270

Filed under:
|
|
|
|

Hi there. Recently, I found a problem with Visual C++ 2008 compiler, but using minor hack avoid it. Currently, I cannot use the same hack, but problem exists as in 2008 as in 2010 (Express).

So, I've prepared for you 2 simple C file: one for DLL, one for program:

DLL (file-dll.c):

#include <stdio.h>

__declspec(dllexport) void
print_to_stream (FILE *stream)
{
  fprintf (stream, "OK!\n");
}

And for program, which links this DLL via file-dll.lib:

Program:

#include <stdio.h>

__declspec(dllimport) void print_to_stream (FILE *stream);

int
main (void)
{
  print_to_stream (stdout);
  return 0;
}

To compile and link DLL:

cl /LD file-dll.c

To compile and link program:

cl file-test.c file-dll.lib

When invoking file-test.exe, I got the fatal error (similar to segmentation fault in UNIX).

As I said early, I had that the same problem before: about transferring FILE* pointer to DLL. I thought, that it may be because of compiler mismatch, but now I'm using one compiler for everything and it's not the problem. ;-(

What can I do now?

UPD: I've found solution:

cl /LD /MD file-dll.c
cl /MD file-test.c file-dll.lib

The key is to link to dynamic library, but (I did not know it) by default it links staticaly and (hencefore) error occurs (I see why).

P.S. Thanks for patience.

© Stack Overflow or respective owner

Related posts about c

    Related posts about visual