P/调用。如何从 C# 中通过编组调用非托管方法?

发布于 2025-01-04 04:02:49 字数 622 浏览 0 评论 0原文

我的 P/Invoke 遇到问题。我正在从 C# 代码调用 .dll(在 C++ 上实现)。有一个类,包含以下方法:

virtual AudioFileList *API  CreateAudioFileList ()=0;
virtual bool API  DisposeAudioFileList (AudioFileList *iAudioFileList)=0;

AudioFileList 类看起来像:

virtual bool API  GetFile (long index, std::string *oPath, AudioFileInfo *fileInfo)=0;
virtual long API  GetNumberFiles ()=0; 

所以,问题是我如何调用 CreateAudioFileList< /code>方法,然后将结果从 C# 代码传递到 DisposeAudioFileList? 谢谢!

I've got a problem with P/Invoke. I'm calling .dll (implemented on c++) from c# code. There is a class, that contains the next methods:

virtual AudioFileList *API  CreateAudioFileList ()=0;
virtual bool API  DisposeAudioFileList (AudioFileList *iAudioFileList)=0;

AudioFileList class looks like:

virtual bool API  GetFile (long index, std::string *oPath, AudioFileInfo *fileInfo)=0;
virtual long API  GetNumberFiles ()=0; 

So, the question is how can I call CreateAudioFileList method and than pass the result to DisposeAudioFileList from C# code?
Thanks!

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

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

发布评论

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

评论(4

挥剑断情 2025-01-11 04:02:49

我一直使用它来生成静态 extern malarkey

http://clrinterop.codeplex.com/releases /查看/14120

I use this all the time to generate my static extern malarkey

http://clrinterop.codeplex.com/releases/view/14120

可可 2025-01-11 04:02:49

不幸的是,没有简单的方法可以通过 P/Invoke 调用本机 C++ DLL 模块中的类。然而,Visual C++ 团队博客文章提供了解决方案,但它很复杂。

您可能会发现另一个有用的链接:http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/5df04db1-bbc8-4389-b752-802bc84148fe/

Unfortunately, there is no easy way to call the classes in a native C++ DLL module through P/Invoke. Yet there's Visual C++ Team Blog post with a solution, but it is complicated.

There's one more link you might find useful: http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/5df04db1-bbc8-4389-b752-802bc84148fe/

痴梦一场 2025-01-11 04:02:49

CodeProject 上的这篇文章解释了如何处理这种编组。

如何封送 C++ 类

This article on CodeProject explains how to deal with this sort of marshalling.

How to Marshal a C++ Class

梦里兽 2025-01-11 04:02:49

由于名称损坏,您不能。您应该投资学习 C++/CLI。这将允许您创建一个中间层,该层提供适当的封送处理并且不受 C++ 中的名称修饰的抑制。

下面是它在 C++/CLI 中的样子(当然未经测试):

.h

public ref class ManagedAudioFileList
{
private:
    const AudioFileList* Native;
    // Replace AudioFileListManager with the class containing
    // the CreateAudioFileList and DisposeAudioFileList methods.
    const AudioFileListManager* Manager;

public:
    ManagedAudioFileList(void);
    !ManagedAudioFileList(void);
    ~ManagedAudioFileList(void);
    // Insert various methods exposed by AudioFileList here.
};

.cpp

ManagedAudioFileList::ManagedAudioFileList(void)
{
    // Replace AudioFileListManager with the class containing the
    // CreateAudioFileList and DisposeAudioFileList methods.
    Manager = new AudioFileListManager();
    Native = Manager->CreateAudioFileList();
}

~ManagedAudioFileList::ManagedAudioFileList()
{
    Manager->DisposeAudioFileList(Native);
    delete Manager;
}

!ManagedAudioFileList::ManagedAudioFileList()
{
}

// Wrap various methods exposed by AudioFileList.

You can't, due to name mangling. You should invest in learning C++/CLI. This will allow you to create an intermediary layer that provides proper marshaling and isn't inhibited by name mangling in C++.

Here's what it might look like in C++/CLI (untested, of course):

.h

public ref class ManagedAudioFileList
{
private:
    const AudioFileList* Native;
    // Replace AudioFileListManager with the class containing
    // the CreateAudioFileList and DisposeAudioFileList methods.
    const AudioFileListManager* Manager;

public:
    ManagedAudioFileList(void);
    !ManagedAudioFileList(void);
    ~ManagedAudioFileList(void);
    // Insert various methods exposed by AudioFileList here.
};

.cpp

ManagedAudioFileList::ManagedAudioFileList(void)
{
    // Replace AudioFileListManager with the class containing the
    // CreateAudioFileList and DisposeAudioFileList methods.
    Manager = new AudioFileListManager();
    Native = Manager->CreateAudioFileList();
}

~ManagedAudioFileList::ManagedAudioFileList()
{
    Manager->DisposeAudioFileList(Native);
    delete Manager;
}

!ManagedAudioFileList::ManagedAudioFileList()
{
}

// Wrap various methods exposed by AudioFileList.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文