Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Wed Jan 29, 2020 10:44 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Passing Args to a function in a plugin
PostPosted: Mon Sep 05, 2011 1:01 am 
Offline

Joined: Thu Jan 12, 2006 3:30 pm
Posts: 88
I basically just got my first plugin to compile in xcode, trying to learn Realbasic better, and also to learn how to actually write things in C++ at the same time. I had to do some work arounds to convert the REALstring to a char, then to a char array with the last one being a null to pass to my function, which basically allows it to work like a simple string being passed to the final function.

My first function is a wrapper around the Macs AuthorizationExecuteWithPrivileges whose prototype is below, and i am basically just sidestepping arguments inside of char * const *arguments.
OSStatus AuthorizationExecuteWithPrivileges(AuthorizationRef authorization,
   const char *pathToTool,
   AuthorizationFlags options,
   char * const *arguments,
   FILE **communicationsPipe);


I'd like to know how i should implement a plugin function which accepts an array of args, and how to pass that properly ( and ideally more elegantly ) to the final function.

This is my plugin -
#include "rb_plugin.h"
#include <string>

// includes the ExecuteWithPermissions or Mac OS
#include "AuthorizationExecute.h"

// #if !TARGET_OS_MAC
// #endif


static int runAuth(REALstring sPathToTool, REALstring sArguments)
{
&nbsp; &nbsp;// this is very ugly just to get it to compile, need to redo this entirely so it's got clean logic
&nbsp; &nbsp;if ( sPathToTool->Length() > 0 )
&nbsp; &nbsp;{
&nbsp; &nbsp;&nbsp; &nbsp;char *sMyPathToTool = NULL;
&nbsp; &nbsp;&nbsp; &nbsp;sMyPathToTool = (char *)malloc(sPathToTool->Length());
&nbsp; &nbsp;&nbsp; &nbsp;if (sMyPathToTool != NULL)
&nbsp; &nbsp;&nbsp; &nbsp;{
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;memcpy (sMyPathToTool, sPathToTool->CString(), sPathToTool->Length());
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if ( sArguments->Length() > 0 )
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;char *sMyArguments = NULL;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;sMyArguments = (char *)malloc(sArguments->Length());
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (sMyArguments != NULL)
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;memcpy (sMyArguments, sArguments->CString(), sArguments->Length());
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;char *myNewArguments[] = { sMyArguments, NULL };
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;return runAuthorization( sMyPathToTool, myNewArguments );
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;else
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;char *myNewArguments[] = { NULL };
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;return runAuthorization( sMyPathToTool, myNewArguments );
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}
&nbsp; &nbsp;&nbsp; &nbsp;}
&nbsp; &nbsp;}
&nbsp; &nbsp;return -1;
}

REALmethodDefinition runAuthDef = {
&nbsp; &nbsp;(REALproc) runAuth,
&nbsp; &nbsp;REALnoImplementation,
&nbsp; &nbsp;"runAuth(sPath as String, sArgs as String ) as integer"
};

void PluginEntry()
{
&nbsp; &nbsp;//char sPathToTool[1024] = "/bin/sh";
&nbsp; &nbsp;//char *sArguments[] = { "open hello", NULL };
&nbsp; &nbsp;//runAuthorization( sPathToTool, sArguments );
&nbsp; &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp;//if (gAuthorizationRef)
&nbsp; &nbsp;//{
&nbsp; &nbsp;//&nbsp; &nbsp;AuthorizationFree(gAuthorizationRef, kAuthorizationFlagDefaults);
&nbsp; &nbsp;//}
}


and the include that does all the work
#include "AuthorizationExecute.h"


#ifdef __cplusplus
extern "C" {
#endif
&nbsp; &nbsp;
/*!
@function runAuthorization
Run an executable tool with enhanced privileges after passing suitable authorization procedures.
@param pathToTool Full pathname to the tool that should be executed with enhanced privileges.
@param arguments An argv-style vector of strings to be passed to the tool.
char sPathToTool[1024] = "/bin/sh";
char *sArguments[] = { "open hello", NULL };
runAuthorization( sPathToTool, sArguments )

*/
int runAuthorization( const char *sPathToTool, char * const *arguments )
{
&nbsp; &nbsp;&nbsp; &nbsp;int myStatus = -1;
&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;#if TARGET_OS_MAC
&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
&nbsp; &nbsp;&nbsp; &nbsp;AuthorizationRef myAuthorizationRef;
&nbsp; &nbsp;&nbsp; &nbsp;myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, myFlags, &myAuthorizationRef);
&nbsp; &nbsp;&nbsp; &nbsp;if (myStatus == errAuthorizationSuccess)
&nbsp; &nbsp;&nbsp; &nbsp;{
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//SInt16 res;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//StandardAlert(kAlertNoteAlert, "\pAuthorization Error", "\pCould not authorize application to update.", 0, &res);
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//return myStatus;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;AuthorizationRights myRights = {1, &myItems};
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;myFlags = kAuthorizationFlagDefaults |
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;kAuthorizationFlagInteractionAllowed |
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;kAuthorizationFlagPreAuthorize |
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;kAuthorizationFlagExtendRights;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;myStatus = AuthorizationCopyRights(myAuthorizationRef, &myRights, NULL, myFlags, NULL );
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (myStatus == errAuthorizationSuccess)
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//break;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//char *myNewArguments[] = { sArguments, NULL };
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;FILE *myCommunicationsPipe = NULL;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// char myReadBuffer[128];
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;myFlags = kAuthorizationFlagDefaults;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, sPathToTool, myFlags, arguments, &myCommunicationsPipe);
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}
&nbsp; &nbsp;&nbsp; &nbsp;}
&nbsp; &nbsp;&nbsp; &nbsp;AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDefaults);
&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;#endif
&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;return myStatus;
}

#ifdef __cplusplus
}
#endif


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group