sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Microsoft C++ Exception When Creating A New Usertype

Open bditt opened this issue 3 years ago • 4 comments

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

bditt avatar Feb 08 '22 20:02 bditt

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

ThePhD avatar Feb 09 '22 04:02 ThePhD

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.

bditt avatar Feb 09 '22 22:02 bditt

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.

ThePhD avatar Feb 10 '22 05:02 ThePhD

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

bditt avatar Feb 10 '22 05:02 bditt