使用dos 作为中介实现cpython 和c# 交互

最近在使用python 处理一些图形的东西。

实现:对一些512 的图进行像素遍历RGBA 变量, 查询通道不是 255 255 255 颜色 的矩阵,进行切图到空白

之前使用c#进行 确实快10 倍左右, python 要等很长时间, 

c++ 我自己不上手,所以想用python 调用c#dll 。程序使用cpython 写的,后来查询 可以使用ironpython 和dotnet进行交换,

这些不过,家里测试没有问题,可以到公司 总是不能创建工程, 我也是醉了。

 https://social.msdn.microsoft.com/Forums/zh-CN/80e5d654-caf4-48c8-9c19-a6e2ae5f22bb/vs

安装这个 我也找不出来什么问题。

搞了半天, 想这样  c++ 使用CLR 封装 c# dll  python 在调用c++的dll  这样一来下路不知道效率如何?

就开始了

using System;
using System.Collections.Generic;
using System.Text;

namespace sen
{
    public class Class1
    {
        public  string  each ()
        {

            return "ceshi";
        }
    }
}

然后c++ 进行封装 

#pragma once

using namespace System;
#using "..\debug\sen.dll"
using namespace sen ;
using namespace std ;


namespace lr {

public ref class Clname
    {
        // TODO: 在此处添加此类的方法。
    public: char* send ()
        {
            Class1 ^ci= gcnew Class1();
    
            System::String ^c = gcnew System::String(ci->each());
            char* ss = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(c);
            //printf (ss);
            return ss ;
        }
        

    };
}

编译是通过了。 

麻烦出现在下一步, c++ 给python 调用还要绑定  看了看很麻烦

http://blog.csdn.net/fxjtoday/article/details/6059874

最后感觉有时间在研究吧, 感觉这样一部下路 在没有纯python快 那真实费力不讨好

、因为就是一个调用关系 ,后来想想 干脆使用  c#控制台 

给python调用

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        static int  Main(string[] args)
        {
            if(args.Length>0){
                foreach (string i in args)
                {
                    Console.WriteLine(i);
                }
            }
            if (args[1]=="11")
            {
                
                Console.WriteLine(50);
                return 50; 
            }else 
            {
                return 20;
            }

        }
    }
}

编译成exe 给python 调用

 

#coding:utf-8

print "ss"


#from ctypes import*

#filename = r"N:\E\egret\Labs\projects\lr\Debug\lr.dll"

#func  = cdll.LoadLibrary(filename)
#print (func.send)
import os 

cmd = r"F:\c#\c#2005\ConsoleApplication6\ConsoleApplication6\bin\Debug\ConsoleApplication6.exe"
v = os.system("%s %s %s" %  (cmd ,"dd" , "11dd"))
print "*************"
print type(v) 

结果还是比较满意的, 

 

 

ss
dd
11dd
*************
<type 'int'>

 同时写好的 给max 脚本调用 

cc = "F:\\c#\\c#2005\\ConsoleApplication6\\ConsoleApplication6\bin\\Debug\\ConsoleApplication6.exe"
c = DOSCommand  (cc+ " sdf"+ " 11")

"F:\c#\c#2005\ConsoleApplication6\ConsoleApplication6\bin\Debug\ConsoleApplication6.exe"
50

 

posted on 2015-04-29 13:46  盖天00  阅读(525)  评论(0编辑  收藏  举报

导航