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

落爺英雄遲暮 提交于 2021-02-08 06:50:00

问题


i need to bind C++ dll to my C# WinCE program. (scanner dll)

how i can do it ?

thank's in advance


回答1:


You need to use Interop to call into unmanaged code.

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




回答2:


An alternative to InterOp is to write a C++ DLL using CLR extensions which acts as a wrapper to the traditional C++ DLL. This gives you a chance to handle unusual types, e.g. 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).

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. 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:

// 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.



来源:https://stackoverflow.com/questions/1909127/how-to-bind-c-dll-to-my-c-sharp-program-wince

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!