• <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++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            C#的Form通過CLI調用C++的DLL


            一 方法

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

            二 實例

            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調用(比較煩,特別是MFC的controls太少了。各種String間的轉化也和累啊,我這里為了簡化,不得不把vs05中默認的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調用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,看起來一樣哦,就是開發速度不同!


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

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

            評論

            # re: C#的Form通過CLI調用C++的DLL[未登錄] 2008-09-26 10:06 笑笑生

            請問:
            對于DLL中的對象方法,需要實例化才可以使用,在這里該如何實現呢?  回復  更多評論   

            # re: C#的Form通過CLI調用C++的DLL[未登錄] 2015-07-30 19:34 jeffrey

            錯誤 6 當前上下文中不存在名稱“CppMathLib” C:\.......\Desktop\CsharpCallCppByCLI\CsharpTest\Form1.cs 54

            請問是什么問題,親手編譯請教。多謝!
              回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804159
            • 排名 - 5

            最新評論

            閱讀排行榜

            亚洲综合久久久| 久久久无码精品亚洲日韩京东传媒 | 久久国产精品视频| 久久国产精品成人片免费| 久久天天躁狠狠躁夜夜2020老熟妇| 国产三级久久久精品麻豆三级| 综合久久精品色| 怡红院日本一道日本久久 | 久久精品无码一区二区日韩AV| 久久精品国产99国产精品澳门| 精品999久久久久久中文字幕| 久久精品国产一区二区三区日韩| 性色欲网站人妻丰满中文久久不卡 | 久久er国产精品免费观看8| 国产精品美女久久久久AV福利| 九九精品99久久久香蕉| 999久久久免费精品国产| 日本三级久久网| 香蕉aa三级久久毛片| 久久久久久久91精品免费观看| 久久久久亚洲精品日久生情| 久久精品国产精品亚洲毛片| 秋霞久久国产精品电影院| 久久99精品久久久久久秒播| 亚洲午夜精品久久久久久浪潮| 久久久久人妻一区二区三区 | 亚洲AV乱码久久精品蜜桃| 久久精品人人做人人妻人人玩| 亚洲欧美日韩中文久久| 2021久久国自产拍精品| 国产91色综合久久免费| 久久综合综合久久97色| 天堂无码久久综合东京热| 国产偷久久久精品专区| 99热都是精品久久久久久| 久久久久免费精品国产| 久久精品国产亚洲7777| 久久成人国产精品| 久久精品中文无码资源站| 午夜精品久久影院蜜桃| 91亚洲国产成人久久精品|