问题
Is there any way for C# to get a list of the methods the swf exposes through ExternaInterface?
回答1:
Update:
Just realized that I've misread your question as you are looking for methods defined via the Flex ExternalInterface class rather than those of the Shockwave ActiveX control
itself; I'm gonna keep my original answer below as it might still be helpful regarding SWF
usage via C# in general.
Concerning ExternalInterface
I don't have an answer right now, but you might look into Fun with C# and the Flash Player 8 External API to get an idea on how to use this API via C# in the first place. (Another helpful sample might be Use External Interface of Flash 8 in Python and C# Applications.)
From what I'm reading in the former article there is likely no straight forward solution and the calling conventions via passing crafted XML fragments to CallFunction() are somewhat quirky, still you should be able to translate Georges solution to this in principle (as I said, it's likely not gonna be pretty ;)
Good luck!
How to access a SWF from .NET via COM Interoperability:
SWF for Windows is implemented by means of the Adobe Flash Player ActiveX control, consequently you would use it from .NET via COM Interoperability.
You can find a (legacy) article/sample regarding this on Adobes site, see Embedding Macromedia Flash Player in a C# Application to Display Stock Information for an overview (please note the introductory note of the article regarding unavailability of the sample code), but see below.
More specifically you can find the initial steps you need to take to achieve your goal on another page of this article, see Embedding and Communicating with the Macromedia Flash Player in C# Windows Applications - in particular please follow the article/steps up to and including section Making the Macromedia Flash Player ActiveX Control Available Within Visual Studio .NET.
Once you have completed the outlined steps to add the Shockwave ActiveX control
to your toolbox and to a particular projects references you can simply double click this reference (named ShockwaveFlashObjects
in Visual Studio 2008), which will open the Visual Studio object browser highlighting the assembly Interop.ShockwaveFlashObjects
; then navigate down into the namespace ShockwaveFlashObjects
, where you'll find, amongst others, the interface IShockwaveFlash
, exposing (depending on your view filter) all its members, including the desired external methods with their respective C# signatures.
回答2:
If you can access your swf ( have the source to it ) then all you need to do is add a callback to a function that returns all the methods you've setup for use with ExternalInterface as an Array, String, etc.
If you do not have access to the swf source, you could make another swf, set it up with ExternalInterface so you can call it. Load the original swf in your new one and in the INIT handler, start scavanging for methods that might be called linked to ExternalInterface.addCallback using describeType.
This is what I have in mind:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, loaded);
loader.load(new URLRequest('externalInterfaceTest.swf'));
function loaded(e:Event):void{
//get all the methods in the loaded swf
var methods:XMLList = describeType(e.target.content).method;
for each(var method:XML in methods){
//get methods defined by the author/not belonging to flash.display.*,flash.events.*,etc
if(method.@declaredBy.indexOf('flash') == -1){
trace('\nmethod description start========================');
trace('methodName: ' + method.@name);
trace('returnType: ' + method.@returnType);
for each(var param:XML in method.parameter){
trace('parameter ' + param.@index + ' type: ' + param.@type + ' optional: ' + param.@optional);
}
trace('\nmethod description end\n========================');
}
}
}
This would work if:
- the swf you load doesn't have a DocumentClass, the functions that might be added as callbacks are in the timeline, therefore public
- the swf you load has all methods linked with ExternalInterface.addCallback declared public
If you have no access to the swf or the developer, some ethical decompiling might help.
A different approach would be to use classes developed by other developer to inspect swfs. Here are some examples:
- senocular's SWFReader
- Thibault Imbert's SWFExplorer
- Denis Kolyako's getDefinitionNames
These classes may not offer the exact functionality you need, but it's a good starting point for what you need, only you'll be looking using binary data( which I'm not very good at right now). You can find the SWF file specifications here.
Hope this helps.
回答3:
You'll have to do this by reading the SWF file from C# code - not by communicating with it in any real sense. This post will point you in the right direction, but there'll probably be quite a bit of work to do. (You'll need to decompile the bytecode and see what functions get passed to ExternalInterface.addCallback
.)
The ActionScript bytecode format is documented, as is the SWF format.
来源:https://stackoverflow.com/questions/1353525/c-sharp-and-flash-communication