Page 1 of 1

need some C/C++ help (solved)

Posted: Sat Feb 23, 2019 9:06 am
by Memblers
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

Posted: Sat Feb 23, 2019 10:25 am
by Dwedit
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

Posted: Sat Feb 23, 2019 1:29 pm
by samophlange
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

Posted: Sun Feb 24, 2019 5:26 am
by Memblers
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)

Posted: Sun Feb 24, 2019 10:01 am
by Dwedit
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.