CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: WriteFile(

  1. #1
    Join Date
    May 1999
    Location
    CA
    Posts
    188

    WriteFile(

    Ok, here is the situation. I am using TAPI to open a phone line. It opens, dials and connects just fine. When it comes time to write data I am using the following code:


    HANDLE commFile=(HANDLE)getModemHandle();

    /* commFile is now a handle to the comm port */
    /* hopefully we can send data using this */

    char data[]="stuff to write, IE all my data goes here";
    DWORD written;

    if(!WriteFile(commFile,data,strlen(data),&written,NULL))
    {
    error=GetLastError();
    }
    /* the flippin data is written */
    /* hang up */




    commFile comes back from getModemHandle with a value. When it gets to WriteFile(), WriteFile returns with an error "Invalid handle". This doesn't make sense to me unless TAPI is lying and not giving me the modem handle.

    Has anyone else had experience in this?

    Thanks.




  2. #2
    Join Date
    Aug 1999
    Location
    Dresden / Germany
    Posts
    51

    Re: WriteFile(

    We had a similar problem with WinSockets.
    We found in the MSVC doc that WriteFile can only write into a socket you use the OVERLAPPED option because this is a asynchronous operation.
    I hope that this is a useful hint...



  3. #3
    Join Date
    May 1999
    Location
    CA
    Posts
    188

    Re: WriteFile(

    Thank you. This would explain much.


  4. #4
    Join Date
    Feb 2000
    Posts
    3

    Re: WriteFile(

    You said that you had a similar problem with WinSockets!
    I started from same type of problem! Not using overlapped options I get err = 87 ERROR_INVALID_PARAMETER .
    Using overlapped options I get err =6 ERROR_INVALID_HANDLE

    Do you see any errors, or do you have any useful hints?

    Thanks


    DWORD err;
    OVERLAPPED ovr;

    SOCKET m_hSocket = socket(AF_INET, SOCK_STREAM, 0);

    SOCKADDR_IN adr;
    adr.sin_addr.s_addr = 0;
    adr.sin_family = AF_INET;
    adr.sin_port = htons( 1234 );

    rc = bind(m_hSocket,(const struct sockaddr *)&adr, sizeof(adr) );
    rc = listen(m_hSocket,MB_TOPMOST);

    rc = sizeof(adr);
    SOCKET so2 = accept(m_hSocket,(struct sockaddr *)&adr, &rc ); //doesn't block here, OK
    //m_lpB,m_lLength are setted in others methods
    if (WriteFile(so2 , m_lpB, m_lLength, &dwWritten, &ovr) == 0)
    {
    err = GetLastError();
    ::MessageBox(::GetTopWindow(::GetDesktopWindow()), "Error on Write data", "Server", MB_ICONERROR);
    }





  5. #5
    Join Date
    Apr 2000
    Location
    Munich
    Posts
    212

    Re: WriteFile(

    Hi,

    at least you have to initialize teh OVERLAPPED struct:


    OVERLAPPED ovr = {0}; // zero mem somehow
    EVENT ev = ::CreateEvent( NULL, FALSE, FALSE, "MyOverlappedEvent");
    ovr.hEvent = ev;




    And note that WriteFile() can deliver an error code if it is working "overlapped". Check then GetLastError() - it must be sth. like IO_PENDING.

    Hope that helps.
    bye!



    ~christian

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured