• <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>

            力為的技術博客

            聯系 聚合 管理
              154 Posts :: 1 Stories :: 561 Comments :: 0 Trackbacks

             一、問題:

            現在有一個C++類庫Mythma.dll,需要Python用戶也可以調用這些服務,所以需要用封裝其為Python擴展模塊Mythma.pyd。
            現在有可執行程序PythonConsole.exe,依賴Mythma.dll模塊,希望PythonConsole.exe可以調用Python腳本,所以PythonConsole.exe中需要嵌入Python解析器。

            此時,PythonConsole.exe既可以直接訪問Mythma.dll,又可以通過Python腳本訪問Mythma.dll。關系圖如下:


            二、實現
            1、假設Mythma.dll中的類為CHelloWorld

             

            class MythmaAPI CHelloWorld
            {
            public
            :
               CHelloWorld(
            void
            );
               
            ~CHelloWorld(void
            );

               
            void SetId(int nId) { m_Id = nId;}

               
            int  GetId() const return m_Id; }

            private:
               
            int
             m_Id;
            }
            ;

             

            2、用Boost.Python將C++模塊封裝,使得Python腳本可以調用C++的功能

                如:

            #include "HelloWorld.h"
            #include 
            <boost/python.hpp>
            using namespace boost::python;

            BOOST_PYTHON_MODULE(MythmaPy)
            {
               class_
            <CHelloWorld>("CHelloWorld")
                  .def(
            "SetId"&CHelloWorld::SetId)
                  .def(
            "GetId"&CHelloWorld::GetId)
                  ;
            }

            此時,可以用Python腳本訪問Mythma.dll中的類,axxscript.py文件中的內容為:

            #import MythmaPy
            from MythmaPy import *
            world 
            = CHelloWorld()
            world.SetId(
            100)
            print world.GetId()


             

            3、在C++程序里內嵌Python解析器,使得C++可以調用Python腳本

            建立一個控制臺程序PythonConsole.exe,用來嵌入Python解析器

            int main(int argc, char **argv)
            {
               
            // Initialize the interpreter
               Py_Initialize();

               
            if (python::handle_exception(exec_mythma))
               
            {
                  
            if (PyErr_Occurred())
                  
            {
                     BOOST_ERROR(
            "Python Error detected");
                     PyErr_Print();
                  }

                  
            else
                  
            {
                     BOOST_ERROR(
            "A C++ exception was thrown  for which "
                        
            "there was no exception translator registered.");
                  }

               }


               
            char ch;
               std::cin 
            >> ch;
               
            // Boost.Python doesn't support Py_Finalize yet, so don't call it!
               return boost::report_errors();
            }


            執行調用Python腳本的過程如下:

            void exec_mythma()
            {
               std::cout 
            << "exec extension module Mythma" << std::endl;

               python::dict global;
               python::
            object result = python::exec_file(
                  
            ".\\axxscript.py", global, global);


               python::
            object world = global["world"];
               CHelloWorld
            & py = python::extract<CHelloWorld&>(world) BOOST_EXTRACT_WORKAROUND;

               std::cout 
            << py.GetId() << std::endl;

               std::cout 
            << "success!" << std::endl;
            }


            三、癥狀


            通過執行PythonConsole.exe無法正常調用腳本,錯誤信息如下:


            假如直接運行腳本,結果正確。不知各位朋友發現問題的原因沒有?有什么解決方法?


            四、測試環境
            VS2008
            Python2.5
            Boost 1.38
            測試工程下載


            五:更新
            終于找到問題的原因,詳見這里
            posted on 2009-09-13 16:16 力為 閱讀(7184) 評論(2)  編輯 收藏 引用 所屬分類: 5. GP&STL&BOOSTTools

            評論

            # re: Boost.Python與C++互操作的問題(更新) 2010-06-09 18:10 iamonkey
            我正在用pyactivemq連接activemq,代碼如下:

            message = session.createMapMessage()
            message.setBytes('aaa', 'bbb')

            pydev 報出如下錯誤,好像是類型不對,就是說最后一個參數'bbb',不是std::vector<unsigned char類型,這種類型只有C語言有,我該如何處理?懇請各位大俠幫忙。

            MapMessage.setBytes(MapMessage, str, str)
            did not match C++ signature:
            setBytes(class cms::MapMessage {lvalue}, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >, class std::vector<unsigned char,class std::allocator<unsigned char> >)
              回復  更多評論
              

            # re: Boost.Python與C++互操作的問題(更新)[未登錄] 2010-06-11 11:08 力為
            @iamonkey
            用個Python的數組試一下吧。
            'bbb'估計按照string來處理了  回復  更多評論
              

            国产精品亚洲综合久久| 久久国产精品-国产精品| 久久香综合精品久久伊人| 亚洲AV无码久久| 99久久精品免费观看国产| 久久免费视频6| 久久天天躁狠狠躁夜夜2020一| 狠狠色噜噜狠狠狠狠狠色综合久久| 2021久久精品国产99国产精品| 久久国产免费直播| 久久99精品久久久久久久不卡| 色悠久久久久久久综合网| 久久精品一区二区国产| 久久精品国产一区二区| 精品一二三区久久aaa片| 久久免费精品视频| 久久夜色精品国产欧美乱| 国产真实乱对白精彩久久| 久久99国产精品久久99| 欧美日韩精品久久久久| 国产一级做a爰片久久毛片| 精产国品久久一二三产区区别| 久久精品国产国产精品四凭| 亚洲AV无码久久| 国产偷久久久精品专区| 日批日出水久久亚洲精品tv| 狠狠色丁香久久综合五月| 亚洲AV日韩精品久久久久| 武侠古典久久婷婷狼人伊人| 91精品久久久久久无码| 97久久久精品综合88久久| 午夜人妻久久久久久久久| 中文国产成人精品久久亚洲精品AⅤ无码精品| 久久久青草久久久青草| 久久人人爽人人爽人人片AV不| 狠狠综合久久AV一区二区三区| 午夜精品久久久久久久无码| 久久久久这里只有精品| 久久无码人妻精品一区二区三区| 久久本道综合久久伊人| 久久久久久青草大香综合精品|