Solved

Pass by reference in VBScript

Posted on 1998-08-19
14
1,195 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

739 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