背景: 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 !
關鍵是 RegAsm.exe 這個.net 工具程式, 相關介紹:
http://msdn.microsoft.com/zh-cn/library/tzat5yw6(VS.80).aspx
這麼註冊C# COM DLL的 :
(ps1:我的dll叫phpimage.dll )
(ps2:請在phpimage.dll的資料夾下,調用這個命令列)
regasm phpimage.dll /tlb:phpimage.tlb /codebase
ok, 你不放心可以查找register, 你會發現裡面有一個 test.PhpI ...... |