Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Program crashes

Program crashes

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionios
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eikthrynir
    wrote on last edited by
    #1

    Hi! I have a problem calling a function defined in a dll, like this:

    __declspec( dllexport ) __int8 __cdecl
      freadCharacter( istream &inputStream, char &destinationCharacter )
    {
      inputStream >> resetiosflags( ios::skipws ) >> destinationCharacter;
      inputStream >> setiosflags( ios::skipws );
      if ( inputStream.bad( ) )
        return 0;
    
      return 1;
    }
    

    In the program that uses the dll, I call the function in this manner:

    char c;
    ifstream src( "input.dat" );
    if ( !src )
    {
      cerr << "Error opening file input.dat\n" << flush;
      exit( EXIT_FAILURE );
    }
    
    freadCharacter( src, c );
    

    When I run the program, it crashes. It crashes exactly when trying to execute this line:

    inputStream >> resetiosflags( ios::skipws ) >> destinationCharacter;
    

    Can anybody help me by telling what is wrong with my code? Is it a real problem that I call a function defined in a dll which takes a reference to istream as a parameter? I'm asking this because if I define the function in my application, it works perfectly. Thanks in advance!

    J 1 Reply Last reply
    0
    • E Eikthrynir

      Hi! I have a problem calling a function defined in a dll, like this:

      __declspec( dllexport ) __int8 __cdecl
        freadCharacter( istream &inputStream, char &destinationCharacter )
      {
        inputStream >> resetiosflags( ios::skipws ) >> destinationCharacter;
        inputStream >> setiosflags( ios::skipws );
        if ( inputStream.bad( ) )
          return 0;
      
        return 1;
      }
      

      In the program that uses the dll, I call the function in this manner:

      char c;
      ifstream src( "input.dat" );
      if ( !src )
      {
        cerr << "Error opening file input.dat\n" << flush;
        exit( EXIT_FAILURE );
      }
      
      freadCharacter( src, c );
      

      When I run the program, it crashes. It crashes exactly when trying to execute this line:

      inputStream >> resetiosflags( ios::skipws ) >> destinationCharacter;
      

      Can anybody help me by telling what is wrong with my code? Is it a real problem that I call a function defined in a dll which takes a reference to istream as a parameter? I'm asking this because if I define the function in my application, it works perfectly. Thanks in advance!

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      I would have needed to run some tests, but it may have to do with the fact that the source contains more than one character. I would change the second parameter from a character type to a string type and see what happens. I dought that will work, because it works in your application. That said, it is where I would start. How badly does the program crash? Can you give an error number? It has been a while since I created a DLL, but I looked up the specs and did not see anything wrong. I do not see a return type specified for "freadCharacter", but the compiler would scream if that was missing in the actual code.

      Eikthrynir wrote:

      Is it a real problem that I call a function defined in a dll which takes a reference to istream as a parameter?

      I can think of no reason why that would cause a problem. INTP Every thing is relative...

      E 1 Reply Last reply
      0
      • J John R Shaw

        I would have needed to run some tests, but it may have to do with the fact that the source contains more than one character. I would change the second parameter from a character type to a string type and see what happens. I dought that will work, because it works in your application. That said, it is where I would start. How badly does the program crash? Can you give an error number? It has been a while since I created a DLL, but I looked up the specs and did not see anything wrong. I do not see a return type specified for "freadCharacter", but the compiler would scream if that was missing in the actual code.

        Eikthrynir wrote:

        Is it a real problem that I call a function defined in a dll which takes a reference to istream as a parameter?

        I can think of no reason why that would cause a problem. INTP Every thing is relative...

        E Offline
        E Offline
        Eikthrynir
        wrote on last edited by
        #3

        I run my program under Windows XP Professional. When it crashes, Windows doesn't show me any run-time error number. It just tells me something like this: "Test.exe has encountered a problem and needs to close. We are sorry for the inconvenience." It's the standard Windows message which appears at every program crash. I have tried even to change the second parameter of freadCharacter into an int. I get the same run-time error. I have tested in freadCharacter whether inputStream is open and it is. I thought that maybe that is my problem, but it's not. Thanks!

        J 1 Reply Last reply
        0
        • E Eikthrynir

          I run my program under Windows XP Professional. When it crashes, Windows doesn't show me any run-time error number. It just tells me something like this: "Test.exe has encountered a problem and needs to close. We are sorry for the inconvenience." It's the standard Windows message which appears at every program crash. I have tried even to change the second parameter of freadCharacter into an int. I get the same run-time error. I have tested in freadCharacter whether inputStream is open and it is. I thought that maybe that is my problem, but it's not. Thanks!

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          I have no idea what the problem is, but I can give you the name of the best book I know of. Programming Applicationtions for Microsoft Windows By Jeffrey Richter. I consider Jeffrey Richter a master Windows Guru, he has even taught people at Microsoft on the subject. I do not know if he has written anything directly related to C++ (where I think your problem may lay), but search for his name. Sorry that I did not have time to investigate this furthur myself. John R. Shaw INTP Every thing is relative...

          E 1 Reply Last reply
          0
          • J John R Shaw

            I have no idea what the problem is, but I can give you the name of the best book I know of. Programming Applicationtions for Microsoft Windows By Jeffrey Richter. I consider Jeffrey Richter a master Windows Guru, he has even taught people at Microsoft on the subject. I do not know if he has written anything directly related to C++ (where I think your problem may lay), but search for his name. Sorry that I did not have time to investigate this furthur myself. John R. Shaw INTP Every thing is relative...

            E Offline
            E Offline
            Eikthrynir
            wrote on last edited by
            #5

            I really appreciate and thank you for the time spent in helping me solve this problem. I will search for Mr. Jeffrey Richter's book. Thanks again!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups