Search code examples
c#c++sqlitein-memory-database

Sharing data between C++ and C# using an in-memory SQLite database?


I am working in a project that features several C++ and a few C# components. I am currently working on a new C# component that needs to share data with a C++ component (there may be more components accessing this in the future). All components are running in the same process. My current idea is to use an in-memory database, but I'm not sure whether it is possible.

SQLite is popular and available for both C++ and C# projects, and it has an in-memory feature. The question is if it is possible to access the same database from both components, can I for instance use a handle to the db (pointer) created in C++ in my C# project?


Solution

  • Yes, that would be possible. Of course, you'd need to share some data structures and handles (as you say). That way SQLite cannot detect any difference between managed code and native code. Therefore there can be no difference in behavior.

    Maybe it is easier if you just use PInvoke or C++/CLI to exchange data structures. Spinning up a whole SQL database just for passing around data in-process seems wasteful.