I.T. Discussion Community!
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► MB LobbyPascal and Delphi Coding BoardDelphi Internet Solutions Topic   Print This     

How to pass/return an array between ASP and Delphi

How to pass/return an array between ASP and Delphi in Delphi Internet Solutions topic (part of our Pascal and Delphi Coding group).

Quick Search: pass/return   between   How pass/return   How pass/return array   between ASP  
emets
 (Inactive)
NZ
I have a problem passing and returning an array between an ASP page and a Delphi COM Object.

I create an array and pass it to the COM Object like this:

arr = obj.ArrTest(Array("element1","element2"))

The function is defined as:

function ArrTest(varname: OleVariant): OleVariant;

Inside the function if I try to treat varname as an array it doesn't work. For example testing for an array:

VarIsArray(varname);

returns False.

Similarly if I create a Variant Array inside the COM Object and return it to the ASP page it seems to go wront.

Can someone give me a simple example of how to do this.

Thanks
emets
 Posted 18 years ago (Thread Starter)
Comment Quote
About emets -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #3049, 6 replies
Thread Started 7/26/2001 5:16:00 PM
View Counter=10414
Last Reply Posted 7/30/2001 9:48:00 PM)
Location=NZ  
Joined=18 years ago   MB Posts=4  
William Pantoja
Woodinville, WA, USA
Here is sample code (Delphi 6, ASP):



unit Unit1;
 
{$WARN SYMBOL_PLATFORM OFF}
 
interface
 
uses
  ComObj, ActiveX, AspTlb, Project1_TLB, StdVcl, Classes;
 
type
  TTest = class(TASPMTSObject, ITest)
  private
    FItems : TStringList;
  protected
    function Get_Data: OleVariant; safecall;
    procedure Set_Data(Value: OleVariant); safecall;
  public
     procedure AfterConstruction; override;
     procedure BeforeDestruction; override;
  end;
 
implementation
 
uses Variants, SysUtils, ComServ;
 
procedure TTest.AfterConstruction;
 
begin
   FItems := TStringList.Create;
end;
 
procedure TTest.BeforeDestruction;
 
begin
   FItems.Free;
end;
 
function TTest.Get_Data: OleVariant;
 
var
   I    : Integer;
   Temp : Variant;
 
begin
   if FItems.Count > 0 then
   begin
      Temp := VarArrayCreate([1,FItems.Count],varVariant);
      for I := 0 to FItems.Count-1 do
      begin
         VarArrayPut(Temp,FItems[I],[I+1]);
      end;
      Result := Temp;
   end
   else begin
      Result := Null;
   end;
end;
 
procedure TTest.Set_Data(Value: OleVariant);
 
var
   I : Integer;
 
begin
   if VarIsArray(Value) then
   begin
      for I := VarArrayLowBound(Value,1) to VarArrayHighBound(Value,1) do
      begin
         FItems.Add(VarArrayGet(Value,[I]));
      end;
   end;
end;
 
initialization
  TAutoObjectFactory.Create(ComServer, TTest, Class_Test,
    ciMultiInstance, tmApartment);
end.



ASP Code:



<%@ Language=VBScript %>
<%
Option Explicit
%>
<html>
<body>
<%
 
Dim objTest
Dim aryInput
Dim aryOutput
Dim I
 
aryInput = Array("Test1","Test2","Test3")
 
Set objTest = Server.CreateObject("Project1.Test")
 
objTest.Data = aryInput
aryOutput = objTest.Data
 
If Not IsNull(aryOutput) Then
   For I = LBound(aryOutput) To UBound(aryOutput)
      Response.Write(aryOutput(I) & "<br>")
   Next
End If
 
Set objTest = Nothing
%>
</body>
</html>

---
William Pantoja

Consultant/Software Engineer
ForceOne Technologies, Inc.

 Posted 18 years ago
Comment Quote
About William Pantoja -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.

Post ID #3057 (Level 1.1)  Reply to 3049
Thread Started 7/26/2001 9:19:00 PM
View Counter=2
Location=Woodinville, WA, USA  
Joined=18 years ago   MB Posts=163   KB Posts=1  
William Pantoja
Woodinville, WA, USA
There are a few differences:

1. TASPObject should be changed to TASPMTSObject.

2. The reference to the Variant unit should be removed.

3. VarArrayGet and VarArrayPut don't exist in Delphi 5. Derefence the array like a normal array.

4. Remove the compilier directive.

Here is the source for Delphi 5:



unit Unit1;
 
interface
 
uses ComObj, ActiveX, AspTlb, Project1_TLB, StdVcl, Classes;
 
