Alchemy之 Memory to ByteArray

2009-05-14
Q: 我在C函數裡開了一片內存寫入了些數據,怎樣將這些數據作為ByteArray返回給AS呢??
A: 可將將數據塊的首指針地址返回給AS以讀取其中數據,具體看下面代碼:

比如說我們在C裡創建了一個整型數組,要返回給AS:
static AS3_Val myfun(void *self, AS3_Val args){
    int buffer[4] = {1, 2, 3, 4};
    return AS3_Array("IntType, AS3ValType", AS3_Int(4), AS3_Ptr(buffer));
}
然後在AS裡進行讀取:
var loader:CLibInit = new CLibInit();
var clib:Object = loader.init();

var rs:Array = clib.myfun();
var len:init = rs[0];
var ptr:uinit = rs[1];

var ns:Namespace = new Namespace("cmodule.ModuleName");
var memory:ByteArray = (ns::gstate).ds;
memory.position = ptr;

var i:int;
for(i = 0; i < len; i ++){
    trace(memory.readInt());
}

M-OSCAR | Powered by Blogger | Entries (RSS) | Comments (RSS) | Designed by MB Web Design | XML Coded By Cahayabiru.com