|
Programmatic Execution |
|
See Also: |
APG scripts can be executed from C++ code. This topic shows how.
To run APG scripts in C++, the APGen.h file must be included. It can normally
be found at <install dir>/include/APGen.h. For insight into the
Automation interfaces that are used in APGen, see
<install dir>/include/APGen.idl.
Below is a C++ example that executes an APG script. This code can be cut and pasted into your own applications.
/*
** apgen_cpp.cpp
**
** WebGecko Software
**
** A simple C++ console app that executes an APG script.
**
** The APG script's path is passed in the cmd line.
*/
#include <stdio.h> // printf
#include <atlbase.h> // ATL base include
// Include APGen COM definitions
#include
"c:/Program Files/APGen/include/APGen.h"
/*--------------------------------------------------------------+
| GetErrorInfoDescription |
| |
| Retrieves the description of the last COM error in this |
| thread, using the IErrorInfo object. |
+--------------------------------------------------------------*/
HRESULT GetErrorInfoDescription(LPWSTR wszErr, DWORD cchErr)
{
HRESULT hr;
// Retrieve the IErrorInfo object for the current thread
CComPtr<IErrorInfo> xpei;
hr =
::GetErrorInfo(0, &xpei);
if (FAILED(hr) ||
!xpei)
{ // This does happen. When no ErrorInfo is set, hr = S_FALSE and xpei = 0.
::lstrcpynW(wszErr, L"Error description not available.", cchErr);
return S_FALSE;
}
// Copy the error description
CComBSTR bstrDesc;
xpei->GetDescription(&bstrDesc);
::lstrcpynW(wszErr, bstrDesc, cchErr);
return S_OK;
}
/*--------------------------------------------------------------+
| main |
| |
| Runs an APG script. APGen.Run() could have been used in |
| place of APGen.OpenScript() and APGScript.Run(). However,|
| using APGen.OpenScript() provides a better foundation for |
| user modification. |
+--------------------------------------------------------------*/
int main(int argc, char* argv[])
{
if ((argc < 2) || (argc > 2))
{
printf("Usage: apgen_cpp <APG script path>\r\n");
return -1;
}
// Copy the script path to a BSTR
CComBSTR bstrPath(argv[1]);
// Declare variables
HRESULT hr = S_OK;
CComPtr<IAPGen> xpAPGen;
CComPtr<IAPGScript> xpScript;
// Error buffer
#define SIZE_ERRBUF 512
WCHAR wszErr[SIZE_ERRBUF]; // Unicode buffer
wszErr[0] =
0;
// Initialize COM
::CoInitialize(0);
// Create the Active Page Generator object
hr =
::CoCreateInstance(__uuidof(APGen), 0, CLSCTX_INPROC_SERVER,
__uuidof(IAPGen), (void**)&xpAPGen);
if (FAILED(hr))
{
::lstrcpyW(wszErr, L"Unable to create APGen object. Try reinstalling Active Page Generator.");
goto lError;
}
// Open the APG script
hr =
xpAPGen->OpenScript(bstrPath, &xpScript);
if (FAILED(hr))
{
::GetErrorInfoDescription(wszErr, SIZE_ERRBUF);
goto lError;
}
// Set Debug = True
xpScript->put_Debug(VARIANT_TRUE);
// Run the script
hr =
xpScript->Run();
if (FAILED(hr))
{
::GetErrorInfoDescription(wszErr, SIZE_ERRBUF);
goto lError;
}
lCleanup:
// Release COM objects
xpScript.Release();
xpAPGen.Release();
// Uninitialize COM
::CoUninitialize();
return hr; // 0 if no errors
lError:
::printf("Error running APG script '%S':\r\n\t%S", bstrPath, wszErr);
goto lCleanup;
}