Hello, I have such a problem. I put windows7 on the virtual machine, in the virtual machine I set the com1 port setting to associate as the 1.txt file (there it was possible to do this) in my ubunt. In visual studio 2015 wrote a program:

#include <windows.h> #include <iostream> #include <io.h> #include <stdio.h> #include <fcntl.h> using namespace std; int main(void) { HANDLE h; h = CreateFile(L"COM1", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (h != INVALID_HANDLE_VALUE) { cout « "OkOpen" « endl; } else { cout « "Bad" « endl; getchar(); return 0; } char buf[25]; memset(buf, 0, 25); strcpy(buf, "hell is my name"); DWORD wtn; OVERLAPPED ol; WriteFile(h, buf, 25, &wtn, &ol); cout « wtn « endl; FlushFileBuffers(h); CloseHandle(h); getchar(); return 0; } 

But he does not write anything to the 1.txt file. What could be the problem, while the prog issued OkOpen Thanks in advance.

    1 answer 1

    Problem number one - you do not check the result returned by the WriteFile() function. In case of an error, it will return false , and GetLastError() will return the error code itself.

    Further, you for some reason pass a pointer to the OVERLAPPED structure, although you create the file without the FILE_FLAG_OVERLAPPED flag and there are no hints that you want to work with the file asynchronously. At the same time, you pass garbage in the structure.

    Total, the code should be so

     if (!WriteFile(h, buf, 25, &wtn, NULL)) cout << "Error writee file. Code: " << GetLastError() << endl; 
    • Checked the record occurred. You can close the question: the error was that it did not configure the transmission parameters through the com-port. Article blablacode.ru/programmirovanie/392 helped - George Kasparyants