PyRun_SimpleFileExFlags crash on ftell?

the following looks to be correct from all the examples I have found…

if (!FileExists(arg2)) return 0;
FILE* fp = fopen(arg2, "rb");
if (fp == NULL) return 0;
std::string fname = FileNameFromPath(arg2);
return PyRun_SimpleFileExFlags(fp , fname.c_str(), 1, NULL); 

I have stepped through the source in the python311 dll and its crashing at

maybe_pyc_file(FILE *fp, PyObject *filename, int closeit)
if (ftell(fp) == 0)  

An invalid parameter was passed to a function that considers invalid parameters fatal.
fp is not null and works from within my main body of code…

I have both the python311.dll and my code compiling with /MT and the same platform toolset etc. Something weird is going on.

Any ideas?

humm switching both to compile with /MD (compile using external vc runtime) allows it to work…

fopen/ftell must use a runtime specific lookup table? so /MT makes the two dlls incompatible since they have their own individual copies? (VS 2019)

sooo…yup and yuck

I guess the best solution is to add a new export to python311.dll that takes the target file path and does its own fopen. /MD adds like 6 dlls to the import table.

Yes, that seems likely. That’s going to be true of most of the C standard library - which is why you basically always need to use a shared C runtime.