473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

VBScript OLE Automation from a C Application

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
Nov 13 '05 #1
3 7002
Joe Caverly <jl*********@yahoo.ca> wrote:
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 <stdlib.h>) here.
}
// 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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2
Je***********@physik.fu-berlin.de wrote:
Joe Caverly <jl*********@yahoo.ca> wrote:

// Get CLSID for ScriptControl Application from registry.
hr = CLSIDFromProgID(L"ScriptControl", &clsid);


What is that 'L"ScriptControl"' supposed to mean?


It's a wide string literal. Perfectly standard C.

<snip>

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #3
Richard Heathfield <do******@address.co.uk.invalid> wrote:
Je***********@physik.fu-berlin.de wrote:
Joe Caverly <jl*********@yahoo.ca> wrote:

// Get CLSID for ScriptControl Application from registry.
hr = CLSIDFromProgID(L"ScriptControl", &clsid);


What is that 'L"ScriptControl"' supposed to mean?

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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
I'm trying to create a vbscript so that I can autologin and get my tasks done unattended. As part of my code, I'm trying to click to get the vbscript to click the "Login" button but I'm always...
3
by: Tash Robinson | last post by:
Hi I am kind of new to active-x programming, and need a point in the right direction. I have an active-x control that I wrote in VB6. I wrote a testbed in VB and everything seems to work OK....
6
by: ASPfool | last post by:
Hello everyone, Please help me before I throw my computer out of the window: I'm working on a web application in classic ASP (vbscript), which is making calls to some webservices. The calls...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
1
by: Jimmer | last post by:
I've got what should be an easy automation problem, but the solution simply isn't coming to me. I've got several public variables set up for automation as follows: Public gappExcel As...
0
by: keys4worship | last post by:
I have a datagrid that contains among other things an SSN and a button. I have added an onclick attribute to the button that works with vbscript to query information on 3270 Pcomm emulator. The...
4
by: client site dll in vbscript | last post by:
Hi i have one dll on client site,i want to use it in aspx page, on clinet site Is it possible or not .Please tell me the solution Please email me at indipren@hotmail.com Regarda Indi
1
by: David C | last post by:
Can I use VBScript in a aspx page similar to using Javascript for running client code? I assume it would be used in a <scriptblock. This is for a controlled intranet internal application and I'd...
5
ravioliman
by: ravioliman | last post by:
How do I execute a vbs subroutine from php. I found this on the web and is in my php code: $scripter = new COM("MSScriptControl.ScriptControl"); $scripter->Language = "vbscript"; $k =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.