• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            C#的Form通過(guò)CLI調(diào)用C++的DLL


            一 方法

                     C#的project調(diào)用C++的DLL,一般也有3中方法:
                     1)最簡(jiǎn)單的方法,通過(guò)PInvoke,但是只能調(diào)用全局function,不能調(diào)用Class。
                     2)通過(guò)COM封裝調(diào)用。
                     3)通過(guò)CLI作為中介,也即本文章所講的。

            二 實(shí)例

            1)假如我們有的Math的dll,
            class CPPDLL_API Math
            {
            public:
                
            static double Add(double x, double y);
                
            static double Multiply(double x, double y);
            }
            ;

            class CPPDLL_API AdvancedMath
            {
            public:
                
            static int Factorial(int x);
            }
            ;

            double Math::Add(double x, double y)
            {
                
            return x + y;
            }

            double Math::Multiply(double x, double y)
            {
                
            return x * y;
            }

            int AdvancedMath::Factorial(int x)
            {
                
            if(x <= 0)
                    
            return 0;
                
            if(1 == x)
                    
            return 1;
                
            return x * Factorial(x - 1);
            }

            2)C++的MFC的Dialog調(diào)用(比較煩,特別是MFC的controls太少了。各種String間的轉(zhuǎn)化也和累啊,我這里為了簡(jiǎn)化,不得不把vs05中默認(rèn)的unicode改為非unicode)
            #pragma comment(lib,"../debug/cppdll.lib")
            #include 
            "../cppdll/cppdll.h"

            void CCppTestDlg::OnBnClickedButton1()
            {
                
            switch(m_op)
                
            {
                
            case Add:
                    
            {            
                        CString xStr;
                        m_EditX.GetWindowText(xStr);
                        CString yStr;
                        m_EditY.GetWindowText(yStr);

                        
            double x = atof(xStr);
                        
            double y = atof(yStr);    
                        
            double sum =Math::Add(x, y); 
                        

                        CString sumStr;
                        sumStr.Format(
            "%f",sum);
                        m_EditSum.SetWindowText(sumStr);
                        
            break;
                    }

                
            case Multiply:
                    
            {
                        CString xStr;
                        m_EditX.GetWindowText(xStr);
                        CString yStr;
                        m_EditY.GetWindowText(yStr);

                        
            double x = atof(xStr.GetBuffer());
                        
            double y = atof(yStr);    
                        
            double sum = Math::Multiply(x, y);

                        CString sumStr;
                        sumStr.Format(
            "%f",sum);
                        m_EditSum.SetWindowText(sumStr);
                        
            break;
                    }

                
            case Factorial:
                    
            {
                        CString xStr;
                        m_EditX.GetWindowText(xStr);            

                        
            double x = atoi(xStr);            
                        
            double sum = AdvancedMath::Factorial(x);

                        CString sumStr;
                        sumStr.Format(
            "%f",sum);
                        m_EditSum.SetWindowText(sumStr);
                    
            break;
                    }

                
            default:
                    
            break;
                }

            }

            3)CLI的wrapper
            #pragma once

            class Math;
            class AdvancedMath;

            namespace CppMathLib
            {
            public ref class MathWrapper
            {
            public:
                
            static double Add(double x, double y);
                
                
            static double Multiply(double x, double y);
                
            }
            ;

            public ref class AdvancedMathWrapper
            {
            public:
                
            static int Factorial(int x);
            }
            ;
            }



            #include 
            "stdafx.h"
            #include 
            "MathWrapper.h"
            #pragma comment(lib, 
            "../debug/CppDLL.lib")
            #include 
            "../CppDLL/cppdll.h"

            using namespace CppMathLib;

             
            double MathWrapper::Add(double x, double y)
            {
                
            return Math::Add(x, y);
            }

             
            double MathWrapper::Multiply(double x, double y)
            {
                
            return Math::Multiply(x,y);
            }


             
            int AdvancedMathWrapper::Factorial(int x)
            {
                
            return AdvancedMath::Factorial(x);
            }

            4)C#的Form調(diào)用CLI的wrapper
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Text;
            using System.Windows.Forms;

            namespace CsharpTest
            {
                
            enum Operation
                
            {
                    Add,
                    Multiply,
                    Factorial,
                    None
                }

                
            public partial class Form1 : Form
                
            {
                    
            private Operation op = Operation.None;

                    
            public Form1()
                    
            {
                        InitializeComponent();     
                    }


                    
            private void radioButtonMultiply_CheckedChanged(object sender, EventArgs e)
                    
            {
                        op 
            = Operation.Multiply;

                        textBoxY.Enabled 
            = true;
                    }


                    
            private void radioButtonAdd_CheckedChanged(object sender, EventArgs e)
                    
            {
                        op 
            = Operation.Add;

                        textBoxY.Enabled 
            = true;
                    }


                    
            private void radioButtonFactorial_CheckedChanged(object sender, EventArgs e)
                    
            {
                        op 
            = Operation.Factorial;

                        textBoxY.Text 
            = "0";
                        textBoxY.Enabled 
            = false;
                    }


                    
            private void button1_Click(object sender, EventArgs e)
                    
            {           
                       
            switch(op)
                       
            {
                           
            case Operation.Add:
                               textBoxSum.Text 
            = CppMathLib.MathWrapper.Add(Double.Parse(textBoxX.Text), Double.Parse(textBoxY.Text)).ToString();
                               
            break;
                           
            case Operation.Multiply:
                               textBoxSum.Text 
            = CppMathLib.MathWrapper.Multiply(Double.Parse(textBoxX.Text), Double.Parse(textBoxY.Text)).ToString();
                               
            break;
                           
            case Operation.Factorial:
                               textBoxSum.Text 
            = CppMathLib.AdvancedMathWrapper.Factorial(Int32.Parse(textBoxX.Text)).ToString();
                               
            break;
                           
            default:
                               
            break;
                       }



                    }


                    
            private void button2_Click(object sender, EventArgs e)
                    
            {
                        
            this.Close();
                    }

                }

            }

            三 截圖比較
                 
             
                 

                 前面的是C++的MFC的dialog,后面的C#的Form,看起來(lái)一樣哦,就是開(kāi)發(fā)速度不同!


            四 代碼下載:http://www.shnenglu.com/Files/mzty/CsharpCallCppByCLI.rar

            posted on 2007-12-25 19:14 夢(mèng)在天涯 閱讀(7913) 評(píng)論(2)  編輯 收藏 引用 所屬分類: CPlusPlusC#/.NETManage c++ /CLI

            評(píng)論

            # re: C#的Form通過(guò)CLI調(diào)用C++的DLL[未登錄](méi) 2008-09-26 10:06 笑笑生

            請(qǐng)問(wèn):
            對(duì)于DLL中的對(duì)象方法,需要實(shí)例化才可以使用,在這里該如何實(shí)現(xiàn)呢?  回復(fù)  更多評(píng)論   

            # re: C#的Form通過(guò)CLI調(diào)用C++的DLL[未登錄](méi) 2015-07-30 19:34 jeffrey

            錯(cuò)誤 6 當(dāng)前上下文中不存在名稱“CppMathLib” C:\.......\Desktop\CsharpCallCppByCLI\CsharpTest\Form1.cs 54

            請(qǐng)問(wèn)是什么問(wèn)題,親手編譯請(qǐng)教。多謝!
              回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804603
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            成人国内精品久久久久影院| 久久中文字幕精品| 99精品久久精品一区二区| 久久国产精品二国产精品| 久久九九全国免费| 97精品久久天干天天天按摩| 午夜精品久久久久久| 色婷婷久久综合中文久久一本| 国内精品伊人久久久久av一坑| 国产亚洲色婷婷久久99精品| 少妇被又大又粗又爽毛片久久黑人| 国产99精品久久| 亚洲精品高清国产一线久久 | 日韩精品久久无码人妻中文字幕| 久久久久亚洲精品日久生情| 久久久久亚洲AV成人片 | 久久精品国产亚洲av麻豆图片| 99久久婷婷国产综合精品草原| 国产成人精品久久| 久久久久亚洲av成人网人人软件 | 亚洲国产成人久久综合区| 久久久久波多野结衣高潮| 性做久久久久久免费观看| 久久精品国产只有精品66| 亚洲精品午夜国产VA久久成人 | 亚洲va久久久噜噜噜久久| 亚洲AⅤ优女AV综合久久久| 免费精品久久久久久中文字幕 | 欧美精品九九99久久在观看| 久久久久国色AV免费看图片| 国产精品va久久久久久久| 亚洲国产精品久久久久婷婷老年| 97r久久精品国产99国产精| 996久久国产精品线观看| 日韩欧美亚洲综合久久影院d3| 99久久免费国产精品热| 亚洲午夜久久影院| 久久国产精品免费一区| 亚洲国产成人久久笫一页| 久久精品国产久精国产果冻传媒| 精品久久人人爽天天玩人人妻|