VBScript OLE Automation from a C Application

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
 
J

Jens.Toerring

Joe Caverly said:
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;

Your program contains that many system specific function calls,
typedefs and probably macros that it's basically impossible to
figure out what might be the problem. Even worse, the question
is compelete off-topic here, because it is about something
absolutely system-specific. All you can expect from clc is that
people point out the C errors in your program because the topic
here is standard C, not some vendor-specific extensions. You
probably would get more helpful answers for your problem
from a newsgroup with microsoft somewhere in its name.
#include <stdio.h>
#include <windows.h>

Non-standard header...
void main(void) {

int 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;

All types execpt 'unsigned short' and 'char' are non-standard C
and you don't tell what they actually mean...
// Initialize OLE Libraries.
OleInitialize(NULL);
{
// Get CLSID for ScriptControl Application from registry.
hr = CLSIDFromProgID(L"ScriptControl", &clsid);

What is that 'L"ScriptControl"' supposed to mean? Unless 'L' is
a macro for a literal string your compiler should complain loudly.
if(FAILED(hr)) {
MessageBox(NULL, "ScriptControl not registered.", "Error",
MB_SETFOREGROUND);
return;

Since main() must be an integer returning function you need something
like "return 0;" or "return EXIT_FAILURE;" (this would require
inclusion of said:
}
// Start Scriptcontrol and get its IDispatch pointer.
hr = CoCreateInstance(&clsid, NULL,
CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER,
&IID_IDispatch, (void **)&pDisp);

Where did you define 'IID_IDispatch'? It definitely shouldn't be
defined in a header file (the only remotely likely candidate being
the non-standard <windows.h> header and I don't think that people
at Microsoft would be that stupid to define variables in header
files) and you never defined it, but you try to take its address...
if(FAILED(hr)) {
MessageBox(NULL, "Couldn't start ScriptControl", "Error",
MB_SETFOREGROUND);
return;
}
// Get the 'Language' property's DISPID.
ucPtr = L"Language";

Again this strange 'L"Language"' stuff. And even if 'L' should be a
macro for a literal string you would need 'ucPtr' to be of type
'const char *' instead of 'unsigned short *'.
pDisp->lpVtbl->GetIDsOfNames(pDisp, &IID_NULL, &ucPtr, 1,
LOCALE_USER_DEFAULT, &dispID);

'IID_NULL' has never been defined.
sprintf(buf, "DISPID for 'Language' property = 0x%08lx",
dispID);

This assumes that the DISPID type is an unsigned long, which may be
correct but is impossible to tell.
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"));

If this is a memory allocation in disguise, shouldn't you deallocate
the memory somewhere when you don't need it anymore?
// 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);

This assumes that the HRESULT type is an unsigned long, how do you
know?
MessageBox(NULL, buf, "Debug Notice", MB_SETFOREGROUND);
}
// All done.
MessageBox(NULL, "done.", "Notice", MB_SETFOREGROUND);
}
// Uninitialize OLE Libraries.
OleUninitialize();

Missing return value from main().
An error is raised when I attempt to set the 'Language' property to
VBScript, that is, "IDispatch::Invoke() failed with 80020003"

How did you manage to get the above "program" to compile? I can
hardly believe it...
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
J

Jens.Toerring

Richard Heathfield said:
(e-mail address removed)-berlin.de wrote:
It's a wide string literal. Perfectly standard C.

Thanks for pointing this out - I never had to deal with wchar_t's,
so I didn't know about this (but for sake of consistency I would
have expected to have the modifier *after* the string like in 3L).

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top