Dynamic API Declarations (ADVANCED) | VB & C# | Snippet 07-31-2016, 02:37 AM
#1
Yo.
So I was in need of some function to dynamically create API calls for things like WriteProcessMemory etc, it was originally intended for crypter use as you could easily decrypt the strings on runtime and no one would ever know that you're even using WinAPI calls on a static analysis. However this is for a different project, but this is completely custom, I only know of one other function similar to this and it's by Menalix, and I was originally using his, however, is didn't work for certain libraries like advapi32.dll so I just redid the entire thing.
THIS SHOULD WORK WITH ANY P.INVOKE(able) FUNCTION!
C#
IMPORTANT: YOU NEED TO IMPORT Microsoft.VisualBasic.dll TO USE Conversions.ToGenericParameter OR ELSE THIS WILL NOT WORK IN C#
C# USAGE:
In this case, I use GetCurrentHwProfileParameter, but you get the idea.
VB.Net
VB.Net USAGE:
See C# Usage, it's not that hard to convert.
Enjoy folks, I think I could handle the array conversion a little better and maybe even inline, but I'm too lazy to LINQ it.
So I was in need of some function to dynamically create API calls for things like WriteProcessMemory etc, it was originally intended for crypter use as you could easily decrypt the strings on runtime and no one would ever know that you're even using WinAPI calls on a static analysis. However this is for a different project, but this is completely custom, I only know of one other function similar to this and it's by Menalix, and I was originally using his, however, is didn't work for certain libraries like advapi32.dll so I just redid the entire thing.
THIS SHOULD WORK WITH ANY P.INVOKE(able) FUNCTION!
C#
IMPORTANT: YOU NEED TO IMPORT Microsoft.VisualBasic.dll TO USE Conversions.ToGenericParameter OR ELSE THIS WILL NOT WORK IN C#
Code:
public static T CreateAPI<T>(string wLib, string mName)
{
AssemblyBuilder ASMB = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(Assembly.GetExecutingAssembly().FullName), AssemblyBuilderAccess.RunAndSave);
ModuleBuilder MODB = ASMB.DefineDynamicModule(MethodBase.GetCurrentMethod().Name);
TypeBuilder TB = MODB.DefineType(MethodBase.GetCurrentMethod().DeclaringType.Name, TypeAttributes.Public);
MethodInfo MI = typeof(T).GetMethods()[0];
List<Type> LP = new List<Type>();
foreach (ParameterInfo pI in MI.GetParameters())
LP.Add(pI.ParameterType);
MethodBuilder MB = TB.DefinePInvokeMethod(mName,
wLib,
MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
MI.ReturnType, LP.ToArray(),
CallingConvention.Winapi,
CharSet.Ansi);
MB.SetImplementationFlags(MB.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);
return Conversions.ToGenericParameter<T>(Delegate.CreateDelegate(typeof(T), TB.CreateType().GetMethod(mName)));
}C# USAGE:
In this case, I use GetCurrentHwProfileParameter, but you get the idea.
Code:
// Note: Return type and Parameters MUST BE CORRECT
public delegate bool GetCurrentHwProfileParameter(IntPtr hw);
public static GetCurrentHwProfileParameter GetCurrentHwProfile;
GetCurrentHwProfile = CreateAPI<GetCurrentHwProfileParameter>("advapi32.dll", "GetCurrentHwProfile"); //Cast the Delegate
GetCurrentHwProfile(lHWInfoPtr); //Now you can call it like a normal function!VB.Net
Code:
Public Shared Function CreateAPI(Of T)(wLib As String, mName As String) As T
Dim ASMB As AssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(New AssemblyName(Assembly.GetExecutingAssembly().FullName), AssemblyBuilderAccess.RunAndSave)
Dim MODB As ModuleBuilder = ASMB.DefineDynamicModule(MethodBase.GetCurrentMethod().Name)
Dim TB As TypeBuilder = MODB.DefineType(MethodBase.GetCurrentMethod().DeclaringType.Name, TypeAttributes.[Public])
Dim MI As MethodInfo = GetType(T).GetMethods()(0)
Dim LP As New List(Of Type)()
For Each pI As ParameterInfo In MI.GetParameters()
LP.Add(pI.ParameterType)
Next
Dim MB As MethodBuilder = TB.DefinePInvokeMethod(mName, wLib, MethodAttributes.[Public] Or MethodAttributes.[Static] Or MethodAttributes.PinvokeImpl, CallingConventions.Standard, MI.ReturnType, LP.ToArray(), _
CallingConvention.Winapi, CharSet.Ansi)
MB.SetImplementationFlags(MB.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)
Return Conversions.ToGenericParameter(Of T)([Delegate].CreateDelegate(GetType(T), TB.CreateType().GetMethod(mName)))
End FunctionSee C# Usage, it's not that hard to convert.
Enjoy folks, I think I could handle the array conversion a little better and maybe even inline, but I'm too lazy to LINQ it.




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)