代码之家  ›  专栏  ›  技术社区  ›  Jeff L

在C++应用程序中使用C++作为DLL移植(非托管)C++到C语言

  •  6
  • Jeff L  · 技术社区  · 15 年前

    我有一个用普通的老C++编写的代码库(NO.NET/托管代码),我正在移植使用这个代码的应用程序。我面临两个选择:

    1. 重写C++中的C++代码,实现相同的功能;
    2. 将C++编译为DLL,并将其作为C语言应用程序中的库。

    我对C比较陌生,对在C应用程序中使用非托管代码库的含义也相当陌生(或者如果有的话)。代码本身的大小是中等的;用C重写可能只需要几天,但我的想法是,将代码保持原样将允许我在其他应用程序中使用它(并在Unix上编译它,等等)。

    做这个决定时,我应该注意什么?在C应用程序中使用DLL是否存在任何主要缺点或缺陷?

    3 回复  |  直到 15 年前
        1
  •  6
  •   Reed Copsey    15 年前

    我会用C++/CLI制作一个包装库来将库暴露到C。这可以使库保持不变,只需从.NET包装它即可使用,这两个选项都是最好的。

        2
  •  2
  •   Randolpho    15 年前

    我发现一件有用的事情就是深入研究 C++/CLI 处理非托管C++库时。使用C++/CLI创建托管包装,并从C代码中调用它。托管包装器可以将库(我假设它是静态链接的)包含在其DLL中,而对于C代码,您只需要一个项目引用。

        3
  •  0
  •   Amy    15 年前

    在C++/CLI中不必编写包装器。您可以直接使用C中的平台调用:

    http://msdn.microsoft.com/en-us/library/aa288468%28VS.71%29.aspx

    编辑:如果你使用C++/CLI来做,你需要做LoadLibrary调用和创建函数指针。这在C中要容易得多。这是来自上面链接的msdn教程,但有我自己添加的注释:

    class PlatformInvokeTest
    {
        [DllImport("msvcrt.dll")]  // Specify the DLL we're importing from
        public static extern int puts(string c); // This matches the signature of the DLL function.  The CLR automatically marshals C++ types to C# types.
        [DllImport("msvcrt.dll")]
        internal static extern int _flushall();
    
        public static void Main() 
        {
            puts("Test");
            _flushall();
        }
    }
    

    编辑:复杂类型也可以进行封送处理,但需要定义结构。这个例子取自我自己调用gdi+的代码。我剪了一点。

    private static int SRCCOPY = 0x00CC0020;
    private static uint BI_RGB = 0;
    private static uint DIB_RGB_COLORS = 0;
    
    
    [DllImport("gdi32.dll")]
    private static extern bool DeleteObject(IntPtr hObject);
    
    [StructLayout(LayoutKind.Sequential)]
    private struct BITMAPINFO
    {
        public uint biSize;
        public int biWidth;
        public int biHeight;
        public short biPlanes;
        public short biBitCount;
        public uint biCompression;
        public uint biSizeImage;
        public int biXPelsPerMeter;
        public int biYPelsPerMeter;
        public uint biClrUsed;
        public uint biClrImportant;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
        public uint[] cols;
    }
    
    public static Bitmap Downsample(Bitmap input, int bpp)
    {
        Bitmap retval = null;
    
        // We will call into this GDI functionality from C#. Our plan:
        // (1) Convert our Bitmap into a GDI hbitmap (ie. copy unmanaged->managed)
        // (2) Create a GDI monochrome hbitmap
        // (3) Use GDI "BitBlt" function to copy from hbitmap into monochrome (as above)
        // (4) Convert the monochrone hbitmap into a Bitmap (ie. copy unmanaged->managed)
    
        IntPtr inputHandle = input.GetHbitmap();
    
        //
        // Step (2): create the monochrome bitmap.
        //
        BITMAPINFO bmi = new BITMAPINFO();
        bmi.biSize = 40;  // the size of the BITMAPHEADERINFO struct
        bmi.biWidth = input.Width;
        bmi.biHeight = input.Height;
        bmi.biPlanes = 1;
        bmi.biBitCount = (short)bpp; // 1bpp or 8bpp
        bmi.biCompression = BI_RGB;
        bmi.biSizeImage = (uint)(((input.Width + 7) & 0xFFFFFFF8) * input.Height / 8);
        bmi.biXPelsPerMeter = 0; // not really important
        bmi.biYPelsPerMeter = 0; // not really important
    
        //
        // Create the color palette.
        //
        uint numColors = (uint)1 << bpp; // 2 colors for 1bpp; 256 colors for 8bpp
        bmi.biClrUsed = numColors;
        bmi.biClrImportant = numColors;
        bmi.cols = new uint[256];
    
        if (bpp == 1)
        {
            bmi.cols[0] = MAKERGB(0, 0, 0);
            bmi.cols[1] = MAKERGB(255, 255, 255);
        }
        else
        {
            for (int i = 0; i < numColors; i++)
            {
                bmi.cols[i] = MAKERGB(i, i, i);
            }
        }
    
        // 
        // Now create the indexed bitmap
        //
        IntPtr bits0;
        IntPtr indexedBitmapHandle = CreateDIBSection(IntPtr.Zero, ref bmi, DIB_RGB_COLORS, out bits0, IntPtr.Zero, 0);
        IntPtr sourceDC = GetDC(IntPtr.Zero);
        IntPtr hdc = CreateCompatibleDC(sourceDC);
        IntPtr hdc0 = CreateCompatibleDC(sourceDC);
    
        SelectObject(hdc, inputHandle);
        SelectObject(hdc0, indexedBitmapHandle);
    
        BitBlt(hdc0, 0, 0, input.Width, input.Height, hdc, 0, 0, SRCCOPY);
    
        retval = Bitmap.FromHbitmap(indexedBitmapHandle);
    
        //
        // Dispose of the crud
        //
        DeleteDC(hdc);
        DeleteDC(hdc0);
        ReleaseDC(IntPtr.Zero, sourceDC);
        DeleteObject(inputHandle);
        DeleteObject(indexedBitmapHandle);
    
        return retval;
    }