Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
// Создаёт прокси для функции и возвращает адрес для вызова.
IntPtr GetFunctionAddress(IFunction function)
{
// Создает прокси, где метод Invoke вызовет IFunction.NumericEvaluation и обработает исключения.
var proxy = ProxyBuilder.GetProxy(function);
var method = proxy.GetType().GetMethod("Invoke");
// Создаёт тип делегата с конкретными типами параметров, приправленными [MarshalAs].
var delegateType = ProxyBuilder.GetDelegateType(method);
// TODO: Делегат надо закешировать, чтобы GC его раньше времени не прибил.
var @delegate = Delegate.CreateDelegate(delegateType, proxy, method);
// Получает адрес неуправляемой прослойки для вызова прокси.
return Marshal.GetFunctionPointerForDelegate(@delegate);
}
if ( !type->IsPublic || type->IsAbstract || !IFunction::typeid->IsAssignableFrom( type ) ) continue;
assemblyInfo->Functions->Add( ( IFunction^ ) Activator::CreateInstance( assembly->GetType( type->ToString() ) ) );
Разве что не уверен, что ModuleInitializer будет корректной заменой DllMain.
Proposed Long-Term Solution
…
In particular, the common language runtime is adding a new load time event that signals the loading of a module into an application domain. This new event is similar to the native DLL_PROCESS_ATTACH event. When a module is loaded, the common language runtime will check the module for a .cctor method in the global scope. The global .cctor is the managed module initializer. This initializer runs just after the native DllMain (in other words, outside of loader lock) but before any managed code is run or managed data is accessed from that module. The semantics of the module .cctor are very similar to those of class .cctors and are defined in the ECMA C# and Common Language Infrastructure Standards.
using System;
using NetEFI;
public class csecho: IFunction {
public FunctionInfo Info {
get {
return new FunctionInfo( "csecho", "s", "return string",
typeof( String ), new[] { typeof( String ) } );
}
}
public FunctionInfo GetFunctionInfo( string lang ) { return Info; }
public bool NumericEvaluation( object[] args, out object result, ref Context context ) {
//while ( !context.IsUserInterrupted ) { }
if ( context.IsDefined( "vbecho" ) ) {
context[ "vbecho" ].NumericEvaluation( args, out result, ref context );
} else {
result = Evaluate( ( string ) args[0] );
}
return true;
}
public string Evaluate( string text ) {
return text;
}
}
Внедрение кода с пользой