StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
liuxilu  
#1 Posted : Saturday, May 2, 2020 9:05:13 AM(UTC)
liuxilu

Rank: Advanced Member

Reputation:

Groups: Translators, Moderators, Approved
Joined: 4/6/2020(UTC)
Posts: 79
China

Thanks: 1 times
Was thanked: 21 time(s) in 21 post(s)
First, place this in load script.
Code:
function MethodFromNativeDll (DllPath, EntryName, RtnT, ParmT) {
    var asmName = new clr.System.Reflection.AssemblyName();
    asmName.Name = "NativeDll";

    var asmBuilder = 
        clr.System.AppDomain.CurrentDomain.DefineDynamicAssembly(
            asmName, clr.System.Reflection.Emit.AssemblyBuilderAccess.Run);

    var modBuilder = 
        asmBuilder.DefineDynamicModule("NativeDll");

    var methdBuilder = 
        modBuilder.DefinePInvokeMethod(EntryName, DllPath,
        clr.System.Enum.Parse(clr.System.Reflection.MethodAttributes.Final.GetType(),
            'Public, Static, PinvokeImpl'),
        clr.System.Reflection.CallingConventions.Standard, RtnT, ParmT, 
        clr.System.Runtime.InteropServices.CallingConvention.Winapi, 
        clr.System.Runtime.InteropServices.CharSet.Ansi);

    methdBuilder.SetImplementationFlags(
         clr.System.Enum.Parse(clr.System.Reflection.MethodImplAttributes.IL.GetType(),
         methdBuilder.GetMethodImplementationFlags().ToString() + ", PreserveSig"));

    modBuilder.CreateGlobalFunctions();
    mthdInfo = modBuilder.GetMethod(EntryName);
    return mthdInfo;
}

Example code:
Code:
//Type for return value.
var RtnT = clr.System.Type.GetType("System.Int32");

//Type array for param types.
var ParmT = host.newArr(clr.System.Type, 2);
ParmT[0] = clr.System.Type.GetType("System.IntPtr");
ParmT[1] = clr.System.Type.GetType("System.Int32&");
//For ByRef vars, append a & after the type name.

//Object array for params.
var Parm = host.newArr(clr.System.Object, 2);
Parm[0] = sp.DesktopWindow().HWnd;
Parm[1] = 0;

//Get method
var Mthd = MethodFromNativeDll("user32.dll", "GetWindowThreadProcessId", RtnT, ParmT);

//Invoke method.
var v = Mthd.Invoke(null, Parm);

sp.MessageBox(Parm[1], v);

Edited by user Tuesday, May 5, 2020 6:11:03 AM(UTC)  | Reason: Not specified

thanks 1 user thanked liuxilu for this useful post.
Rob on 5/3/2020(UTC)
sunnyabcd  
#2 Posted : Tuesday, May 5, 2020 4:02:02 AM(UTC)
sunnyabcd

Rank: Member

Reputation:

Groups: Approved
Joined: 4/10/2020(UTC)
Posts: 22

Thanks: 8 times
Was thanked: 2 time(s) in 2 post(s)
give errors:

ReferenceError: MethodFromNativeDll is not defined
at LoadScripts:13:12 -> var Mthd = MethodFromNativeDll("user32.dll", "SetParent", RtnT, ParmT);

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.