Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Looks like a very simple problem:

VS2017 15.9.13 (The latest version today)

My main program, followed by a DLL:

C++
#include <windows.h>
#include <stdio.h>
#include <time.h>

extern "C" int WINAPI wWinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
	time_t now = time ( 0 );
	FILE *log = _wfopen ( L"test.dat", L"wt,ccs = UNICODE" );

	if ( HMODULE hdll = LoadLibrary ( L"Dll1" ) )
	{
		typedef int ( _cdecl *typeCreate )(int, FILE *);
		typeCreate Create = ( typeCreate ) GetProcAddress ( hdll, "MySub" );
		if ( Create )
		{
			fwprintf ( log, L"Test 2 at %s", _wctime ( &now ) ); fflush ( log );
			int nn = ftell ( log );
			int n = 21;
			nn = Create ( n, log );
		}
	}
    fclose (f);
	return EXIT_SUCCESS;
}

C++
#include <stdio.h>
extern "C" __declspec (dllexport)  int MySub ( int n, FILE *f )
{
	int nn = 0;
	if ( f )
	{
		nn = ftell ( f );
		fwprintf ( f, L"Number %d\n", n );
		fflush ( f );
		nn = ftell ( f );
	}
	return nn;
}


Running this program, after changing and resetting some project settings fails with:

Debug assertion failed!
Program: C:\xxxxxx\Dll1\Debug\Project1.exe
File: minkernel\crts\ucrt\src\appcrt\lowio\lseek.cpp
Line: 103

Expression: _osfile(fh) & FOPEN

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)


What I have tried:

When this project is freshly created it works as expected. After making (and reverting) some project settings, like specifying Windows 8.1 as the Windows SDK version, the program keeps failing on the ftell statement in the DLL, no matter what I try.

I have the complete project available as a zip file, but I don't see a way to upload this file.

Hoping for a good suggestion,

Henk B
Posted
Updated 1-Jul-19 22:56pm
v3

The problem was in the project settings, specifically the runtime library. For the DLL is was multi-threaded debug DLL (/MDd), while for the main progam it was multi-threaded debug (/MTd). After setting both projects to /MDd all is well again.

Henk B
 
Share this answer
 
Comments
丹京 16-May-22 6:04am    
thanks, this solved my problem.
I think it comes from some problem before, so I dont like the you dont use full pathes. This can be the cause of your problem. Other causes may be rights issues or handle already in use.

Ap you better check the return codes for sucess codes of the called functions before. You may get a -1 and had moved on.
 
Share this answer
 
Comments
duxburydutch 30-Jun-19 14:21pm    
Thanks for your response. I noticed that the problem does not exist when everything is compiled as X64. But when I switch back to Win32 (x86) the problem reappears.
Sorry for hiding the complete path, but I don't see why that could mask or alter the problem. There are no rights issues, because the fwprintf and ftell in the main program work correctly.
Henk B

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900