我在写入流时遇到问题,将其作为FILE *传递给DLL中包含的函数。编译器:Visual C ++,2005年至2015年的所有版本。精确的错误各不相同; VS 2015版本如下。对于Win32和x64目标,问题是相同的。
尝试解决问题,请考虑以下功能:
#include <stdio.h>
void TestPrintf(FILE *TestFile)
{
fprintf(TestFile, "Hello World");
}
现在,另一个函数将以下列方式调用上述函数:
#include <stdio.h>
void TestPrintf(FILE *TestFile);
void AnyFunction( void )
{
FILE *TestFile;
fopen_s( &TestFile, "PrintTest.txt", "wt");
TestPrintf(TestFile);
}
第一个函数是DLL的一部分。取决于中的设置 在编译DLL和包含调用者的程序期间的属性/ C / C ++ /代码生成,我收到以下错误消息:
Caller编译为多线程调试(静态):
为多线程调试(静态)或多线程调试DLL编译的DLL:
Debug assertion failed
File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp
Line: 980
Expression: __acrt_first_block == header
为Multithread(静态)编译的DLL:
Debug assertion failed in the same file as before, but now
Line: 892
Expression: is_block_type_valid(header->block_use)
为多线程DLL编译的DLL:
Again another variant of the same. Now it is:
Line: 888
Expression: _CrtlsValidHeapPointer(block)
Caller编译为多线程调试DLL:
为Multithread(静态)或Multithread Debug(静态)编译的DLL:
same messages as above
为多线程DLL编译的DLL:
Yet another variant of the same. Now it is:
HEAP[FilePrintTest.exe]: Invalid address specified to RtlValidateHeap(
01060000, 0107B7A0 )
为多线程调试DLL编译的DLL:
This is error-free.
Caller编译为Release(多线程或多线程DLL):
all four versions of the DLL go error-free.
VS版本之间的区别似乎是VS 2015首先按预期写入输出并在之后崩溃,而VS 2005在写入输出时已经崩溃。
如果函数 TestPrintf 不在DLL中,而是在静态库中,则没有这样的问题(当然,会有关于库冲突的链接器消息,但测试程序仍然有效) )。但是,不幸的是,我确实需要DLL中的写入功能,我必须能够从任何地方调用它。
以上所有内容也适用于调用者在控制台程序中并且流是 stdout 的情况。
所以我的问题是:我身边有一个愚蠢的错误吗?如果没有,有没有办法解决这个问题?
我很感激你的建议! 马丁
答案 0 :(得分:0)
请查看fopen_s
语法。您正在使用指针前面的FILE * *
传递&
。
尝试删除&
符号:
fopen_s(TestFile, /*...*/);
您的TestFile
已经是指针:
FILE * TestFile;
您的使用情况与cppreference.com
提供的语法不符