Can I pass a FILE object through the borders of a DLL?

I have a C ++ environment where some calculations are delegated (sometimes automatically generated) to C-functions or C ++ -functions with external "C" link. These are low-level routines that must be evaluated very quickly and with minimal overhead, and they are usually located in separate shared objects / DLLs. Their current signature is something like:

int my_generated_function(const double* input, double* output, double* work);

which will be in the shared library downloaded using dlopenon POSIX or LoadLibraryon Windows. The corresponding function pointers are retrieved using dlsym(handle, "my_generated_function")either POSIX or GetProcAddress(handle, TEXT("my_generated_function"))Windows.

Is it safe to carry a signature with a FILE object pointer?

int my_generated_function(const double* input, double* output, double* work,
                          FILE* logfile);

Note that the shared object containing my_generated_functionmay be compiled with a different (but binary compatible) compiler than the code that loads the shared object.

+4
source share
1 answer

You can think of FILE*as an opaque handle. The problem is that the actual object under this descriptor, in other words, the definition of the FILE structure and its implementation details, is specific to the / CRT compiler.

, , , FILE, , Visual Studio 2008 2010 - , VS2015 2017. , (FILE) , CRT .

, FILE* DLL, , /CRT V++.

, -/CRT- DLL, Win32 HANDLE (, API, CreateFile). , V++/CRT.

Win32 HANDLE FILE*, _get_osfhandle _fileno, this SO answer.

+5

Source: https://habr.com/ru/post/1685167/


All Articles