[comp.sys.mac.programmer] AppleTalk & Think C -- Help sought

audit038@spacm1.spac.spc.com (01/23/91)

HELP!!!  The following Think C code is my attempt at registering a name
on AppleTalk.  This either blows up or registers garbage.  I'm new to the MAC
and I was wondering if someone could point out where I'm blowing it.

Thanks in advance.
*****************************************************

#include "AppleTalk.h"
#define REMOVE_ALL_EVENTS	0
#define NIL_POINTER			0L

main()

{

	ABRecHandle	myABRecHandle;
	EntityName	myEntity;
	AddrBlock	entityAddr;
	Ptr			nbpNamePtr;
	Ptr			myBuffer;
	int			errCode;
	Boolean		async;
	int			nbpNameBufSize;
	int			mySocket = 20;
	
	long		memAmount;

	InitGraf( &thePort );
	MaxApplZone();
	InitFonts();
	FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
	InitWindows();
	InitMenus();
	TEInit();
	InitDialogs( NIL_POINTER );
	InitCursor();


	errCode = MPPOpen();
	if ( errCode != noErr)
	{
		printf("MPPOpen didn't work");
	}

	/* Call memory manager to allocate ABusRecord */
	myABRecHandle = (ABRecHandle)  NewHandle(nbpSize);
	MoveHHi( myABRecHandle );
	HLock (myABRecHandle );
	
	/* Set up our entity name to register */
	strcpy( myEntity.objStr, "Johnny");
	strcpy( myEntity.typeStr, "PrintSpooler");
	strcpy( myEntity.zoneStr, "*");
	
	nbpNamePtr = NewPtr(  strlen(myEntity.objStr) + strlen(myEntity.typeStr) + strlen(myEntity.zoneStr) + 12 );
	nbpNameBufSize = strlen(myEntity.objStr) + strlen(myEntity.typeStr) + strlen(myEntity.zoneStr) + 12;

	
	/* Set up the ABusRecord for the NBPRegister call */
	(*myABRecHandle)->nbpProto.nbpEntityPtr = &myEntity;
	(*myABRecHandle)->nbpProto.nbpBufPtr =  (Ptr) nbpNamePtr;  /* buffer used by NBP internally */
	(*myABRecHandle)->nbpProto.nbpBufSize = nbpNameBufSize;
	(*myABRecHandle)->nbpProto.nbpAddress.aSocket = mySocket;
	(*myABRecHandle)->nbpProto.nbpRetransmitInfo.retransInterval = 8;
	(*myABRecHandle)->nbpProto.nbpRetransmitInfo.retransCount = 3;

	async = FALSE;

	errCode = NBPRegister( &myABRecHandle, async );
	if ( errCode != noErr )
	{
		printf("NBPRegister failed");
	}


	
}
-- 
John Coffman

peirce@outpost.UUCP (Michael Peirce) (01/23/91)

In article <2245.279c7e47@spacm1.spac.spc.com>, audit038@spacm1.spac.spc.com writes:
> 
> HELP!!!  The following Think C code is my attempt at registering a name
> on AppleTalk.  This either blows up or registers garbage.  I'm new to the MAC
> and I was wondering if someone could point out where I'm blowing it.

You're using the old call, NBPRegister.  The "preferred" interface (i.e.
the interface Apple recommends you use now) is PRegisterName. See
Inside Macintosh Volume V, Chapter 28 for *some* info on this.

Not only are you using the old call, you are doing so asynchronously
which means it would try to post a completion event or some such nonsense
when it's done.  Your code checks status right away, then claims success
without waiting for the real completion of the routine.

Here's a fragment of code for using PRegisterName (sorry about the
Pascal, but it should be easy to transliterate...):

 1: NBPSetNTE(@theElementBuff,theName,theType,'*',theSocket);
 2: WITH theMPPPB DO BEGIN
 3:    entityPtr  := @theElementBuffer;
 4:    interval   := 3;
 5:    count      := 2;
 6:    verifyFlag := $FF;
 7:  END;
 8: CheckStatus(PRegisterName(@theMPPPB,kSYNC));

Note that entityPtr really points to a name table entry in this case
and the memory it points to can't move until after the name is unregistered.

-- michael


--  Michael Peirce         --   outpost!peirce@claris.com
--  Peirce Software        --   Suite 301, 719 Hibiscus Place
--  Macintosh Programming  --   San Jose, California 95117
--           & Consulting  --   (408) 244-6554, AppleLink: PEIRCE