J
Joe Caverly
Hi,
I'm using Visual C++ 32-bit Professional Edition 5.0
Using Microsoft Knowledge Base Article 181473 as a basis, I'm trying
to transform this VB Code;
Dim sc As Object
Dim code As String
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
On Error Resume Next
code = "Function Square(x)" & vbCrLf _
& "Square = x * x" & vbCrLf _
& "End Function"
sc.AddCode code
If Err Then
MsgBox "Syntax Error", vbCritical
End If
result = sc.Run("Square", 12.34)
MsgBox result
into plain C code, not C++. Here's what I've done so far;
#include <stdio.h>
#include <windows.h>
void main(void) {
IDispatch *pDisp; // Main IDispatch pointer.
unsigned short *ucPtr; // Temporary variable to hold names.
DISPID dispID; // Temporary variable to hold DISPIDs.
CLSID clsid; // Holds CLSID of server after CLSIDFromProgID.
HRESULT hr; // General error/result holder.
char buf[8192]; // Generic buffer for output.
// IDispatch::Invoke() parameters...
DISPPARAMS dispParams = { NULL, NULL, 0, 0 };
VARIANT parm1;
DISPID dispidNamed = DISPID_PROPERTYPUT;
// Initialize OLE Libraries.
OleInitialize(NULL);
{
// Get CLSID for ScriptControl Application from registry.
hr = CLSIDFromProgID(L"ScriptControl", &clsid);
if(FAILED(hr)) {
MessageBox(NULL, "ScriptControl not registered.", "Error",
MB_SETFOREGROUND);
return;
}
// Start Scriptcontrol and get its IDispatch pointer.
hr = CoCreateInstance(&clsid, NULL,
CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER,
&IID_IDispatch, (void **)&pDisp);
if(FAILED(hr)) {
MessageBox(NULL, "Couldn't start ScriptControl", "Error",
MB_SETFOREGROUND);
return;
}
// Get the 'Language' property's DISPID.
ucPtr = L"Language";
pDisp->lpVtbl->GetIDsOfNames(pDisp, &IID_NULL, &ucPtr, 1,
LOCALE_USER_DEFAULT, &dispID);
sprintf(buf, "DISPID for 'Language' property = 0x%08lx",
dispID);
MessageBox(NULL, buf, "Debug Notice", MB_SETFOREGROUND);
// Initiate parameters to set Language property to VBScript.
VariantInit(&parm1);
parm1.vt = VT_BSTR;
parm1.bstrVal = SysAllocString( OLESTR("VBScript"));
// One argument.
dispParams.cArgs = 1;
dispParams.rgvarg = &parm1;
// Handle special-case for property-puts!
dispParams.cNamedArgs = 1;
dispParams.rgdispidNamedArgs = &dispidNamed;
// Set 'Language' property to VBScript.
hr = pDisp->lpVtbl->Invoke(pDisp,
dispID, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
DISPATCH_PROPERTYPUT | DISPATCH_METHOD,
&dispParams, NULL, NULL, NULL
);
if(FAILED(hr)) {
sprintf(buf, "IDispatch::Invoke() failed with %08lx", hr);
MessageBox(NULL, buf, "Debug Notice", MB_SETFOREGROUND);
}
// All done.
MessageBox(NULL, "done.", "Notice", MB_SETFOREGROUND);
}
// Uninitialize OLE Libraries.
OleUninitialize();
}
An error is raised when I attempt to set the 'Language' property to
VBScript, that is, "IDispatch::Invoke() failed with 80020003"
TIA for your assistance.
Joe
I'm using Visual C++ 32-bit Professional Edition 5.0
Using Microsoft Knowledge Base Article 181473 as a basis, I'm trying
to transform this VB Code;
Dim sc As Object
Dim code As String
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
On Error Resume Next
code = "Function Square(x)" & vbCrLf _
& "Square = x * x" & vbCrLf _
& "End Function"
sc.AddCode code
If Err Then
MsgBox "Syntax Error", vbCritical
End If
result = sc.Run("Square", 12.34)
MsgBox result
into plain C code, not C++. Here's what I've done so far;
#include <stdio.h>
#include <windows.h>
void main(void) {
IDispatch *pDisp; // Main IDispatch pointer.
unsigned short *ucPtr; // Temporary variable to hold names.
DISPID dispID; // Temporary variable to hold DISPIDs.
CLSID clsid; // Holds CLSID of server after CLSIDFromProgID.
HRESULT hr; // General error/result holder.
char buf[8192]; // Generic buffer for output.
// IDispatch::Invoke() parameters...
DISPPARAMS dispParams = { NULL, NULL, 0, 0 };
VARIANT parm1;
DISPID dispidNamed = DISPID_PROPERTYPUT;
// Initialize OLE Libraries.
OleInitialize(NULL);
{
// Get CLSID for ScriptControl Application from registry.
hr = CLSIDFromProgID(L"ScriptControl", &clsid);
if(FAILED(hr)) {
MessageBox(NULL, "ScriptControl not registered.", "Error",
MB_SETFOREGROUND);
return;
}
// Start Scriptcontrol and get its IDispatch pointer.
hr = CoCreateInstance(&clsid, NULL,
CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER,
&IID_IDispatch, (void **)&pDisp);
if(FAILED(hr)) {
MessageBox(NULL, "Couldn't start ScriptControl", "Error",
MB_SETFOREGROUND);
return;
}
// Get the 'Language' property's DISPID.
ucPtr = L"Language";
pDisp->lpVtbl->GetIDsOfNames(pDisp, &IID_NULL, &ucPtr, 1,
LOCALE_USER_DEFAULT, &dispID);
sprintf(buf, "DISPID for 'Language' property = 0x%08lx",
dispID);
MessageBox(NULL, buf, "Debug Notice", MB_SETFOREGROUND);
// Initiate parameters to set Language property to VBScript.
VariantInit(&parm1);
parm1.vt = VT_BSTR;
parm1.bstrVal = SysAllocString( OLESTR("VBScript"));
// One argument.
dispParams.cArgs = 1;
dispParams.rgvarg = &parm1;
// Handle special-case for property-puts!
dispParams.cNamedArgs = 1;
dispParams.rgdispidNamedArgs = &dispidNamed;
// Set 'Language' property to VBScript.
hr = pDisp->lpVtbl->Invoke(pDisp,
dispID, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
DISPATCH_PROPERTYPUT | DISPATCH_METHOD,
&dispParams, NULL, NULL, NULL
);
if(FAILED(hr)) {
sprintf(buf, "IDispatch::Invoke() failed with %08lx", hr);
MessageBox(NULL, buf, "Debug Notice", MB_SETFOREGROUND);
}
// All done.
MessageBox(NULL, "done.", "Notice", MB_SETFOREGROUND);
}
// Uninitialize OLE Libraries.
OleUninitialize();
}
An error is raised when I attempt to set the 'Language' property to
VBScript, that is, "IDispatch::Invoke() failed with 80020003"
TIA for your assistance.
Joe