Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data from a C process to a C++ process

This is my first time posting a question here, I usually find answers in the archive but I'm stumped this time.

I'm grabbing data off of a joystick using some code from the vendor that uses Windows Driver Kit. The data is in the form of an array with 6 elements (it's a 6 degree of freedom mouse).

I had already written the code that needs to grab the data and it is in C++...it uses the standard library a lot with vectors and what not. It seems that using the standard library with WDK is a big head ache that I spent a couple days trying to get to work but failed. My next idea was to use boost::interprocess but this to is difficult to use with WDK for the same reasons.

I'm wondering if there is a way to share memory between a C process and C++ process. I would like to write the array to memory using a C program and read it in from a C++ program. It needs to happen very fast and there should be a way to make sure I don't read it in the middle of a write (mutex?).

Any ideas or suggestions are welcome.

EDIT I made a DLL instead, now I just have a DLL that has a getValues() function that I can call from my C++ project. I had to use the pimpl idiom to hide the c stuff though. Thanks for the help guys!!

like image 215
Airuno2L Avatar asked Jan 09 '13 14:01

Airuno2L


1 Answers

Perhaps I missed something, but it looks like you created a process to retrieve the joystick data. To save yourself some trouble, replace that process with the C DLL suggested in the comments. Your main C++ application can then simply call a function within that DLL to retrieve the joystick data without even worrying about locks or inter-process communication.

Of course, if you do need two processes, you'll need to use shared memory in the DLL and process-level locks. Shared memory is needed because DLLs are loaded separately into each process's virtual space; nothing is shared, hence the need for shared-memory.

like image 68
Ioan Avatar answered Oct 19 '22 17:10

Ioan