Log in

View Full Version : AddFunction outside of AvisynthPluginInit2?


jstelly
14th April 2005, 23:14
The other thread about using C# to write AviSynth filters got me curious, so I've been working on a filter that's meant to load managed filters. I'm exposing a LoadManaged() function in this filter, which should load a .NET assembly, enumerate all classes that implement my managed filter interface, and let them register any functions they want to expose to Avisynth scripts.

The problem seems to be that IScriptEnvironment->AddFunction() doesn't work when I call it outside of AvisynthPluginInit2. My LoadManaged function works, in that it enumerates the classes and AddFunction() is called, but if I try to use the newly registered function (Example()) in my script, it gives me a:

Script error: there is no function named "Example"

Is AddFunction restricted to only work during initialization?

stickboy
15th April 2005, 09:23
Exactly where do you call AddFunction?

jstelly
15th April 2005, 13:59
Well, first I call AddFunction in my AvisynthPluginInit2() to register the LoadManaged() function. Then when LoadManaged() is called, I load that assembly and call AddFunction for any functions the assembly wants to expose.

extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit2(IScriptEnvironment* env) {
env->AddFunction("LoadManaged", "s", LoadManagedFunc, 0);
return "dnSynth managed plugin";
}

AVSValue __cdecl LoadManagedFunc(AVSValue args, void* user_data, IScriptEnvironment* env) {
return FilterManager::LoadManaged(gcnew String(args[0].AsString()), AvsEnvironment::GetEnv(env));
}

AvsEnvironment is a managed wrapper for IScriptEnvironment that gets passed to the managed filters. So my scripts look something like this:

loadplugin("dnsynth.dll") # This is the filter where the above code is. It exposes LoadManaged()

LoadManaged("dnExample.dll") # This is a managed filter. In here I'd like to call AddFunction() in my C# filter class to register a function 'Example()'

Example() # I get an error here that there's no Example() function but I know I called AddFunction for it and the syntax looks right.

stickboy
15th April 2005, 17:53
It must be doable, because LoadPlugin/LoadVirtualDubPlugin/etc. are ordinary functions, and they call env->AddFunction.
Example() # I get an error here that there's no Example() function but I know I called AddFunction for it and the syntax looks right.We probably could help you better if you showed some code to LoadManaged. Otherwise, the only advice I have is to look at the source code to LoadPlugin or LoadVirtualDubPlugin.

jstelly
15th April 2005, 23:57
Good idea. I'll go ahead and look at LoadPlugin. If I can't get it working then I'll post the code here (there's a lot of plumbing code so it's a bit tough to pinpoint any one thing). Thanks.