07010311.txt 01-Jul-00


Subject: Persistent array in DLL?
From: "Will Chapman" <willchapman@dial.pipex.com>

I have a DLL which contains a function that my main app
  calls frequently. Thus
Main App
  do while DataComingFromDLL
    GetDatafromDLL()  && Call to DLL
    ProcessData()
  enddo
GetDataFromDLL refers to an array and I only want to build
  that once thus:
DLL
Global gaData as array
  GetDataFromDLL(uRef)
  if isNil(gaData)
    gaData := FillArray()
  endif
  ascan(gaData,uRef)
return DoOtherThings(uRef)
The problem with this approach is that gaData is subject to
  GC and so it isn't persistent.
I've played with RegisterKid but I can't figure out how to
  convert what has to be a dynamic array (I have no way of
  knowing its size at run-time)
ArrayStore() looks like a possibility but the help file
  example doesn't show how to get the array back from
  'storage'.
Any ideas...

Thanks...
Will


Subject: Re: Persistent array in DLL?
From: "Geoff Schaller" <geoffsch@bigpond.net.au>

Will,

I don't think I understand your problem. If you declare the
  global in the main app and use the function in the dll to
  create the array and then save it to your global, there is
  no problem! You can't directly refer to the global from
  the main in the dll (compiler error stuff) but you can
  build the array and pass it out. Alternately, you can use
  the System Object to pass around arrays - it transcends
  dll boundaries quite simply. I always get a batch of
  critics when I suggest this but so far, no one has come up
  with any evidence that it causes problems. I can post some
  code here if you would like to try this.

Geoff


Subject: Re: Persistent array in DLL?
From: "Will Chapman" <willchapman@dial.pipex.com>

>
array and pass it out. Alternately, you can use the System

  Object to pass around arrays - it transcends dll
  boundaries quite simply. I always get a
<
I think that approach will work; I'll give it a shot.
Thanks for the idea...

Will


Subject: Re: Persistent array in DLL?
From: Jean-Marie Berthiaume <#jiembe@#videotron.ca>

Can you post some code please. I'd like to know how you pass
  those arrays by the System Object.

Jean-Marie Berthiaume


Subject: Re: Persistent array in DLL?
From: "Geoff Schaller" <geoffsch@bigpond.net.au>

OK - Stephane also posted some elsewhere but here's how:
1)    Define a global object class:
CLASS MyGlobalObject INHERIT Vobject
  EXPORT aARRAY1[100] AS ARRAY
  EXPORT oShell AS MyShellWindow
  EXPORT oApp AS APP
  .... any other variable type you like....
METHOD Init(aParams...) CLASS MyGlobalObject
  ....assign anything you like (eg shell window, app,
  arrays)
  RegisterAxit(SELF)
METHOD DoStuff(Params...) CLASS MyGlobalObject
  ... anything you like (ie a "static global" function)
METHOD Destroy() CLASS MyGlobalObject
  ... clear up arrays and objects as per normal
2)    In your app Start() method, do something like:
  LOCAL oMainShell AS ShellWindow
  LOCAL DayOfWeek AS ARRAY
  oMainShell := ShellWindow{}
  DayOfWeek := {"......"}
  SysObject(MyGlobalObject{oMainShell, SELF, .....})

3)    The above is all that is necessary to set up the
  system object, you could have used protects and set up
  assigns/accesses, they can be as fully typed as you like
  and it can be totally GUI. there is no problem with any of
  this. Then, simply access them anywhere in your app or
  dlls without any other code:
to get the shell window:
oMain := SysObject():oShell
to call a method:
SysObject():MyMethod(.....)
to use an array
MyParam := SysObject():aArray[34]
SysObject():aArray := aNewArray

Regards,
Geoff


Subject: Re: Persistent array in DLL?
From: "Mike Bertenshaw" <mikebertenshaw@ais-lims.com>

Will,

Why don't you declare aData as a local static?

Mike


Subject: Re: Persistent array in DLL?
From: "Geoff Schaller" <geoffsch@bigpond.net.au>

hang-on, how does that work across a dll boundary?


Subject: Re: Persistent array in DLL?
From: "Will Chapman" <willchapman@dial.pipex.com>

Mike

I don't think that'll work as I'm in and out of the DLL so
  even a static local will, I think, be a candidate for the
  garbage collector each time I leave the DLL.
I've fiddled about with trying to put the array in reserved
  memory but I think the best approach is going to be
  Geoff's idea of tacking the array onto my main app class
  and passing the AppObject to the DLL so it can both fill
  the array and, later, refer to it. Neat if it works..
I'm going to try it as soon as the phone stops ringing<g>...
Have a good Easter...

Cheers
Will


Subject: Re: Persistent array in DLL?
From: "Mike Jones" <mike@richsoftware.com>

Will,

Do you mean you're loading and unloading the DLL
  dynamically?

Mike

Subject: Re: Persistent array in DLL?
From: "Will Chapman" <willchapman@dial.pipex.com>

I'm loading with Loadlibrary() and releasing when the entire
  transaction is over. Meanwhile one particular function
  within the DLL is called repeatedly and that meant an
  array was being repeatedly filled.
Geoff's idea of having the array as an instance of the Main
  app solved the problem - I pass the DLL a reference to the
  main app, the DLL fills the array and then uses the array
  across the DLL barrier as and when it needs to. A neat
  approach and an obvious solution when it was mentioned.
Thanks for everyone;s input...


Will


Subject: Re: Persistent array in DLL?
From: "Geoff Schaller" <geoffsch@bigpond.net.au>

Mike,

He would have to be - otherwise the static objects remain
  intact inside the dll for the life of the master thread.
  This is quite easy to test. So I agree with where you are
  going. Leave the dll's in memory (which should be
  everyone's goal anyway) and his "global" objects will
  remain available. A static is a static, whether its in the
  main exe or in a dll.

Geoff


Subject: Re: Persistent array in DLL?
From: "Mike Jones" <mike@richsoftware.com>

Geoff,

>
agree with where you are going. Leave the dll's in memory
  (which should be everyone's goal anyway) and his "global"
  objects will remain available. A
<
I'm surprised you didn't mention it before :-)

Mike
