Re: HOWTO Implement LoadLibrary, GetProcAdress, and FreeLibrary.
- From: ATS <ATS@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 15 Nov 2005 13:05:18 -0800
Thanks for the reply, but without having it completely spoon fed I have no
wheres to go but this approach. I need the full
LoadLibray\GetProcAddress\FreeLibrary, in C#. I can not use static mechanisms
to load my DLL, as the DLL will not exists until run time. And the function
prototype will not necessarily be static either. This approach was taken on
by others to basically use the JIT to FORCE C# into working with
LoadLibrary\GetProcAddress\FreeLibrary exactly as I need it, where both the
DLL is not know until run time, and the prototype of the function to call is
also not known until run time.
Now, for clarification, I do not need the prototype of the function to be
dynamic, I can live with using (for the moment) a hard coded prototype, so
long as the DLL is not statically coded.
By the way, I've changed the Invoke function from other posts on google as
such:
=======================
public static string Invoke(IntPtr IntPtr_Function, string csParam1,
string csParam2)
{
object[] zobject_Parameters = new object[]
{
csParam1,
csParam2
};
Type[] zType_ParameterTypes = new Type[]
{
typeof(string),
typeof(string)
};
String[] zcsParameterTypes = new String[]
{
"value",
"value"
};
Type Type_Return = typeof(string);
return (string) Invoke
(
IntPtr_Function,
zobject_Parameters,
zType_ParameterTypes,
zcsParameterTypes,
Type_Return
);
}
public static object Invoke
(
IntPtr IntPtr_Function,
object[] zobject_Parameters,
Type[] zType_ParameterTypes,
String[] zcsParameterTypes,
Type Type_Return
)
{
// To begin, we will setup a temporary assembly, module, and method
which we will use JIT
// technqiues to build, that will call our desired function.
AssemblyName AssemblyName_This = new AssemblyName();
AssemblyName_This.Name = "Invokable";
AssemblyBuilder AssemblyBuilder_This =
AppDomain.CurrentDomain.DefineDynamicAssembly
(
AssemblyName_This,
AssemblyBuilderAccess.Run
);
ModuleBuilder ModuleBuilder_This =
AssemblyBuilder_This.DefineDynamicModule
(
"CInvoker"
);
MethodBuilder MethodBuilder_This = ModuleBuilder_This.DefineGlobalMethod
(
"Invoker",
MethodAttributes.Public | MethodAttributes.Static,
Type_Return,
zType_ParameterTypes
);
ILGenerator ILGenerator_This = MethodBuilder_This.GetILGenerator();
int i;
// We must now push each paramter onto the stack. As we do, we must
push them on to reflect
// whether they are static values, or references.
for (i = 1; i <= zobject_Parameters.Length; i++)
{
switch (zcsParameterTypes[i - 1])
{
case "value":
ILGenerator_This.Emit(OpCodes.Ldarg, i);
break;
case "address":
ILGenerator_This.Emit(OpCodes.Ldarga, i);
break;
default:
throw
(
new Exception
(
"The parameter #" + i.ToString() + " does not have a valid
reference type"
)
);
}
}
// We must now push the function pointer onto the stack.
if (IntPtr.Size == 4)
{
ILGenerator_This.Emit(OpCodes.Ldc_I4, IntPtr_Function.ToInt32());
}
else if (IntPtr.Size == 8)
{
ILGenerator_This.Emit(OpCodes.Ldc_I8, IntPtr_Function.ToInt64());
}
else
{
throw new PlatformNotSupportedException();
}
// Now we pop the parameters, and the function from the stack, which
makes the call to the
// function. After which, we push the return type onto the stack.
ILGenerator_This.EmitCalli
(
OpCodes.Calli,
CallingConvention.StdCall,
Type_Return,
zType_ParameterTypes
);
ILGenerator_This.Emit(OpCodes.Ret);
// At this point, out code for the JIT is ready. We now need to
actually call it, and
// return its results.
ModuleBuilder_This.CreateGlobalFunctions();
MethodInfo MethodInfo_This = ModuleBuilder_This.GetMethod("Invoker");
return MethodInfo_This.Invoke(null, zobject_Parameters);
}
.
- References:
- Re: HOWTO Implement LoadLibrary, GetProcAdress, and FreeLibrary.
- From: Nicholas Paldino [.NET/C# MVP]
- Re: HOWTO Implement LoadLibrary, GetProcAdress, and FreeLibrary.
- Prev by Date: Re: String Builder insert
- Next by Date: Re: String Builder insert
- Previous by thread: Re: HOWTO Implement LoadLibrary, GetProcAdress, and FreeLibrary.
- Next by thread: Re: Legal Image Question
- Index(es):
Relevant Pages
|