我正在尝试将一些较旧的 win32 代码封装在 C++/CLI 引用类中,以便可以更好地从 .NET 代码访问它。该类需要启动一个 Win32 线程并将指针作为线程参数传递给该类。代码看起来像这样:
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;
}
该线程确实需要是 Win32 线程,因为它从 MME 子系统接收消息。我尝试将 PlayThread 函数指针包装在 Interior_ptr 中,但编译器不允许这样做。
另外,我尝试使线程函数成为类方法,但编译器不允许在 ref 类方法上使用 _stdcall 修饰符。
你知道有办法处理这个问题吗?
I'm trying to encapsulate some older win32 code in a C++/CLI ref class to make it better accessible from .NET code. This class needs to start a Win32 thread and pass a pointer to the class as a thread parameter. 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 really needs to be a Win32 thread because it receives messages from the MME subsystem. I've tried wrapping the PlayThread function pointer in an interior_ptr, but the compiler wouldn't allow that.
Also, I've tried to make the thread function a class method, but the compiler does not allow the _stdcall modifier on ref class methods.
Do you know of a way to handle this?
发布评论
评论(1)
托管类使用“句柄”而不是引用来传递。您不能将托管类的句柄视为指针。您要做的是创建一个本机帮助程序类,该类保存托管类的句柄。然后将指向本机帮助程序的指针传递到线程启动函数中。像这样:
Managed classes are passed around using 'handles' instead of references. You can't treat a handle to a managed class like a pointer. What you're going to want to do is create a native helper class that holds a handle to the managed class. Then you pass a pointer to the native helper into the thread start function. Like this: