ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.05k stars 492 forks source link

Microsoft C++ Exception When Creating A New Usertype #1305

Open bditt opened 2 years ago

bditt commented 2 years ago

Hi there, I'm running into an issue when it comes to manual mapping my DLL.

The DLL is written in Visual Studio 2019, C++ 17 Standard, with Use MFC in static library enabled.

Using Lua 5.4.3 compiled on my windows PC via command line.

I can run it normally when I use LoadLibrary, but when manual mapping it, I get a Microsoft C++ Exception during the creation of a new usertype.

Like I said, this only happens when using my manual mapper I made, but not with LoadLibrary. So I'm not sure if it's a mapper issue or a Sol issue.

Vector Class:

class Vector2 {
public:
    float x, y;

    inline Vector2() {
        x = y = 0.0f;
    }

    inline Vector2(float f1, float f2) {
        x = f1;
        y = f2;
    }

    inline Vector2 operator/(float v) const {
        return Vector2(x / v, y / v);
    }

    void operator -= (Vector2 other)
    {
        x -= other.x;
        y -= other.y;
    }

    inline Vector2 operator-(const Vector2& v) const {
        return Vector2(x - v.x, y - v.y);
    }

    inline Vector2 operator+(const Vector2& v) const {
        return Vector2(x + v.x, y + v.y);
    }

    inline Vector2& operator+=(const Vector2& v) {
        x += v.x; y += v.y; return *this;
    }

    inline bool Zero() const {
        return (x > -0.1f && x < 0.1f && y > -0.1f && y < 0.1f);
    }
};

Code:

            lua.open_libraries(sol::lib::base, sol::lib::string, sol::lib::table, sol::lib::io);

            printf("2\n");
            auto v2 = lua.new_usertype<Vector2>(x_("Vector2"), sol::constructors<Vector2(), Vector2(float, float)>());
            printf("2.1\n");
            v2[x_("x")] = &Vector2::x;
            printf("2.2\n");
            v2[x_("y")] = &Vector2::y;

RustClient_ziPDtSXV2a

ThePhD commented 2 years ago

What's a "manual mapper" and how does that work?

bditt commented 2 years ago

What's a "manual mapper" and how does that work?

Manual Mapping is basically emulating LoadLibrary and you basically allocate space in a process to write the dll into memory. I don't think this should be an issue, but I just figured I'd mention it.

ThePhD commented 2 years ago

I absolutely expect that to fail then, because there's more than just memory that needs to run. You need to properly initialize every static variable, run every kick-on routine that's associated with the DLL being loaded, and more.

Is this an Open Source project? I could try debugging it that way.

bditt commented 2 years ago

It's not open source, but I think this is the closest I can show that's like the project.

https://github.com/mactec0/Kernelmode-manual-mapping-through-IAT