ASP 內建元件(ADODB.Connection、Scripting.FileSystemObject 等)功能有限,VBScript 缺乏複雜資料結構(鏈表、B+ 樹等)支援。CGI/ISAPI 開發週期長、調試難、穩定性差(ISAPI 掛掉拖垮 IIS)。ASP 元件結合 C++ 高效能與 ASP 易部署優勢,是最佳折中方案。
MyStudio(即元件 ProgID 前綴);DllRegisterServer 等),無需手動維護。CMyComponent;IMyComponent;在 ClassView 右鍵 IMyComponent → Add Method / Property。
| 類型 | IDL 寫法 | C++ 對應 | 備註 |
|---|---|---|---|
| 輸入 | [in] int n | int n | 必加 [in] |
| 輸出/返回 | [out, retval] int* pn | int* pn | 唯一返回值,函數體 *pn = val |
| 字串 | [in] BSTR str / [out, retval] BSTR* pStr | BSTR | 轉換:CString s = bstr1; *pStr = s.AllocSysString(); |
| ASP 物件 | [in] IUnknown* pUnk | IUnknown* | QueryInterface 取 IRequest 等 |
| 不定參數 | [in] SAFEARRAY(VARIANT) arr | SAFEARRAY* | 用 COleSafeArray 操作 |
HRESULT CMyComponent::StrConv(BSTR bstr1, BSTR* bstr2) {
CString str = bstr1; // BSTR → CString
str = str.Left(str.GetLength() - 1);
*bstr2 = str.AllocSysString(); // CString → BSTR
return S_OK;
}
// .h 中聲明成員
float m_myFloat;
// put_MyFloat
STDMETHODIMP CMyComponent::put_MyFloat(float newVal) {
m_myFloat = newVal; return S_OK;
}
// get_MyFloat
STDMETHODIMP CMyComponent::get_MyFloat(float* pVal) {
*pVal = m_myFloat; return S_OK;
}
<%@ Language=VBScript %>
<%
Set Obj = Server.CreateObject("MyStudio.MyComponent")
j = Obj.InitMyComp(5)
Response.Write j <br> ' 輸出 5
Obj.MyFloat = 3.9
j = Obj.MyFloat
Response.Write j <br> ' 輸出 3.9
str1 = "abcd"
str2 = Obj.StrConv(str1)
Response.Write str2 ' 輸出 abc
%>