[comp.sys.mac.programmer] Problems finding applications running under Multifinder

slewis@altair.aero.org (08/22/90)

  I am trying to determine whether an application is running under THINK C
 and later launch it if it is running. The Code ApplicationIsRunning below runs the
first time and gives illegal instruction on the on the GetItem call the second
time I call it. The Handle AppleMenu is not altered between
 calls. I don't understand the problem. 
 
 	Also is the code for ActivateApplication proper? If the Activation is in WaitNextEvent
with the wake up interval 0 will it wake up? If the target uses the same routine to
return control will my program continue from where it left off???


MenuHandle AppleMenu;

... /* Initialization */
	AppleMenu = NewMenu(1,(StringPtr)"\P\024");	    /* file menu			*/
	AppendMenu(AppleMenu,(StringPtr)"\pAbout ...");
	AddResMenu(AppleMenu, (ResType) 'DRVR');					
	InsertMenu(AppleMenu,0);            /* Add as last menu                */
	....
	
boolean CaseLessCompare(register char *s1,register char *s2,register int i)
{
	while(i--) if(*(s1++) != *(s2++)) return(false);
	return(true);
}

/*
	Test to see if an application is running
*/
boolean ApplicationIsRunning(char *ApplicationName)
{
	int i,NItems;
	int NameLength;
	StringPtr ItemName;
	
	NItems = CountMItems(AppleMenu);
	NameLength = strlen(ApplicationName);
	for(i = 1; i <= NItems; i++) {
		GetItem(AppleMenu,(short)i,ItemName);
		if(CaseLessCompare((char *)&ItemName[1],ApplicationName,NameLength)) 
			return(true);
	}
	return(false); /* Not Running */
}

/*
	Activate Application - it better send you back
*/
void ActivateApplication(char *ApplicationName)
{
	int i,NItems;
	int NameLength;
	char *ItemName;
	char *AccessaryName = NULL;  /* will be real name if running */
	GrafPtr savePort;
	
	/* Is the Application running - if so get its real name */
	NItems = CountMItems(AppleMenu);
	NameLength = strlen(ApplicationName);
	for(i = 1; i <= NItems; i++) {
		GetItem(AppleMenu,(short)i,(StringPtr)ItemName);
		if(CaseLessCompare(&ItemName[1],ApplicationName,NameLength)) {
			AccessaryName = ItemName;
		}
	}
	if(!AccessaryName) error_exit("Target Application Not Running"); /* Not Running */
	
	GetPort(&savePort);
	OpenDeskAcc((StringPtr)AccessaryName);
	SetPort(savePort);
}