C#调用C++写dll的接口函数或者类对象麻烦详细说明一下谢谢!

解决方案 »

  1.   

    补充说明一下,还包括接口类的继承,比如,需要在C#里面继承实现一个C++ dll的一个接口类同时实现它的接口函数C#不熟悉
      

  2.   

    [ComImport(),Guid("F490EB00-1240-11D1-9888-006097DEACF9"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IActiveDesktop
    {
    void ApplyChanges(ActiveDesktopApply dwFlags);
    void GetWallpaper([MarshalAs(UnmanagedType.LPWStr)]string pwszWallpaper, int cchWallpaper, int dwReserved);
    void SetWallpaper([MarshalAs(UnmanagedType.LPWStr)]string pwszWallpaper, int dwReserved);
    void GetWallpaperOptions(ref WallpaperOption pwpo, int dwReserved);
    void SetWallpaperOptions([In]ref WallpaperOption pwpo, int dwReserved);
    void GetPattern([MarshalAs(UnmanagedType.LPWStr)]string pwszPattern, int cchPattern, int dwReserved);
    void SetPattern([MarshalAs(UnmanagedType.LPWStr)]string pwszPattern, int dwReserved);
    void GetDesktopItemOptions(ref ComponentOption pco, int dwReserved);
    void SetDesktopItemOptions([In]ref ComponentOption pco, int dwReserved);
    void AddDesktopItem([In]ref Component pcomp, int dwReserved);
    void AddDesktopItemWithUI(IntPtr hwnd, [In]ref Component pcomp, DeskTopItemAddWithUI dwFlags);
    void ModifyDesktopItem([In]ref Component pcomp, ComponentElement dwFlags);
    void RemoveDesktopItem([In]ref Component pcomp, int dwReserved);
    void GetDesktopItemCount(out int lpiCount, int dwReserved);
    void GetDesktopItem(int nComponent, ref Component pcomp, int dwReserved);
    void GetDesktopItemByID(IntPtr dwID, ref Component pcomp, int dwReserved);
    void GenerateDesktopItemHtml([MarshalAs(UnmanagedType.LPWStr)]string pwszFileName, [In]ref Component pcomp, int dwReserved);
    void AddUrl(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]string pszSource, [In] ref Component pcomp, AddUrl dwFlags);
    void GetDesktopItemBySource([MarshalAs(UnmanagedType.LPWStr)]string pwszSource, ref Component pcomp, int dwReserved);
    }这是一个从C#导入C++接口的例子,其实如果的C++类是标准的COM,直接使用regsvr32注册到系统里,然后在C#项目上添加引用,选COM引用,找到对应的注册就可以了还可以使用一个命令,想不起来了,可以生成interop的接口文件
      

  3.   

    是的,c#调用C++接口函数,一般先封装成Dll文件,再在C#的源代码中引入,申明其接口,最后调用。例子:namespace CSHARP_POSTEK_PRINT
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                PrintLab.OpenPort("POSTEK C168 200DPI");//打开打印机端口
                PrintLab.PTK_ClearBuffer();//清空缓冲区
                PrintLab.PTK_SetPrintSpeed(3);//设置打印速度
                PrintLab.PTK_SetDarkness(8);//设置打印黑度
                PrintLab.PTK_SetLabelHeight(600, 0);//设置标签的高度和定位间隙\黑线\穿孔的高度
                PrintLab.PTK_SetLabelWidth(800);//设置标签的宽度
                PrintLab.PTK_DrawTextTrueTypeW(200, 300, 40, 40, "宋体", 1, 400, false, true, true, "1", "12456789");//打印一行 TrueType Font文字
                PrintLab.PTK_DrawBarcode(100, 20, 0, "1", 3, 3, 80, 'N', "12345");//打印一个条码
                PrintLab.PTK_PrintLabel(1, 1);//命令打印机执行打印工作
                PrintLab.ClosePort();//关闭打印机端口
            }
        }
    }
    public class PrintLab
    {
        [DllImport("WINPSK.dll")]
        public static extern int OpenPort(string printname);
        [DllImport("WINPSK.dll")]
        public static extern int PTK_SetPrintSpeed(uint px);
        [DllImport("WINPSK.dll")]
        public static extern int PTK_SetDarkness(uint id);
        [DllImport("WINPSK.dll")]
        public static extern int ClosePort();
        [DllImport("WINPSK.dll")]
        public static extern int PTK_PrintLabel(uint number, uint cpnumber);
        [DllImport("WINPSK.dll")]
        public static extern int PTK_DrawTextTrueTypeW
                                            (int x, int y, int FHeight,
                                            int FWidth, string FType,
                                            int Fspin, int FWeight,
                                            bool FItalic, bool FUnline,
                                            bool FStrikeOut,
                                            string id_name,
                                            string data);
        [DllImport("WINPSK.dll")]
        public static extern int PTK_DrawBarcode(uint px,
                                        uint py,
                                        uint pdirec,
                                        string pCode,
                                        uint pHorizontal,
                                        uint pVertical,
                                        uint pbright,
                                        char ptext,
                                        string pstr);
        [DllImport("WINPSK.dll")]
        public static extern int PTK_SetLabelHeight(uint lheight, uint gapH);
        [DllImport("WINPSK.dll")]
        public static extern int PTK_SetLabelWidth(uint lwidth);
        [DllImport("WINPSK.dll")]
        public static extern int PTK_ClearBuffer();
        [DllImport("WINPSK.dll")]
        public static extern int PTK_DrawRectangle(uint px, uint py, uint thickness, uint pEx, uint pEy);
        [DllImport("WINPSK.dll")]
        public static extern int PTK_DrawLineOr(uint px, uint py, uint pLength, uint pH);    
    }
      

  4.   

    http://topic.csdn.net/u/20091113/16/c65048a2-608a-4889-9f0c-fb2d69462cab.htmlhttp://topic.csdn.net/u/20091208/19/490341bb-b8eb-428a-b1c7-21206dbb4323.html