C#C
C#6mo ago
Evyr

✅ Looking for guidance with P/Invoke and unmanaged resources

I'm starting to dip my toes into P/Invoke and using unmanaged resources, and I'd like to hear if I'm doing anything particularly wrong or bad practice.

I've got a test class in my C++ dll with an int32 member, and a couple functions to heap allocate and destroy it, and some functions that take a pointer to the class and call its respective methods:

class TestClass {
public:
    TestClass();

    int32_t getI32();
    void setI32(int32_t value);
private:
    int32_t i32;
};

extern "C" __declspec (dllexport) void* TestClass_createTestClass();
extern "C" __declspec (dllexport) int32_t TestClass_getI32(void* pTestClass);
extern "C" __declspec (dllexport) void TestClass_setI32(void* pTestClass, int32_t value);
extern "C" __declspec (dllexport) void TestClass_destroyTestClass(void* pTestClass);
Was this page helpful?