Call .Net Function Using Addresses

3

I trying to call .NET function from native code and via virtual address. I wrote a simple class library that have a class and static method and I browse with dnSpy

I also checked ImageBase value is 0x10000000

using System;

// Token: 0x02000002 RID: 2
public class Class1 : object
{
    // Token: 0x06000001 RID: 1 RVA: 0x00002050 File Offset: 0x00000250
    public static int Calculate(int x, int y)
    {
        return x + y;
    }
}

I'm looking at the documentation of LoadLibrary which says:

Loads the specified module into the address space of the calling process.

When I call calculate function, I'm getting AV Error.

Exception thrown at 0x10002050 in ConsoleApplication1.exe: 0xC0000005: Access violation executing location 0x10002050. occurred

#include <Windows.h>
#include <iostream>

using namespace std;

typedef int(*Calculate)(int x, int y);

PVOID RvaToVa(PVOID rva, PVOID imageBase)
{
    return PVOID((int)rva + (int)imageBase);
}

int main()
{
    HMODULE hModule = LoadLibrary(L"ClassLibrary1.dll");
    if (!hModule)
    {
        cout << GetLastError() << endl;
        return 0;
    }

    PVOID fPtr = RvaToVa((PVOID)0x00002050, (PVOID)0x10000000);
    Calculate calculate = (Calculate)fPtr;

    if (calculate)
    {
        int result = calculate(31, 32);
        cout << result << endl;
    }
    FreeLibrary(hModule);
    return 0;
}
c#
asked on Stack Overflow Nov 29, 2017 by İsmail Kocacan • edited Dec 15, 2018 by Cœur

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0