© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•7mo ago•
41 replies
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);
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);
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ P/Invoke x86 ASM
C#CC# / help
3y ago
Looking for Boilerplate and Learning Resources
C#CC# / help
2y ago
How to effectively dispose unmanaged resources?
C#CC# / help
3y ago
Looking for resources regarding login flows and UI states
C#CC# / help
15mo ago