C ++ / CLI ref class using win32 stream
I am trying to encapsulate some old win32 code into a C ++ / CLI ref class to make it more accessible from .NET code. This class should start a Win32 thread and pass a pointer to the class as a parameter to the thread. The code looks something like this:
ref class MmePlayer
{
int StartPlayback()
{
hPlayThread = CreateThread(NULL, 0, PlayThread, this, 0, &PlayThreadId);
}
};
static DWORD WINAPI PlayThread(LPVOID pThreadParam)
{
// Get a pointer to the object that started the thread
MmePlayer^ Me = pThreadParam;
}
The thread must really be a Win32 thread because it receives messages from the MME. I tried to wrap the PlayThread function pointer in inner_ptr, but the compiler didn't allow it. Also, I tried to make the stream function a class method, but the compiler doesn't allow the _stdcall modifier on the ref class methods. Do you know how to deal with this?
source to share
Managed classes are passed using "descriptors" instead of references. You cannot treat a handle to a managed class like a pointer. What you need to do is create your own helper class that contains a handle to the managed class. Then you pass a pointer to the native helper to the thread start function. Like this:
#include <msclr/auto_gcroot.h>
using msclr::auto_gcroot;
ref class MmePlayer;
class MmeHelper
{
auto_gcroot<MmePlayer^> myPlayer;
};
ref class MmePlayer
{
int StartPlayback()
{
myHelper = new MmeHelper();
myHelper->myPlayer = this;
hPlayThread = CreateThread(NULL, 0, PlayThread, myHelper, 0, &PlayThreadId);
}
MmeHelper * myHelper;
};
static DWORD WINAPI PlayThread(LPVOID pThreadParam)
{
// Get a pointer to the object that started the thread
MmeHelper* helper = pThreadParam;
}
source to share