托管类使用“句柄”而不是引用传递。不能像托管指针那样处理托管类的句柄。您要做的是创建一个本机帮助程序类,该类包含托管类的句柄。然后将指向本机助手的指针传递到线程启动函数中。这样地:
#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;
}