简体   繁体   中英

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

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

how i can do it ?

thank's in advance

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

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, 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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