Solved

Pass by reference in VBScript

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

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

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DNS, website, godaddy 6 79
Error viewing ASP page 12 151
Problem to go to Web page 2 100
Element alignment and word wrapping 9 62
Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
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…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now