need some C/C++ help (solved)

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
need some C/C++ help (solved)
by on (#234926)
I'm having a dumb problem, probably with a dumb cause, but I haven't been able to figure it out.

I'm working on UART emulation code in C, so I can emulate the USB adapter from the Cheapocabra devkit. I was planning to post it once I get it tested and cleaned up, but for now I'm kinda stuck, so hopefully someone can help spot the problem. The code in async.c is isn't really in a usable state, it's kinda hard to get it working when I can't properly test it though..

Normally I only work with C, so maybe it's C++ related? What I've got is some code that works every time when I compile and run it by itself with a very simple main() function. Now I'm trying to add it into Nintendulator for testing, and it fails every time. serial_open() in serial_windows.c is where it fails. Works every time in a stand-alone test (C compiler), fails every time built with Nintendulator (C++). Checking GetLastError() returns code 2, which is "file not found". Stepping through it with a debugger hasn't been very enlightening, just windows kernal stuff viewed as a disassembly.. yeah.. not sure where to go from there.

Is there some reason why this would fail when being used in Nintendulator, but works fine in my own little "main.c" file?
-header files are wrapped in extern C{}
-using visual studio 2017
-I'm calling async_open() from Nintendulator.cpp, just before it enters the main loop
-serial.h, serial_windows.c, serial_linux.c is not my code, they're some simple libraries I've been using and modifying
Re: need some C/C++ help
by on (#234927)
Seems that it calls CreateFile on "\\.\COM1" or something like that. A good place to stick breakpoints is right around CreateFile to see if the filename is okay.
Re: need some C/C++ help
by on (#234935)
The first thing to do would be to ensure that you're passing the exact same parameters to CreateFile in both situations. If that is the case, maybe Nintendulator is also accessing that resource in an exclusive way? Or it may be doing other things (including loading in DLLs) which changes the context in which that function operates. I would be surprised if this were due to Nintendulator being a C++ application.
Re: need some C/C++ help
by on (#234956)
Thanks for the tips, I still couldn't figure it out in Nintendulator. It must be somehow using the wrong character format for the filename, doesn't seem like there could be anything else.

I've resolved the problem for now by moving over to FCEUX. Builds fine and opens the COM port, I'm not stuck anymore. UART emulation coming soon.
Re: need some C/C++ help (solved)
by on (#234962)
Oh wait, yeah, forgot that "CreateFile" was a macro and not a function.
CreateFileA and CreateFileW are the real functions, and defining UNICODE can break that.