473,598 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("S criptControl")
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::Invo ke() parameters...
DISPPARAMS dispParams = { NULL, NULL, 0, 0 };
VARIANT parm1;
DISPID dispidNamed = DISPID_PROPERTY PUT;
// Initialize OLE Libraries.
OleInitialize(N ULL);
{
// Get CLSID for ScriptControl Application from registry.
hr = CLSIDFromProgID (L"ScriptContro l", &clsid);
if(FAILED(hr)) {
MessageBox(NULL , "ScriptCont rol not registered.", "Error",
MB_SETFOREGROUN D);
return;
}
// Start Scriptcontrol and get its IDispatch pointer.
hr = CoCreateInstanc e(&clsid, NULL,
CLSCTX_LOCAL_SE RVER|CLSCTX_INP ROC_SERVER,
&IID_IDispat ch, (void **)&pDisp);
if(FAILED(hr)) {
MessageBox(NULL , "Couldn't start ScriptControl", "Error",
MB_SETFOREGROUN D);
return;
}

// Get the 'Language' property's DISPID.
ucPtr = L"Language";
pDisp->lpVtbl->GetIDsOfNames( pDisp, &IID_NULL, &ucPtr, 1,
LOCALE_USER_DEF AULT, &dispID);

sprintf(buf, "DISPID for 'Language' property = 0x%08lx",
dispID);
MessageBox(NULL , buf, "Debug Notice", MB_SETFOREGROUN D);

// Initiate parameters to set Language property to VBScript.
VariantInit(&pa rm1);
parm1.vt = VT_BSTR;
parm1.bstrVal = SysAllocString( OLESTR("VBScrip t"));

// One argument.
dispParams.cArg s = 1;
dispParams.rgva rg = &parm1;

// Handle special-case for property-puts!
dispParams.cNam edArgs = 1;
dispParams.rgdi spidNamedArgs = &dispidNamed ;

// Set 'Language' property to VBScript.
hr = pDisp->lpVtbl->Invoke(pDisp ,
dispID, &IID_NULL, LOCALE_SYSTEM_D EFAULT,
DISPATCH_PROPER TYPUT | DISPATCH_METHOD ,
&dispParams, NULL, NULL, NULL
);
if(FAILED(hr)) {
sprintf(buf, "IDispatch::Inv oke() failed with %08lx", hr);
MessageBox(NULL , buf, "Debug Notice", MB_SETFOREGROUN D);
}

// All done.
MessageBox(NULL , "done.", "Notice", MB_SETFOREGROUN D);
}
// Uninitialize OLE Libraries.
OleUninitialize ();

}

An error is raised when I attempt to set the 'Language' property to
VBScript, that is, "IDispatch::Inv oke() failed with 80020003"

TIA for your assistance.

Joe
Nov 13 '05 #1
3 7021
Joe Caverly <jl*********@ya hoo.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("S criptControl")
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::Invo ke() parameters...
DISPPARAMS dispParams = { NULL, NULL, 0, 0 };
VARIANT parm1;
DISPID dispidNamed = DISPID_PROPERTY PUT;
All types execpt 'unsigned short' and 'char' are non-standard C
and you don't tell what they actually mean...
// Initialize OLE Libraries.
OleInitialize(N ULL);
{
// Get CLSID for ScriptControl Application from registry.
hr = CLSIDFromProgID (L"ScriptContro l", &clsid);
What is that 'L"ScriptContro l"' supposed to mean? Unless 'L' is
a macro for a literal string your compiler should complain loudly.
if(FAILED(hr)) {
MessageBox(NULL , "ScriptCont rol not registered.", "Error",
MB_SETFOREGROUN D);
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 = CoCreateInstanc e(&clsid, NULL,
CLSCTX_LOCAL_SE RVER|CLSCTX_INP ROC_SERVER,
&IID_IDispat ch, (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_SETFOREGROUN D);
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_DEF AULT, &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_SETFOREGROUN D); // Initiate parameters to set Language property to VBScript.
VariantInit(&pa rm1);
parm1.vt = VT_BSTR;
parm1.bstrVal = SysAllocString( OLESTR("VBScrip t"));
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.cArg s = 1;
dispParams.rgva rg = &parm1; // Handle special-case for property-puts!
dispParams.cNam edArgs = 1;
dispParams.rgdi spidNamedArgs = &dispidNamed ; // Set 'Language' property to VBScript.
hr = pDisp->lpVtbl->Invoke(pDisp ,
dispID, &IID_NULL, LOCALE_SYSTEM_D EFAULT,
DISPATCH_PROPER TYPUT | DISPATCH_METHOD ,
&dispParams, NULL, NULL, NULL
);
if(FAILED(hr)) {
sprintf(buf, "IDispatch::Inv oke() failed with %08lx", hr);
This assumes that the HRESULT type is an unsigned long, how do you
know?
MessageBox(NULL , buf, "Debug Notice", MB_SETFOREGROUN D);
} // All done.
MessageBox(NULL , "done.", "Notice", MB_SETFOREGROUN D);
}
// 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::Inv oke() failed with 80020003"


How did you manage to get the above "program" to compile? I can
hardly believe it...
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@p hysik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oe rring
Nov 13 '05 #2
Je***********@p hysik.fu-berlin.de wrote:
Joe Caverly <jl*********@ya hoo.ca> wrote:

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


What is that 'L"ScriptContro l"' supposed to mean?


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

<snip>

--
Richard Heathfield : bi****@eton.pow ernet.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******@addre ss.co.uk.invali d> wrote:
Je***********@p hysik.fu-berlin.de wrote:
Joe Caverly <jl*********@ya hoo.ca> wrote:

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


What is that 'L"ScriptContro l"' 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***********@p hysik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oe rring
Nov 13 '05 #4

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

Similar topics

2
33238
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 getting that error mentioned below. The unusual thing about it is that the login and password fields are filled but the line of code where it's supposed to click the login button returns an error code rather than logging on to the site. Please help....
3
5262
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. I included the testbed and active-x in the same project so I can refrence the active-x in design time and when I run the testbed I can set breakpoints in the active-x for debugging.
6
12753
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 are made using SOAP - i installed the SOAP3 toolkit on my windows 2k server to enable this. The webservice calls are returning String Arrays which I can't seem to do anything useful with. The call shown below returns a string array with two...
25
3709
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 machines. The code opens MS Word through Automation and then opens a particular Word doc. It's still working fine on most machines; but on one or two of them, the user is getting an Automation Error. The code used is as follows: Dim objWord As...
1
808
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 Excel.Application 'ADO Object for Excel Automation Public gstrExcelDir As String 'Source or Destination Directory
0
1434
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 vbscript uses something like document.form.txtSsn.Value to grab the SSN. When the button on the datagrid is clicked the SSN is placed in the hidden texbox for use with the vbscript. My problem is that the button is used to place the SSN in the...
4
2512
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
6777
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 like to be able to use Word objects using CreateObject() etc. Is that possible, and if so, where can I find more info on it? Thanks. David
5
8354
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 = $scripter->eval(UnBusyInvoice()); <<--my subroution print "Result: $k\n"; $scripter = null; If fails as follows:
0
7894
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3938
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2410
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.