Welcome, Guest. Please login or register.

Author Topic: Help with OpenLibrary()  (Read 4039 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Help with OpenLibrary()
« on: March 07, 2004, 04:38:38 PM »
Quote
int main(void)
{ if (IntuitionBase = OpenLibary("intuition.library", 33L))
....{DisplayBeep(0L);
....}
..else
....{exit(20);
....}
}


It's a matter of opinion indeed. IMHO embedding '{' and code on the same line is not the clearest possible way (moving code blocks gets tedious then). Not to mention your example doesn't build at all (due to missing casts, and typos).

Here is how I would do it:
Code: [Select]

#include <exec/execbase.h>
#include <dos/dos.h>
#include <intuition/intuitionbase.h>

#include <proto/exec.h>
#include <proto/intuition.h>

struct ExecBase *SysBase;
struct IntuitionBase *IntuitionBase;

int main(void)
{
  int ret = RETURN_FAIL;

  SysBase = *(struct ExecBase **)4;
  IntuitionBase = (struct IntuitionBase *) OpenLibrary(&quot;intuition.library&quot;, 33);
  if (IntuitionBase)
  {
    DisplayBeep(NULL);

    ret = RETURN_OK;

    CloseLibrary((struct Library *) IntuitionBase);
  }

  return ret;
}

 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Help with OpenLibrary()
« Reply #1 on: March 08, 2004, 07:26:15 AM »
Quote
I saw that you includes a lot more header files. Is this really neccessary?

IMO yes.

for struct ExecBase
for RETURN_#?
for struct IntuitionBase

for OpenLibrary()/CloseLibrary()
for DisplayBeep()

Quote
I thought your example was a bit complicated just to show the use of DisplayBeep.

Well, most compilers have autoopen for libraries. But since the intuition.library was explicitly opened already, I decided to make it all startup-code -free. This added some complexity. I also removed some clear un-amiga call: exit().

Also, I wanted to present proper example, that actually builds and works, with most compilers, and without warnings.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Help with OpenLibrary()
« Reply #2 on: March 08, 2004, 09:40:01 AM »
Quote
One more thing though, why do you call exit "an un-amiga call"?

exit() terminates execution of the application, only calling routines added by atexit(), (and executing destructors, if any).

This indeed works ok for standard C applications that use ANSI C filehandles (fopen), memory (malloc) and such only, these are released properly at exit() call.

However, AmigaOS resources are NOT released at all, unless if you expicitly add cleanup routine with atexit().

As such, using exit() in Amiga application is not a good design IMO.

Take this (buggy) example:
Code: [Select]

#include <dos/dos.h>

#include <proto/exec.h>
#include <proto/dos.h>

int main(void)
{
  BPTR fh;

  fh = Open(&quot;t:testfile&quot;, MODE_NEWFILE);

  if (fh)
  {
    struct Library *SomeBase;

    SomeBase = OpenLibrary(&quot;some.library&quot;, 0);
    if (!SomeBase)
    {
      /* BUG: note that fh is not closed */
      exit(20);
    }

    CloseLibrary(SomeBase);

    Close(fh);
  }

  return 0;
}