atelier:mitsuba

i love UI/UX, Blend, XAML, Behavior, P5, oF, Web, Tangible Bits and Physical computing. なにかあればお気軽にご連絡ください。atelier@c-mitsuba.com

Silverlight-OOBとGainerを繋ぎたい@その4

前はGainerを叩くのに成功したので、今度はCOMのお勉強です。

前にやってみてーときよくらさんにお願いした記事を参考にしながら
http://d.hatena.ne.jp/kiyokura/20100116/p1
@Tworksさんにへるぷみーを投げながら、なんとかできました。

とりあえずはCOMを作ってみる。
きよくらさんの記事を参考にしながら、プロジェクトを作ってプロパティを設定しました。
で、かいたコードがこんな感じ。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Class1
{
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [Guid("D212663B-9664-4090-AC14-25291647B6C0")]
    [ComVisible(true)]
    public interface IMyCom
    {
        String Execute();
    }

    [Guid("F873B5EE-BBF3-42e0-922A-53F7A5CC74AD")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    [ProgId("Class1.hogehoge")]
    public class Class1 : IMyCom    
    {
        public Class1()
        {
        }

        #region IMyCom メンバ

        public String Execute()
        {
            int i = 0;
            System.Diagnostics.Process.Start("notepad.exe");
            return "hogeee";
        }

        #endregion
    }
}

とりあえず、テキストとして、hogeeeを返すのと、メモ帳を立ち上げるCOMです。

で、SLの方に、ボタンおいて、こんな感じに書きました。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.InteropServices.Automation;


namespace SilverlightApplication9
{

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            dynamic obj = System.Runtime.InteropServices.Automation.AutomationFactory.CreateObject("Class1.hogehoge");
            String hoge = obj.Execute();
            button1.Content = hoge;
        }
    }
}

COMをオブジェクトに登録して
String型hoge にCOMの返り値を入力。
帰ってきたhogeの内容をボタンのラベルにします。
するとボタンを押すとメモ帳が立ち上がって、ボタンのラベルがhogeeeeeに変わるって仕組みですね。

あとは、このCOMを前にWindowsFormでやったようにGainerをつなげて、値取ってくればSLとGainerがつながるのかも!
Gainerの基板にあわせて、Perspective3Dを動かせるようにしたいなぁ。
メモ帳を立ち上げられるんならgsp.exeも一緒に立ち上げられそうだし。

でも残念ながら学校にGainerをおいてきちゃったので、続きはまた火曜日w

さて、がんばるぞー

もしできたらインストーラー作って公開しますー♪