type
   TTest = class(TASPMTSObject, ITest)
   private
    FItems : TStringList;
   protected
    function Get_Data: OleVariant; safecall;
    procedure Set_Data(Value: OleVariant); safecall;
   public
      procedure AfterConstruction; override;
      procedure BeforeDestruction; override;
   end;
 
implementation
 
uses SysUtils, ComServ;
 
procedure TTest.AfterConstruction;
 
begin
   FItems := TStringList.Create;
end;
 
procedure TTest.BeforeDestruction;
 
begin
   FItems.Free;
end;
 
function TTest.Get_Data: OleVariant;
 
var
   I    : Integer;
   Temp : Variant;
 
begin
   if FItems.Count > 0 then
   begin
      Temp := VarArrayCreate([1,FItems.Count],varVariant);
      for I := 0 to FItems.Count-1 do
      begin
         Temp[I+1] := FItems[I];
      end;
      Result := Temp;
   end
   else begin
      Result := Null;
   end;
end;
 
procedure TTest.Set_Data(Value: OleVariant);
 
var
   I : Integer;
 
begin
   if VarIsArray(Value) then
   begin
      for I := VarArrayLowBound(Value,1) to VarArrayHighBound(Value,1) do
      begin
         FItems.Add(Value[I]);
      end;
   end;
end;
 
initialization
   TAutoObjectFactory.Create(ComServer, TTest, Class_Test,
    ciMultiInstance, tmApartment);
end.

---
William Pantoja

Consultant/Software Engineer
ForceOne Technologies, Inc.

 Posted 18 years ago
Comment Quote
About William Pantoja -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.

Post ID #3065 (Level 1.2)  Reply to 3049
Reply Posted 7/27/2001 5:26:00 AM
Location=Woodinville, WA, USA  
Joined=18 years ago   MB Posts=163   KB Posts=1  
William Pantoja
Woodinville, WA, USA
Although there is really no problem, the AfterConstruction and BeforeDestruction methods are specifically designed to be used for initialization and finalization code.

---
William Pantoja

Consultant/Software Engineer
ForceOne Technologies, Inc.

 Posted 18 years ago
Comment Quote
About William Pantoja -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.

Post ID #3096 (Level 1.3)  Reply to 3049
Reply Posted 7/30/2001 9:09:00 PM
Location=Woodinville, WA, USA  
Joined=18 years ago   MB Posts=163   KB Posts=1  
emets
 (Inactive)
NZ
Thank you William, I'll try it in Delphi 5 and see what happens. From what I can see about the only differences are that my class is derived from TASPObject and in Delphi 6 there seems to be new unit Variants. Anyway, I can't wait to have a go at it.

Thank again
emets
 Posted 18 years ago (Thread Starter)
Comment Quote
About emets -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #3059 (Level 1.4)  Reply to 3049
Reply Posted 7/26/2001 11:50:00 PM
Location=NZ  
Joined=18 years ago   MB Posts=4  
emets
 (Inactive)
NZ
Great! It is all GO now.

Is it ok if I used:
  
type
TTest = class(TASPMTSObject, ITest)
private
FItems: TStringList;
...
public
procedure Initialize; override;
destructor Destroy; override;
end;

implementation

uses ComServ, SysUtils;

procedure TTest.Initialize;
begin
inherited;
FItems := TStringList.Create;
end;

destructor TTest.Destroy;
begin
FItems.Free;
inherited;
end;


instead of AfterConstruction, Before Destruction. It seems to work fine but I am not sure if there is some specific reason for using one instead of the other.

Thank You
emets
 Posted 18 years ago (Thread Starter)
Comment Quote
About emets -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #3074 (Level 1.5)  Reply to 3049
Reply Posted 7/27/2001 5:23:00 PM
Location=NZ  
Joined=18 years ago   MB Posts=4  
Most Recent Post
emets
 (Inactive)
NZ
William,

Thank you for all your effort and the valuable information you provided me with.

emets
 Posted 18 years ago (Thread Starter)
Comment Quote
About emets -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #3099 (Level 1.6)  Reply to 3049
Reply Posted 7/30/2001 9:48:00 PM
Location=NZ  
Joined=18 years ago   MB Posts=4  

Revive Thread!

Add a comment to revive this old thread and make this archived thread more useful.

Write a Comment...
Full Editor
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1103A1
Enter key:
Icon: A Post    Thread    Idea    Important!    Cool    Sad    No    Yes    Includes a Link...   
Thread #3049 Counter
10414
Since 4/2/2008

Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech