Solved

Pass by reference in VBScript

Posted on 1998-08-19
14
1,198 Views
Last Modified: 2013-12-25
I have a com dll which has a method with OUT parameter of type variant array. I have created an instance of this dll in an ASP using server.createobject. I want to pass a variant array to this method using pass by reference. How to do this.
0
Comment
Question by:rvsamuel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 

Author Comment

by:rvsamuel
ID: 1854453
Edited text of question
0
 
LVL 2

Expert Comment

by:Jagar
ID: 1854454
This should be done in the DLL the arguement should be declared a ByRef.  Then you just pass it normally, ex

In DLL
Public Sub MyProcedure(ByRef x as Variant)

In VBScript
MyProcedure SomeArray
0
 

Author Comment

by:rvsamuel
ID: 1854455
The dll is devloped using c++ and will accept only a variant pointer
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 2

Expert Comment

by:milenvk
ID: 1854456
I have the answer but 25 points are less than the price of it
0
 

Author Comment

by:rvsamuel
ID: 1854457
if u're going to give me the code along with it,i'll increase the value to 75 points
0
 
LVL 2

Expert Comment

by:milenvk
ID: 1854458
Yes, of course I'll give you the code...
0
 
LVL 2

Expert Comment

by:milenvk
ID: 1854459
Oops... by mistake I put this as answer... sorry... increase the points and you'll have the answer...
0
 

Author Comment

by:rvsamuel
ID: 1854460
Adjusted points to 75
0
 
LVL 2

Accepted Solution

by:
milenvk earned 70 total points
ID: 1854461
Here's an example of what you have to have in the IDL file:

[id(1), helpstring("method Foo")] HRESULT Foo([out, retval] SAFEARRAY(VARIANT) *ppArr);

And here's the C++ prototype of the function:
HRESULT Foo(SAFEARRAY **ppArr);


Then you have to construct the variant array and fill it up with data. Here's an example of construction and fill up of a single dimension array with 10 elements:

VARIANT *varArray = NULL;
SAFEARRAYBOUND bounds = {10, 0};
*ppArr = SafeArrayCreate(VT_VARIANT, 1, &bounds);
SafeArrayAccessData(*ppArr, (void**)&varArray);
for(int i = 0; i < 10; i++)
{
  V_VT(varArray + 1) = VT_I2;
  V_I2(varArray + 1) = i;
}

SafeArrayUnaccessData(*ppArr);

Well, basically that's it :-)
VB will happilly accept that structure and will work with it.
0
 

Author Comment

by:rvsamuel
ID: 1854462
how will i use it in my vbscript function?
0
 
LVL 2

Expert Comment

by:milenvk
ID: 1854463
Here's an example how:

Dim arr, obj

Set obj = Server.CreateObject("MyObjServer.MyObject")

arr = obj.Foo 'here arr becomes a VBScript array (remember the [retval] attribute?)

for each n in arr
  MsgBox n
next n


This will create an object of your type, assuming the object's ProgID is "MyObjServer.MyObject" and then will display the contents of the array returned by Foo in subsequent message boxes. I.e. it will open 10 message boxes displaying the vaules from 0 to 9.
0
 
LVL 2

Expert Comment

by:milenvk
ID: 1854464
By the way if you want to modify an array passed to you by reference you have to add another [in] attrbite, thus in the above example the attributes of the ppArr parameter will be [in, out, retval]. Of course then you'll have a constructed array and you have to work with it, instead of creating one of your own. You can access its data as shown, resize it, etc. The SAFEARRAY subject is fully covered in the Win32 API documentation.
0
 

Author Comment

by:rvsamuel
ID: 1854465
i tried ur code,the problem i face is,the array's second element returns the ubound of the array and the remaining elements return no value,so i cannot solve this problem.

0
 
LVL 2

Expert Comment

by:milenvk
ID: 1854466
There should be some mistake you make. I use this code in my projects with VBScript and JScript and everything runs fine. What do you mean by "the array's second element returns the ubound"? Does that mean that you receive a 2 elements array but you expect more?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question