Like Tree6Likes

Need pointer help: Interrupt vectors

This is a discussion on Need pointer help: Interrupt vectors within the C Programming forums, part of the General Programming Boards category; I need some pointer help in moving a Turbo C 2.1 (DOS) program to a console application in Windows, using ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Need pointer help: Interrupt vectors

    I need some pointer help in moving a Turbo C 2.1 (DOS) program to a console application in Windows, using the free Borland C++ v5.5 compiler (in C mode, not C++). Specifically, I need to save the system TIMER interrupt vector, install a vector to myTIMER interrupt and, on program exit, restore the original TIMER vector. In Turbo C 2.1, I used the following:
    Code:
    #define TIMER 0X1C
    void interrupt (* oldtimer )( void );   /* <- BCC55: expected ')' error    */
    void main(void) {
        oldtimer = getvect( TIMER );    /* save the Timer vector in oldtimer   */
        setvect(TIMER, MyTimer);        /* install a new vector to MyTimer     */
            /* MyTimer() calls (*oldtimer)(); decrements global MyWaitTimer    */
            /* ... program executes, loading MyWaitTimer with timeouts as reqd */
        setvect(TIMER, oldtimer);       /* Restore the original TIMER vector   */
    }
    In Borland C++ v5.5, there is no getvect()/setvect(). So, how can I save the Timer vector, in a form that I can call it, install a vector to MyTimer, and restore the original Timer vector at program exit? Alterntely, I need a timer that can set a flag on timeout, or a countdown timer that I can load and then check it to see if it has expired.
    Go easy on me, I'm an embedded systems guy with no Windows programming experience, who learned just enough DOS stuff to write a C program, some 20-odd years ago.
    Thanks,
    jhilory

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    There are no such things as interrupt vectors in win32 user space.

    Using threads is a near replacement.
    Code:
    DWORD myTimerWrapper ( void *p ) {
      while ( 1 ) {
        MyTimer();  // do your thing
        Sleep(x);   // how often your DOS TIMER interrupt happened.
      }
    }
    In your main(), you would use CreateThread to set this off doing it's thing while your main gets on with doing what it wants/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, for starts, ditch/lose/burn/trash/abandon turbo C... it's wildly outdated crap that probably should never have gone to market in the first place.

    Next stop trying to write DOS programs on a Windows computer... unless you're using windows 3.1 you're barking totally up the wrong tree. With Win2000 there was a complete shift in programming... DOS no longer ran Windows... Now Windows runs a DOS emulator ... cmd.exe. You are no longer allowed to access hardware directly and that means no more IRQs to mess with...

    So basically unless your hardware is as sorely outdated as your compiler... you're working with a whole new programming paradigm...

    Lets get you up to date here...
    First pick a new 32/64 bit compiler... there's lots of free ones out there, do some looking around. I generally recommend Pelles C which comes with all the headers and libs for both CLI and GUI development under Windows, all the resource editors you'll need and one of the best help files I've ever seen.

    Next get into a tutorial on C-99, once again, lots of them out there... and update your programming skills.

    Finally for the timer functions you can use a Windows timer with a callback function to do your work...
    Timers (Windows)

    Yep... things really have changed THAT much.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    There are no such things as interrupt vectors in win32 user space.

    Using threads is a near replacement.
    Code:
    DWORD myTimerWrapper ( void *p ) {
      while ( 1 ) {
        MyTimer();  // do your thing
        Sleep(x);   // how often your DOS TIMER interrupt happened.
      }
    }
    In your main(), you would use CreateThread to set this off doing it's thing while your main gets on with doing what it wants/
    This will work... but it's a lot better to use an actual Windows timer.

    The concept is pretty simple... set a timer with a Callback function... ( Timers (Windows) ). It runs in it's own thread, so it will not block the executing thread. Each time the timer fires the calback is run, allowing you to do whatever your timed function is. When the callback returns, the main thread is unblocked and off you go...

    (A callback is just a function pointer... )

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    Quote Originally Posted by CommonTater View Post
    This will work... but it's a lot better to use an actual Windows timer.

    The concept is pretty simple... set a timer with a Callback function... ( Timers (Windows) ). It runs in it's own thread, so it will not block the executing thread. Each time the timer fires the calback is run, allowing you to do whatever your timed function is. When the callback returns, the main thread is unblocked and off you go...

    (A callback is just a function pointer... )
    Thanks, this points me in the right direction! I had searched a number of forums, looking for something that would guide me, before deciding to post here. It didn't even occur to me to go to Microsoft's MSDN site (duh!). I'll create a timer with a callback, that should get me going again.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jhilory View Post
    Thanks, this points me in the right direction! I had searched a number of forums, looking for something that would guide me, before deciding to post here. It didn't even occur to me to go to Microsoft's MSDN site (duh!). I'll create a timer with a callback, that should get me going again.
    Did you look into getting a proper compiler? I was not joking about that... Turbo C has become so outdated it's executables won't even run on 64 bit versions of Windows...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > Did you look into getting a proper compiler?
    Did you read they're using Borland 5.5 ?

    Agreed, it too is getting on a bit in terms of age, but at least it is a win32 compiler.

    A good stepping stone - so long as you take another step at some point.
    For one thing, the API calls it supports may be limited to what was in win32 when BC5.5 was published. Do Embarcadero keep the API libraries up to date?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > Did you look into getting a proper compiler?
    Did you read they're using Borland 5.5 ?

    Agreed, it too is getting on a bit in terms of age, but at least it is a win32 compiler.

    A good stepping stone - so long as you take another step at some point.
    For one thing, the API calls it supports may be limited to what was in win32 when BC5.5 was published. Do Embarcadero keep the API libraries up to date?
    It will include win2000 calls up to service pack 1 ... it won't have any of the XP, Vista, Win7 or Windows Server specific calls.
    Salem likes this.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    Quote Originally Posted by CommonTater View Post
    It will include win2000 calls up to service pack 1 ... it won't have any of the XP, Vista, Win7 or Windows Server specific calls.
    The files in the BCC55\Lib are all dated 6/27/2000, so Embarcadero, apparently, hasn't done any updating. I had no success in adding a timer to a simple, working "Hello World" console app. This may have been improper syntax on my part. I had tried to add the timer to delay a few seconds after printing "Hello...", before exit. I was trying to follow the syntax given on Microsoft's MSDN site SetTimer function (Windows). I decided to follow CommonTater's advice and ditch the old compiler and look for a newer one. I installed Visual Studio Express 2010 and downloaded its "Hello World" example from the MSDN site. It wouldn't run that! Error in converting.

    So, I decided to take CommonTater's suggestion and look at the Pelles C compiler. I have installed it, and compiled its "Hello World" example, and it runs with no problems. Next, I will try to add a timer, before going further with porting my old Turbo C 2.01 application. So far, the Pelles C looks to be lean-and-mean, compared to Visual C++ Express.

    Here is the syntax for Set/Kill Timer from the MSDN site:
    Code:
     
    UINT_PTR WINAPI SetTimer(
      __in_opt  HWND hWnd,
      __in      UINT_PTR nIDEvent,
      __in      UINT uElapse,
      __in_opt  TIMERPROC lpTimerFunc
    );
    
    BOOL WINAPI KillTimer(
      __in_opt  HWND hWnd,
      __in      UINT_PTR uIDEvent
    );
    Does anyone know if this is the proper syntax to use with Pelles C (Console mode)?

    Thanks, jhilory

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Yes, any samples from MSDN should run on Pelles C with no problem. Check the Pelles C help file for information on the functions it supports and there implementations. Additionally, if you are going to mix windows functions with console programming you may find some value with adrian's win32 tutorials and when you are ready to shift into windows programming the Forger's Win32 Tutorials are an excellent resource.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    // timer with callback
    #define WIN32_DEFAULT_LIBS
    #include<windows.h>
    #include <stdio.h>
    
    void CALLBACK TimeProc(HWND wnd, UINT msg, UINT event, DWORD time)
      { 
        printf("Hello world... on a timer!\n");
        Beep(500,100);
      }
    
    
    int main (void)
      { MSG msg;
    
    
        if (! SetTimer(NULL,0,1000,TimeProc) )
          printf("Well that didn't work");  
    
       
        while( GetMessage(&msg,NULL,0,0) )
          DispatchMessage(&msg);
    
     
      return 0;
    }
    Please note ... the message dispatcher loop has to run in order for the timer to execute.
    This means your code will have to also contain a windows MsgProc() or *at least* idle in this loop.

    I would also suggest you experiment with Salem's method from message #2... use the one that works best for you.
    Last edited by CommonTater; 09-22-2011 at 02:59 AM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Yes, any samples from MSDN should run on Pelles C with no problem.
    That's not entirely true... the majority will require some special settings in the Project Options (microsoft extensions and calling convention) and a few are using DirectX, etc. which may require additional downloads to run.

    But yes... Pelles C is a keeper!

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    That's not entirely true... the majority will require some special settings in the Project Options (microsoft extensions and calling convention) and a few are using DirectX, etc. which may require additional downloads to run.

    But yes... Pelles C is a keeper!
    Thanks for clearing that up. You see I am a lowly Visual Studio user.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Thanks for clearing that up. You see I am a lowly Visual Studio user.
    My condolences...

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    Quote Originally Posted by CommonTater View Post
    Code:
    // timer with callback
    #define WIN32_DEFAULT_LIBS
    #include<windows.h>
    #include <stdio.h>
    
    void CALLBACK TimeProc(HWND wnd, UINT msg, UINT event, DWORD time)
      { 
        printf("Hello world... on a timer!\n");
        Beep(500,100);
      }
    
    
    int main (void)
      { MSG msg;
    
    
        if (! SetTimer(NULL,0,1000,TimeProc) )
          printf("Well that didn't work");  
    
       
        while( GetMessage(&msg,NULL,0,0) )
          DispatchMessage(&msg);
    
     
      return 0;
    }
    Please note ... the message dispatcher loop has to run in order for the timer to execute.
    This means your code will have to also contain a windows MsgProc() or *at least* idle in this loop.

    I would also suggest you experiment with Salem's method from message #2... use the one that works best for you.
    Thanks. Using the Pelles C compiler, I am finally starting to get some positive results! I was able to run the MSDN sample Using Waitable Timer Objects (Windows), but only after I discovered that I had to turn on the Microsoft extensions (Project Options | Compiler tab | Checkbox for "Enable Microsoft Extensions"). Next, I will try your code, above. Then, I'll look into the suggestion made by Salem in Msg #2 of this thread. That looks to be the most promising solution for me, right now, because I can use that to run the same countdown timers that are currently used in my Turbo C 2.01 program. That will mean fewer changes, and the less that I have to change, right now, in order to get something working, the better.

    Thanks to CommonTater, AndrewHunter and Salem for your helpful suggestions and insights. You've helped me get past a major hurdle! Also, thanks to the other contributors to this forum, reading your postings has been very helpful.

    jhilory

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors in vectors - pointers and iterators
    By fisherking in forum C++ Programming
    Replies: 8
    Last Post: 07-27-2010, 10:34 AM
  2. Help with writing an interrupt
    By David_O in forum C Programming
    Replies: 5
    Last Post: 02-17-2010, 11:26 AM
  3. ASM interrupt and others
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 06-08-2002, 08:18 PM
  4. interrupt
    By juandy in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2002, 09:24 AM
  5. Interrupt???
    By GiSH in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2001, 12:27 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21