Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline jonssonjTopic starter

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Posts: 254
    • Show only replies by jonssonj
Help with OpenLibrary()
« on: March 07, 2004, 12:19:14 PM »
Hello!

Im reading my RKRM Libraries manual and Im trying to do the first sample, that uses the DisplayBeep() function.

Im using Storm C V4 GUI but with the Storm C V3 Compiler.

My program looks like this:

#include
#include

/* Get storage for the library base. The base Name must be IntuitionBase */
struct IntuitionBase *IntuitionBase;

int main()
{
 IntuitionBase = (struct IntuitionBase *) OpenLibrary("Intuition.library", 33L);
 if (IntuitionBase) {
  DisplayBeep(0L);
  CloseLibrary(IntuitionBase);
 } else {
  exit(20);
 }  
}

When I try to compile this program I get the following error

Error: In function `int main()´:
    type `Library´ is not a base type for type `IntuitionBase´
OpenLib.cpp Line 11

Can someone tell what the heck is going on?

Its frustrating to get stuck on the first example... :)

/Jörgen
 

Offline Jettah

  • Full Member
  • ***
  • Join Date: Nov 2003
  • Posts: 115
    • Show only replies by Jettah
Re: Help with OpenLibrary()
« Reply #1 on: March 07, 2004, 01:51:49 PM »
Hi Jorgen,

Try and switch to gcc compiler in favor of Storm V3.

I've tried your little example with the gcc compiler: it works! But when switching to Storm C3 all I get is an error in line 12: Illegal argument: CloseLibrary(IntuitionBase); This one is to be resolved like this: CloseLibrary(Library *)IntuitionBase); Under Storm C3 it compiles and yes your screen flashes! On the other hand flashing the screen to draw a user's attention is a bad, bad practice. There are far more userfriendly ways to communicate with the user when it comes to drawing attention.

Keep going on! Working from simple programs onwards is the best way of teaching yourselves how to program and don't forget 'borrowing' code from someone else. It may come in very, very handy at times...

Cheers mate. Oh, ehm, one more remark, but admittedly very personel is the style of writing. Personally I prefer to do it this way:

int main(void)
{ if (IntuitionBase = OpenLibary("intuition.library", 33L))
....{DisplayBeep(0L);
....}
..else
....{exit(20);
....}
}

It makes it far more readable and therefore easier to maintain. I've gone sofar as to indenting every next level by only two positions, while 4 is considered default. But as I said it is just a matter of personal taste.

Regards,

Tjitte


P.S. Indentation is not shown correctly, as blanks at the beginning of a line are removed. Therefore I've replaced them with dots.
Sometimes I wish I was Mt Vesuvius: laying on my back in the sun while smoking a bit and everybody seeing me would say: \\"Look! He\\\'s active!\\" (author unknown to me)
 

Offline CD32Freak

Re: Help with OpenLibrary()
« Reply #2 on: March 07, 2004, 03:50:36 PM »
Code: [Select]

int main(void)
{ if (IntuitionBase = OpenLibary("intuition.library", 33L))
    {DisplayBeep(0L);
    }
  else
    {exit(20);
    }
}

Use the code tag ( symbol) and then it will be displayed correctly :-)
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Help with OpenLibrary()
« Reply #3 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 pjhutch

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 452
  • Country: england
  • Gender: Male
  • Amiga user and developer
    • Show only replies by pjhutch
    • http://www.pjhutchison.org
Re: Help with OpenLibrary()
« Reply #4 on: March 07, 2004, 04:39:36 PM »
CloseLibrary requires a pointer of type Library. So you need to cast IntuitionBase as a library.
e.g.
CloseLibrary((struct Library *)IntuitionBase);

Then it should compile ok.
 

Offline Jettah

  • Full Member
  • ***
  • Join Date: Nov 2003
  • Posts: 115
    • Show only replies by Jettah
Re: Help with OpenLibrary()
« Reply #5 on: March 07, 2004, 04:49:37 PM »
@Piru

One is never too old to learn... ;->

Regards,

Tjitte

Sometimes I wish I was Mt Vesuvius: laying on my back in the sun while smoking a bit and everybody seeing me would say: \\"Look! He\\\'s active!\\" (author unknown to me)
 

Offline Jettah

  • Full Member
  • ***
  • Join Date: Nov 2003
  • Posts: 115
    • Show only replies by Jettah
Re: Help with OpenLibrary()
« Reply #6 on: March 07, 2004, 04:51:01 PM »
You are right. I forgot. I'm getting old. BTW, what was this all about? ;->

Cheers,

Tjitte
Sometimes I wish I was Mt Vesuvius: laying on my back in the sun while smoking a bit and everybody seeing me would say: \\"Look! He\\\'s active!\\" (author unknown to me)
 

Offline Jettah

  • Full Member
  • ***
  • Join Date: Nov 2003
  • Posts: 115
    • Show only replies by Jettah
Re: Help with OpenLibrary()
« Reply #7 on: March 07, 2004, 04:53:06 PM »
@CD32Freak

Thanks for the tip. I always wondered how suchs things were done. I'm in debt with you over this one...

Cheers,

Tjitte
Sometimes I wish I was Mt Vesuvius: laying on my back in the sun while smoking a bit and everybody seeing me would say: \\"Look! He\\\'s active!\\" (author unknown to me)
 

Offline jonssonjTopic starter

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Posts: 254
    • Show only replies by jonssonj
Re: Help with OpenLibrary()
« Reply #8 on: March 08, 2004, 07:01:14 AM »
Thanks to all of you, for all your replys.

@Jettah
My intention is not to draw the user's attention this way. I'm only doing the sample program in the RKRM Library manual. But thanks for the point...   :-)  About the layout of my program, I did not know that the spaces disappeared in the message when pressing the submit button. All my programs that I write are usually indented.

@Piru
I saw that you includes a lot more header files. Is this really neccessary? I thought your example was a bit complicated just to show the use of DisplayBeep.

Anyway, thanks to you all for the help.

/Jörgen
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Help with OpenLibrary()
« Reply #9 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 jonssonjTopic starter

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Posts: 254
    • Show only replies by jonssonj
Re: Help with OpenLibrary()
« Reply #10 on: March 08, 2004, 09:08:55 AM »
@Piru
Thanks a lot for your help. :-) I appreciate it a lot. One more thing though, why do you call exit "an un-amiga call"? It is used in the RKRM Libraries manual.

/Jörgen
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Help with OpenLibrary()
« Reply #11 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;
}

 

Offline jonssonjTopic starter

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Posts: 254
    • Show only replies by jonssonj
Re: Help with OpenLibrary()
« Reply #12 on: March 08, 2004, 05:58:47 PM »
Ok, thanks!

Now I am on my amiga at home and I have right now tryed your example. The code compiles and links ok, but when I run the program, I can´t see anything that flashes and I can´t hear any beep either.

When I debug the program, it seems like it calls the function DisplayBeep(NULL);

What is wrong?

One more question, How do I add variables to the storm C V4 watch window, when I debug a program?

/Jörgen
 

Offline Steady

Re: Help with OpenLibrary()
« Reply #13 on: March 09, 2004, 08:57:42 AM »
@jonssonj

Maybe you disabled all DisplayBeep() actions in Prefs.
 

Offline jonssonjTopic starter

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Posts: 254
    • Show only replies by jonssonj
Re: Help with OpenLibrary()
« Reply #14 on: March 09, 2004, 09:51:47 AM »
I will check that out when I get home tonight.

Is there anyone out there with more suggestions?

/Jörgen