使用 win32 线程的 C++/CLI 引用类

发布于 2024-08-04 07:31:19 字数 588 浏览 1 评论 0 原文

我正在尝试将一些较旧的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈情不如逗狗 2024-08-11 07:31:19

托管类使用“句柄”而不是引用来传递。您不能将托管类的句柄视为指针。您要做的是创建一个本机帮助程序类,该类保存托管类的句柄。然后将指向本机帮助程序的指针传递到线程启动函数中。像这样:

#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;
}

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:

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文