简体   繁体   English

如何将C ++ DLL绑定到我的C#程序-WinCE

[英]How to bind c++ dll to my C# program - winCE

i need to bind C++ dll to my C# WinCE program. 我需要将C ++ dll绑定到我的C#WinCE程序。 (scanner dll) (扫描仪dll)

how i can do it ? 我该怎么办?

thank's in advance 提前致谢

You need to use Interop to call into unmanaged code. 您需要使用Interop来调用非托管代码。

using System.Runtime.InteropServices; // DllImport
public class Win32 {
  [DllImport("User32.Dll")]
  public static extern void SetWindowText(int h, String s);
}

Here is an article that discusses the topic in detail (also where the code is sourced from). 这是一篇详细讨论该主题的文章(以及代码的来源)。

http://msdn.microsoft.com/en-us/magazine/cc301501.aspx http://msdn.microsoft.com/zh-CN/magazine/cc301501.aspx

An alternative to InterOp is to write a C++ DLL using CLR extensions which acts as a wrapper to the traditional C++ DLL. InterOp的替代方法是使用CLR扩展编写C ++ DLL,用作传统C ++ DLL的包装。 This gives you a chance to handle unusual types, eg custom structures or classes, if Marshaling isn't going to work. 如果封送处理不起作用,这使您有机会处理异常类型,例如自定义结构或类。 (According to MSDN you can extend the Marshaling support ( http://msdn.microsoft.com/en-us/library/bb531313.aspx ) but I haven't tried this personally, and depending on what you're doing it might be a lot of work). (根据MSDN,您可以扩展封送处理支持( http://msdn.microsoft.com/zh-cn/library/bb531313.aspx ),但我没有亲自尝试过此操作,具体取决于您在做什么要做很多工作)。

For example if you want to access a DLL which exports a class, you can have a wrapper DLL which owns an instance of the C++ class and defines a .NET class which maps onto the C++ class. 例如,如果要访问导出类的DLL,则可以有一个包装DLL,该DLL拥有C ++类的实例并定义一个映射到C ++类的.NET类。 For example, here's a snippet from a C++/CLR DLL which we use to make one of our old C++ DLLs available in .NET: 例如,这是一个C ++ / CLR DLL的片段,我们用来在.NET中提供一个旧的C ++ DLL:

// This is the constructor for the CLR (managed) object
FileInf::FileInf()
{
    // Create the C++ (unmanaged) object
    m_pFileInf = gcnew DILib::FileInf();
}

// This is a managed interface which replicates the old 
// unmanaged functionality
bool FileInf::IsDirectory()
{
    return m_pFileInf->IsDirectory();
}

I'd say if InterOp works then stick with it, but I'm not sure if it's the best way to solve every C++ / .NET interfacing problem, and this is an alternative. 我想说InterOp是否可以工作,然后坚持下去,但是我不确定这是否是解决每个C ++ / .NET接口问题的最佳方法,这是一种替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM