Re: HOWTO Implement LoadLibrary, GetProcAdress, and FreeLibrary.



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);
}

.



Relevant Pages

  • Re: wide screen text mode?
    ... on the stack and perform a far call to the routine. ... update yx with cursor address/not 0008/0000 ... PUSH BP;save BP ... TEST AL,02H;00/01 CHAR OR 02/03 STRING? ...
    (comp.os.msdos.programmer)
  • Re: Stack
    ... character pointer once i exit the push function. ... So in case of *Strings* i cannot display the popped string or the ... members of the stack at any given time. ... struct stackelement item; ...
    (comp.lang.c)
  • Re: Getting a stack trace
    ... If all you want to know is the function call sequence (not the whole stack ... number or a string and then pop it off. ... function you push that function's ID to the stack, ... At error you just dump the contents of the stack to ...
    (alt.os.linux)
  • Re: Getting a stack trace
    ... If all you want to know is the function call sequence (not the whole stack ... number or a string and then pop it off. ... function you push that function's ID to the stack, ... At error you just dump the contents of the stack to ...
    (comp.unix.programmer)
  • Re: Is it wise to push all-in early in a tournament?
    ... The best times to make a push play ... But why call when you can push and take down a nice pot when your opponent ... But remember that if this "big bet" is a significant portion of your stack, ... I consider it my goal in any tournament to do the latter as much as ...
    (rec.gambling.poker)