CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Join Date
    Sep 2010
    Posts
    15

    CreateFile Failed problem

    Okay, So I am new to programming and am trying to transfer data from a machine via RS232 to an "adapter" program that allows connection to a "client or agent" program that sends the data to the web(localhost) in .xml format. Basically a socket setup. Both programs seem to be working fine, but when I connected the client program to the "adapter", it says CreateFile Failed.

    When I run the first program, executeable window pops up and says:
    Server Started, waiting on port 7878

    Then when I connect to the localhost it says:
    Server Started, waiting on port 7878
    Connected to: 127.0.0.1 on port 2816
    CreateFile Failed

    I am pretty sure the port numbers do not need to match since I am trying to view this data on the local host. 7878 is just the default port number in the adapter program.

    I am not sure which section of code to look at so can someone tell me where to look to fix this problem? I am not sure if it is an .xml proble, schema problem, or what the problem is. Thanks for any advice or help you can give me.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: CreateFile Failed problem

    From MSDN article CreateFile
    Return Value

    If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot.

    If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
    Victor Nijegorodov

  3. #3
    Join Date
    Sep 2010
    Posts
    15

    Re: CreateFile Failed problem

    Is there a problem with this file?

    serial.win32.cpp-external dependency file

    Code:
    bool Serial::connect()
    {
      if (mConnected)
      {
        printf("You are already connected, please disconnect first\n");
        return false;
      }
      
      COMMTIMEOUTS cto;
      DCB dcb;
      char parity, stop_bits;
      int speed;
    	 	 
      if (mFd != INVALID_HANDLE_VALUE && !CloseHandle(mFd)){
        printf("Can't close comm port");
        return false;
      }
      
      mFd = CreateFileA(mDevice,
    		    GENERIC_READ|GENERIC_WRITE,
    		    0,
    		    NULL,
    		    OPEN_EXISTING,
    		    0,
    		    NULL);
    	 
      if(mFd == INVALID_HANDLE_VALUE) {
        char port[64];
        strcpy(port, "\\\\.\\");
        strcat(port, mDevice);
        mFd = CreateFileA(port,
    		      GENERIC_READ|GENERIC_WRITE,
    		      0,
    		      NULL,
    		      OPEN_EXISTING,
    		      0,
    		      NULL);
    		 
        if(mFd == INVALID_HANDLE_VALUE) {
          printf("CreateFile Failed \n");
          return false;
        } 
      }
    	 
      if(!SetupComm(
           mFd,	// handle of communications device
           1024,	// size of input buffer
           1024	// size of output buffer
           )
        ){
        printf("SetupComm failed \n");
        CloseHandle(mFd);
        return false;
      }
    	 
      // verify baudrate and pairty
    	 
      // set DCB
      switch (mBaud) {
      case 110:
        speed = CBR_110;
        break;
      case 300:
        speed = CBR_300;
        break;
      case 600:
        speed = CBR_600;
        break;
      case 1200:
        speed = CBR_1200;
        break;
      case 2400:
        speed = CBR_2400;
        break;
      case 4800:
        speed = CBR_4800;
        break;
      case 9600: 
        speed = CBR_9600;
        break;
      case 19200:
        speed = CBR_19200;
        break;
      case 38400:
        speed = CBR_38400;
        break;
      case 57600:
        speed = CBR_57600;
        break;
      case 115200:
        speed = CBR_115200;
        break;
      default:
        speed = CBR_9600;
        printf("WARNING Unknown baud rate %d for %s (B9600 used)\n",
    	   mBaud, mDevice);
      }
    
      if (strncmp(mParity, "none", 4) == 0) {
        parity = NOPARITY;
      } else if (strncmp(mParity, "even", 4) == 0) {
        parity = EVENPARITY;
      } else {
        /* odd */
        parity = ODDPARITY;
      }
    
      /* Stop bit (1 or 2) */
      if (mStopBit == 1)
        stop_bits = ONESTOPBIT;
      else /* 2 */
        stop_bits = TWOSTOPBITS;
    
      memset(&dcb,0,sizeof(dcb));
      dcb.DCBlength = sizeof(dcb);
      dcb.BaudRate = speed;
      dcb.fBinary = TRUE;
      dcb.StopBits = stop_bits;
      dcb.Parity = parity;
      dcb.ByteSize = mDataBit;
    	 
    
      dcb.fDtrControl = DTR_CONTROL_DISABLE;
      dcb.fOutxCtsFlow = FALSE ;	 
      dcb.fRtsControl = RTS_CONTROL_DISABLE;
    	 
      // setup software flow control
      dcb.fInX = dcb.fOutX = 0;
    		 
      memset(&cto, 0, sizeof(cto));
      cto.ReadIntervalTimeout = 100;
      cto.ReadTotalTimeoutMultiplier = 1;
      cto.ReadTotalTimeoutConstant = 1000;
      cto.WriteTotalTimeoutMultiplier = cto.ReadIntervalTimeout;
      cto.WriteTotalTimeoutConstant = 1000;	 
    
      if(!SetCommTimeouts(mFd, &cto)){
        printf("SetTimeouts failed ");
    	CloseHandle(mFd);
        return false;
      }	 
    	 
      if(!SetCommState(mFd, &dcb)){
        printf("SetCommState failed \n");
    	CloseHandle(mFd);
        return false;
      }
    	 
      mConnected = true;
      
      return true;
    }
    
    bool Serial::disconnect()
    {
      if (mConnected)
      {
        CloseHandle(mFd);
        mConnected = false;
    	mFd = INVALID_HANDLE_VALUE;
        return true;
      }
      else
      {
        return false;
      }
    }
    
    int Serial::read(char *aBuffer, int aLen)
    {
      int ret;
      DWORD bytes_read;
      if(ReadFile(mFd, aBuffer, aLen, &bytes_read, NULL) ) 
      {
        ret = bytes_read;
      }
      else
      {
        DWORD errors;
        COMSTAT status;
        
        ClearCommError(mFd, &errors, &status);
        ret = -1;
      }
    
      return ret;
    }
    
    int Serial::write(const char *aBuffer, int aLen)
    {
      DWORD bytes_written;
      int ret;
      if(WriteFile(mFd, aBuffer, aLen, &bytes_written, NULL) ) 
      {
        ret = bytes_written;
      }
      else
      {
        DWORD errors;
        COMSTAT status;
        ClearCommError(mFd,&errors,&status);
        ret = -1;
      }
      return ret;
    Last edited by wklove2003; September 7th, 2010 at 11:06 AM.

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: CreateFile Failed problem

    Please edit your post adding Code tags around code snippets. and don|t forget thr indentations.
    Also read Announcement: Before you post....
    Victor Nijegorodov

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: CreateFile Failed problem

    Please add code tags.

    Also what does GetLastError() tell you that Victor suggested in reply #2?

  6. #6
    Join Date
    Sep 2010
    Posts
    15

    Re: CreateFile Failed problem

    How do I call the GetLastError()? Sorry, this is all very new to me

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CreateFile Failed problem

    Quote Originally Posted by wklove2003 View Post
    How do I call the GetLastError()? Sorry, this is all very new to me


    Seriously, how can you not know how to call a function when the code you posted is full of other function calls?? What about that call to CreateFile(), strcpy, etc.? You seem to be doing fine calling those functions.

    GetLastError() is just another function you call when you get an error. Just call the function and see what the value that is returned to you is. That value is the error condition.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 7th, 2010 at 11:18 AM.

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: CreateFile Failed problem

    Quote Originally Posted by wklove2003 View Post
    How do I call the GetLastError()? Sorry, this is all very new to me
    Didn't you read the documentation about GetLastError?
    Didn't you try to search for CreateFile GetLastError example?
    Well, very simple:
    Code:
      mFd = CreateFileA(mDevice,
    		    GENERIC_READ|GENERIC_WRITE,
    		    0,
    		    NULL,
    		    OPEN_EXISTING,
    		    0,
    		    NULL);
    	 
      if(mFd == INVALID_HANDLE_VALUE)
     {
          DWORD dwError = GetLastError();
          // some code to output or display error message
          ... 
    }
    Victor Nijegorodov

  9. #9
    Join Date
    Sep 2010
    Posts
    15

    Re: CreateFile Failed problem

    I am not getting any message, they are running the same way. No errors pop up in the call stack for either program. Nothing happens when I inserted the GetLastError function

  10. #10
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: CreateFile Failed problem

    GetLastError does NOT display any message. It only returns you the error code!
    It's up to you how to output or display error message.
    Victor Nijegorodov

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: CreateFile Failed problem

    Quote Originally Posted by wklove2003 View Post
    I am not getting any message, they are running the same way. No errors pop up in the call stack for either program. Nothing happens when I inserted the GetLastError function
    Step through your code in a debugger and look at the dwError value.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CreateFile Failed problem

    Quote Originally Posted by wklove2003 View Post
    I am not getting any message, they are running the same way. No errors pop up in the call stack for either program. Nothing happens when I inserted the GetLastError function
    The GetLastError() function returns to you a value -- that value is the error code. You are to do with that error code whatever you need to do with it -- log it, display it in your own window, inspect it, who knows.

    You are under the misconception that GetLastError() should only be used when you have an error in your program. That is false. It should be called every time you call a Windows API function and that function fails. It should be part of your program, and right now, it isn't. You are calling many Windows API functions in the code you posted, and you don't call GetLastError() even once for when they fail.

    Yes, you are checking for a return value from the functions you're calling themselves, but you must call the "second line of defense", which is the GetLastError() function to tell you exactly why the function failed. In addition, you should also learn to use FormatMessage() in conjuction with GetLastError(). Therefore your code is not really complete.

    Secondly, if you think about it for a moment, why would an API function such as GetLastError() want to display error boxes? That is up to the programmer and application as to how to display or record errors. What if I don't want error messages popping up all over the place, and instead just want to log the error number in a file?

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Sep 2010
    Posts
    15

    Re: CreateFile Failed problem

    Ok, so when I try to step through in debug and putting @err,h in Watch window I get:

    Watch:
    @err,h CXX0026: Error: bad format string

    CallStack:
    > haas.exe!main(int aArgc, char * * aArgv) Line 40 C++
    haas.exe!__tmainCRTStartup() Line 555 + 0x17 bytes C
    kernel32.dll!7c817077()
    [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
    Code:
    #include "internal.hpp"
    #include "haas_adapter.hpp"
    #include "server.hpp"
    #include "string_buffer.hpp"
    
    int main(int aArgc, char *aArgv[])
    {
      int port = 7878;
      int i = 0;
      bool debug = false;
      bool positions = false;
    
      while (aArgc > 0 && aArgv[i][0] == '-')
      {
        if (aArgv[i][1] == 'd')
          debug = true;
        else if (aArgv[i][1] == 'p')
          positions = true;
        else
        {
          printf("Invalid option: %s\n", aArgv[i]);
          printf("Usage: %s [-dp] <Serial_COM> [port]\n", aArgv[0]);
          exit(1);
        }
    
        i++;
        aArgc--;
      }
    
      if (aArgc > 2)
        port = atoi(aArgv[i + 1]);
        
      /* Construct the adapter and start the server */
      HaasSerial *serial = new HaasSerial(aArgv[i], 19200, "none", 7, 1, debug);
      HaasAdapter adapter(port, serial, positions);
      adapter.startServer();
      
      return 0;
    }
    Line 40 is:
    int main(int aArgc, char *aArgv[]) Line 39
    { Line 40

    Any Ideas?

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CreateFile Failed problem

    Quote Originally Posted by wklove2003 View Post
    Ok, so when I try to step through in debug and putting @err,h in Watch window I get:
    ???

    What is all of this stuff? How hard is it to call the GetLastError() function and inspect the error value returned??? If you don't want to ignore what we're telling you, then why post?

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Sep 2010
    Posts
    15

    Re: CreateFile Failed problem

    Sorry, I just havent been able to get a GetLastError() function to return anything. That is why I started trying other things. Seems to only cause problems when I try putting something in there. When I do not get any problems, I also do not get any result so I am not sure what I am doing wrong.

    I am a complete newb to programming, but unfortunately, have been handed a project that is dealing with a fairly complex program that was downloaded. So most of the code I have posted, I did not actually do. Just trying to get this program to run properly ASAP

Page 1 of 3 123 LastLast

Tags for this Thread

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