PHP 調用 C# dll

來源:互聯網
上載者:User

弄了半天, PHP 終於能調用我的C# dll 了.

 

該死的,我對C# COM註冊一向不瞭解, PHP 文檔上只給PHP那部分內容,沒告訴我怎麼弄dll

我還傻兮兮的用 Regsvr32 註冊那個c# dll.

 

背景:

Windows xp sp3 ; apache 2.2.14 ; php 5.2.12 ;

VS2010 beta ;

 

語言:

PHP5 , C#

 

C#部分:

建立一個 C# Class Library . (dll)

建立完成後, 開啟項目的屬性,

在點選左邊的 "Application"(就是第一個tab) , 然後點擊 Assembly Information 按鈕 , 

在彈出的Dialog中, 必須在底部勾上: Make assembly COM-visible !

否則 , 這個dll將不能以COM方式訪問 .

( 當然, 你也可以在代碼中的類聲明中寫上[ComVisible(true)] , 效果一樣)

 

代碼如下:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices;  

 

namespace test

{   

    //[ComVisible(true)] //or check "Assembly COM-Visible" at Application-Assembly_Information dialog ;

    public class PhpImage

    {

        public string test()

        {

            return "Hello world!";

        }

    }

}

然後編譯出了dll , 這裡叫 phpimage.dll我以為直接在php中寫如下語句既可:<?php   $myPhpImg = new COM("test.PhpImage");   echo $myPhpImg ->test() ;?>

 

 

後來結果當然是錯誤的,無法建立com對象.

其實用屁股想也知道, 系統哪裡知道test.PhpImage是對應哪一個dll !

 

搞了半天, 後來發現一個文章,依樣畫葫蘆:

 

http://hi.baidu.com/softways/blog/item/6c0755b515731dcb36d3cabf.html

 

他是要:發布C# COM DLL

我也差不多,我只是想知道怎麼註冊C# 的 COM DLL

我就再重複一遍吧:

 

關鍵是 RegAsm.exe 這個.net 工具程式, 相關介紹:

http://msdn.microsoft.com/zh-cn/library/tzat5yw6(VS.80).aspx

 

這麼註冊C# COM DLL的 :

(ps:我的dll叫phpimage.dll )

(ps2:請在phpimage.dll的檔案夾下,調用這個命令列)

 

regasm phpimage.dll /tlb:phpimage.tlb /codebase

 

ok, 你不放心可以尋找register, 你會發現裡面有一個 test.PhpImage 的項 , 包含有clsid , 你再尋找這個clsid, 就能發現這個clsid對應的資訊,包括dll的路徑.

 

最後我的頁面上就顯示了 "Hello world!"

 

PS: 誰知到怎麼用DOTNET來實現啊,嘗試了都不行,暈.

 

前人栽樹,後人乘涼.

如果這篇拙文對您有協助,請謝謝我(汗),這樣我也很高興啊!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.