+6  A: 

bytes_written needs to be an address of a variable; the compiler wouldn't compile the statement you posted.

Likewise, "OVERLAP" doesn't make sense.

Did you check whether CreateFile succeeded?

What's in write_buf when you call strlen on it?

Try copy-and-pasting the actual code that you're using.

Also that doesn't seem like a very good/informative sample that you're using. Try http://www.robbayer.com/files/serial-win.pdf and http://msdn.microsoft.com/en-us/library/ms810467.aspx

Also start with the sample program from the Microsoft site, test it before you modify it (to check that it's working on your machine) and then modify it.

ChrisW
First of all, I mention that the code runs, which means that it compiled. Secondly, I added some code that was trimmed off the initial post. Thirdly, thanks for the links.
thepocketwade
Now that you've added the declaration of bytes_written, I see the problem: it's a pointer, and you haven't initialized it before you use it. Ditto the OVERLAP pointer (you haven't initialized it before you used it either). Also, a failure to open the COM port would mean that the handle contained a value of INVALID_HANDLE_VALUE (not 0).
ChrisW
ChrisW
Thanks for that, it's little things like that that keep tripping me up working on this code. Making the changes you suggested allowed me to see I was getting an error code of 997, which I'm working on resolving now. Thanks.
thepocketwade
A: 

When you call SetCommState is the return value 0? It could be erroring out which could be causing more problems.

Also, have you stepped through line by line to make sure that it is the WriteFile call that crashes it?

Lastly, you could have some antivirus or other software hooking into your app causing these problems (look for unknown dll's loaded in your module list).

Joshua
The return value is 0. On a second look, I replaced the WriteFile line with a ReadFile call, and it also crashed, but without either the code runs fine.
thepocketwade
I'm new to Visual Studio, and I still haven't figured out where the module list is.
thepocketwade
When you're debugging, click on the Debug menu, Windows -> Modules (Ctrl + D, M if you're using default shortcuts)
Joshua