Page 1 of 1

can't invoke a mono method that return a string

Posted: Fri Dec 06, 2019 2:54 pm
by Spazi
Hi,

Like the title says, I get a crash when trying to invoke any method that has a return type of String (lua crash on "return monopipe.readString(resultlength);").
Is there a way to force mono_invoke to return the string address instead, so I do the string reading myself ?

Thank you

Re: can't invoke a mono method that return a string

Posted: Fri Dec 06, 2019 6:47 pm
by Eric
replace your mono collector dll's with the ones in these and then try. (There was an error with mono_free) [Link]

extract to your ce base folder/autorun/dll folder

Re: can't invoke a mono method that return a string

Posted: Fri Dec 06, 2019 10:12 pm
by Spazi
Wow, did not expect a response so fast.
I had already made a custom dll in the meantime, but an official correct solution is always better.
Thank you, will put it to good use

Re: can't invoke a mono method that return a string

Posted: Fri Dec 06, 2019 10:19 pm
by Spazi
Well, actually, just tested, it still crashed with yours on the game I'm testing :/
Same thing, it crash on getting the string from pipe

Re: can't invoke a mono method that return a string

Posted: Sat Dec 07, 2019 10:10 am
by Eric
most mono games have no anti debug so you can debug the dll sourcecode with visual studio while attached to the game
perhaps you can see and fix it

and don't forget to close the game before compiling/replacing else the test will be done on the old dll

Re: can't invoke a mono method that return a string

Posted: Sat Dec 07, 2019 1:59 pm
by Spazi
I get a "Write acces violation" exception from the call to 'mono_type_get_type' in 'WriteObject'

I'll see if I can go into the mono function with Mono sources to have more detail

Re: can't invoke a mono method that return a string

Posted: Sat Dec 07, 2019 2:58 pm
by Spazi
Disregard pervious message, was because I was going step by step and, cheat engine was putting the pipe to a faulty behavior.

So, exception is on :
void *string = mono_object_to_string(object, NULL);

in WriteObject, and is a Read access violation.
Will try to know more

Re: can't invoke a mono method that return a string

Posted: Sat Dec 07, 2019 3:37 pm
by Spazi
(Or you could implement a workaround in this case, and have Lua read the string instead of Mono, like I did :D
Would save me a lot of hours I think '^^

I had Lua read a net string like this, to circumvent that invoke problem (and added another invoke method to MonoDataCollector that return the string address instead of trying to read it)

Code: Select all

local _readNetStringIT = nil
local _readNetStringCE = 0
local _readNetStringMN = nil
function readNetString(addr)
    local classid, classname = mono_object_getClass(addr)
    if classname ~= 'String' then return nil end

    if _readNetStringIT == nil then
       for _, m in pairs(mono_class_enumMethods(classid)) do
           if m.name == 'System.Collections.IEnumerable.GetEnumerator' then _readNetStringIT = m.method end
       end
    end

    local it = mono_invoke_method(nil,_readNetStringIT,addr,{})

    if _readNetStringMN == nil then
       for _, m in pairs(mono_class_enumMethods(mono_object_getClass(it))) do
           if m.name == 'MoveNext' then _readNetStringMN = m.method end
       end
       for _, f in ipairs(mono_class_enumFields(mono_object_getClass(it))) do
           if f.name == 'currentElement' then _readNetStringCE = f.offset end
       end
    end

    local sread = ''
    local mn = mono_invoke_method(nil, _readNetStringMN, it, {})
    repeat
          sread = sread .. readString(it+_readNetStringCE, 1)
          mn = mono_invoke_method(nil, _readNetStringMN, it, {})
    until mn == 0

    return sread
end
)

Re: can't invoke a mono method that return a string

Posted: Sat Dec 07, 2019 3:58 pm
by Eric
perhaps you can skip mono_object_to_string and just call mono_string_to_utf8 directly(havn't tested yet)

Re: can't invoke a mono method that return a string

Posted: Sat Dec 07, 2019 4:28 pm
by Spazi
Okay, it worked with commenting the "object_to_string" and changing the paramater for to_utf8 :)

I'll wait your official dll builds

Re: can't invoke a mono method that return a string

Posted: Mon Dec 09, 2019 11:30 pm
by Spazi
There is another problem with current invoke implementation, and parameters.

What you did works fine for value type, but it will always crash when passing any object parameter.
That is because the array you create and give to mono_invoke is malformed for object parameters.

Current implementation "box" everything, which is good only for value type. For object, you need to send the address, not box the address

I fixed it for me, for vtPointer parameter, by modifying the ReadObject function :

Code: Select all

	case MONO_TYPE_PTR:
		if (size > 0)
		{
			Read(addr, size);
			result = (void *) *((UINT64*)addr);
		}
		break;
	case MONO_TYPE_BYREF:
	case MONO_TYPE_CLASS:
	case MONO_TYPE_FNPTR:
	case MONO_TYPE_GENERICINST:
	case MONO_TYPE_ARRAY:
	case MONO_TYPE_SZARRAY:
		if (size > 0)
		{
			Read(addr, size);
		}
		break;
	}
Haven't tested for arrays and the like, but since in lua, we can only send vtPointer, that should cover it already