Solved

Pass by reference in VBScript

Posted on 1998-08-19
14
1,192 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

803 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