Solved

Pass by reference in VBScript

Posted on 1998-08-19
14
1,185 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
Comment Utility
Edited text of question
0
 
LVL 2

Expert Comment

by:Jagar
Comment Utility
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
Comment Utility
The dll is devloped using c++ and will accept only a variant pointer
0
 
LVL 2

Expert Comment

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

Author Comment

by:rvsamuel
Comment Utility
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
Comment Utility
Yes, of course I'll give you the code...
0
 
LVL 2

Expert Comment

by:milenvk
Comment Utility
Oops... by mistake I put this as answer... sorry... increase the points and you'll have the answer...
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:rvsamuel
Comment Utility
Adjusted points to 75
0
 
LVL 2

Accepted Solution

by:
milenvk earned 70 total points
Comment Utility
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
Comment Utility
how will i use it in my vbscript function?
0
 
LVL 2

Expert Comment

by:milenvk
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

744 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

17 Experts available now in Live!

Get 1:1 Help Now