Tutorials:
ASP COM Links:
|
|
Steps for building our component: 3
| Step 11: |
First we will code the method for POST forms.
Now add the following code to the FormPOST method. Look for explanations and short
comments in the source code comments below.
|
STDMETHODIMP CProcessForm::FormPOST()
{
HRESULT hr = S_OK;
CComVariant vtTemp;
IUnknown* pUnknown = NULL;
IEnumVARIANT* pEnum = NULL;
ULONG lValue = 0;
CComPtr piRequestVariables;
hr = m_piRequest->get_Form(&piRequestVariables);
if(FAILED(hr))
{
return hr;
}
if((m_bOnStartPageCalled == TRUE) && (m_piRequest != NULL))
{
hr = piRequestVariables->get__NewEnum(&pUnknown);
if(!FAILED(hr))
{
hr = pUnknown->QueryInterface(IID_IEnumVARIANT, (void**)&pEnum);
}
if(FAILED(hr))
{
return hr;
}
while(S_OK == (pEnum->Next(1,&vtTemp,&lValue)))
{
CComVariant vtValue;
CComBSTR bstrTemp;
//write out the field name and equal to sign
m_piResponse->Write(vtTemp);
vtValue = "=";
m_piResponse->Write(vtValue);
//get the field value and write it out
piRequestVariables->get_Item(vtTemp,&vtValue);
vtValue.ChangeType(VT_BSTR);
m_piServer->HTMLEncode(vtValue.bstrVal,&bstrTemp);
m_piResponse->Write(CComVariant(bstrTemp));
vtTemp = "<P>\n";
m_piResponse->Write(vtTemp);
}
}
return hr;
}
|
| Step 12: |
Similarly we will code the method for GET forms. Now
add the following code to the FormGET method.
Look for explanations and short comments in the source code comments below.
|
STDMETHODIMP CProcessForm::FormGET()
{
HRESULT hr = S_OK;
CComVariant vtTemp;
IUnknown* pUnknown = NULL;
IEnumVARIANT* pEnum = NULL;
ULONG lValue = 0;
CComPtr piRequestVariables;
hr = m_piRequest->get_QueryString(&piRequestVariables);
if(FAILED(hr))
{
return hr;
}
if((m_bOnStartPageCalled == TRUE) && (m_piRequest != NULL))
{
//get enumerator object
hr = piRequestVariables->get__NewEnum(&pUnknown);
if(!FAILED(hr))
{
hr = pUnknown->QueryInterface(IID_IEnumVARIANT, (void**)&pEnum);
}
if(FAILED(hr))
{
return hr;
}
while(S_OK == (pEnum->Next(1,&vtTemp,&lValue)))
{
CComVariant vtValue;
CComBSTR bstrTemp;
m_piResponse->Write(vtTemp);
vtValue = " = ";
m_piResponse->Write(vtValue);
piRequestVariables->get_Item(vtTemp,&vtValue);
vtValue.ChangeType(VT_BSTR);
m_piServer->HTMLEncode(vtValue.bstrVal,&bstrTemp);
//Again cast it to CComVariant as Write only accepts variants
m_piResponse->Write(CComVariant(bstrTemp));
vtTemp = "<P>\n";
m_piResponse->Write(vtTemp);
}
}
return hr;
}
|
|
Step 13: |
Now our COM component is ready to be compiled and used via ASP pages. Compile the
component. You will now get a file called MyForm.dll in the debug directory of your project.
Visual C++ will register it for you. If you are running your web server on a
different machine then you will have to copy this dll to that machine and manually
register it by using the regsvr32.exe command line utility. The next steps
will show you two simple ASP files to test this component.
|
Note: We have not added any error checking code to this example.
It is left upto you as an exercise.
Main |
1 |
2 |
3 |
4 |
5
|