Returning a struct from an Air Native extension

2019-07-04 01:55发布

问题:

Is it possible to return a struct from your native code? It's relatively straight forward to return an int or a boolean but how do you return a more complex struct back to the actionscript?

回答1:

You can return any object that can be represented in the native code as a FREObject. This actually includes any Actionscript class or Actionscript primitive data type. This includes things like: int, String, Array, BitmapData, ByteArray etc.

For example lets construct a return array of length 4 with the int values 0 - 3:

FREObject returnAnArray( FREContext cts, void* funcData, uint32_t argc, FREObject argv[])
{
    FREObject returnArray = NULL;
    FRENewObject((const uint8_t*)"Array", 0, NULL, &returnArray, nil );
    FRESetArrayLength( returnArray, 4 );

    for ( int32_t i = 0; i < 4; i++)
    {
        FREObject element;
        FRENewObjectFromUint32( i, element );
        FRESetArrayElementAt( returnArray, i, element );
    }
    return returnArray;
}

The method to construct Actionscript classes is a little more complex but follows a similar path. This is ofcourse a native C example, the Java equivalent is somewhat different but still it is possible to return complex objects from the native code.

For more information there is heaps of documentation here:

http://help.adobe.com/en_US/air/extensions/index.html



回答2:

The accepted answer is not accurate for your original question. You want to return a struct... well you can't actually do that but the proper way to do this is to write your native struct/class as an actionscript class, then use the ExtensionContext object to associate your native struct or class pointer with your context. Then when you can write methods into your struct/class that will directly interface with methods and properties within. If any of the methods within return another struct or class, repeat the process recursively.



回答3:

Java examples

Returning Array

FREObject stringElement = FREObject.newObject("String element value"); 
FREArray array = FREArray.newArray( "String", 1, false ); 
array.setObjectAt( 0, stringElement );
//now you can return array to AS3

More info http://help.adobe.com/en_US/air/extensions/WS982b6f491d178e6d6565d9b1132a79a012f-7ff8.html

Also consider using JSON

JSONObject jsonObject = new JSONObject();

try
{
    jsonObject.put("messgae", "Hello");
    jsonObject.put("name", "John");
}
catch (JSONException e)
{
}

FREObject jsonString FREObject.newObject(jsonObject.toString());
//now you can return jsonString to AS3


回答4:

Return value from finished process is limited to an int. But you can write data to stdout and read it in your AIR application:

p = new NativeProcess();
p.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);

private function onOutputData(evt:ProgressEvent):void {
    var outputData:String = p.standardOutput.readUTFBytes(p.standardOutput.bytesAvailable);
    trace(outputData);
}

StandardOutput implements IDataOut interface like ByteArray, so you can read any basic types from it. See also NativeProcess docs.