winim icon indicating copy to clipboard operation
winim copied to clipboard

winim/clr parameter passing for non-main methods

Open S3cur3Th1sSh1t opened this issue 3 years ago • 2 comments

Hey,

I'm facing a problem similar to this one. I would like to load a DLL from Nim passing parameters to it's constructor. The C# example is the following:

namespace TestSpace
{
    public class Run
    {
        public Run(uint value1, IntPtr value2, int value3)
        {
             // Set Stuff
        }

        public void DoStuff()
        {
             // Do Stuff
        }
    }
}

I tried executing this constructor via e.g.

var assembly = load(dllbytes)
var Test = assembly.GetType("TestSpace.Run")
dump Test
# Works fine till there

with the following trials:

var
    intvalue1: int = 1
    intvalue2: int = 2
    testHandle: HANDLE
Test.new(intvalue1,intvalue2,testHandle)
# leads to Error: unhandled exception: unable to invoke specified member: new (0x80131512) [CLRError]
Test.new(nil, aaa,testHandle,aaa)
# leads to unable to invoke specified member: CreateInstance (0x80131604) [CLRError]
var cmd: seq[string]
var i = 1
while i <= paramCount():
    cmd.add(paramStr(i))
    inc(i)
echo cmd
var arr = toCLRVariant(cmd, VT_BSTR)
Test.invoke(nil, toCLRVariant([arr]))
# Also Error: unhandled exception: unable to invoke specified member: CreateInstance (0x80131604) [CLRError]
# This is a string array from how I understood it, actually makes no sense as the function expects uint, Pointer and int. But it's not possible to embed (uint,HANDLE,int) in an array right?

I also tried executing the DoStuff function via e.g.

Test.invoke("DoStuff", BindingFlags_InvokeMethod or BindingFlags_Default)
# failed with Error: unhandled exception: unable to invoke specified member: DoStuff (0x80131512)
Test.DoStuff()
# same error
var arr = toCLRVariant([""], VT_BSTR) # Passing no arguments
echo @asd.DoStuff(nil, toCLRVariant([arr]))
# Error: unhandled exception: convert from VT_ARRAY(1D)|VT_VARIANT to integer [VariantConversionError]
# Why does it expect integer input?

Some idea how to pass the parameters for these examples? Appreciate any help.

Greetings

S3cur3Th1sSh1t avatar Jun 14 '22 12:06 S3cur3Th1sSh1t

@S3cur3Th1sSh1t Did you ever figure this out? I'm having similar issues trying to create Assembly/AppDomain event handlers and basically getting the same errors.

daddycocoaman avatar Feb 02 '23 05:02 daddycocoaman

No, as a Workaround I was using an executable instead of an DLL and loaded that as parameter passing works fine there.

S3cur3Th1sSh1t avatar Feb 02 '23 07:02 S3cur3Th1sSh1t