Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Dec 11, 2017 2:51 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Declaring a function and using Variants
PostPosted: Fri May 11, 2012 7:51 am 
Offline

Joined: Fri May 11, 2012 7:43 am
Posts: 14
Good afternoon all,

I am trying to migrate from VB6 to RB. The contex seems very similar and looks like the idea opertunity as VB6 is no longer supported. But, I write software based on a suppliers .dll.

The suppliers .dll uses the declaration

Declare function StartJob Lib "c:\calcdll.dll" (ByRef p1 As Double, ByRef p2 As Variant, ByRef p3 as Double) As Boolean

And since I have tried to use this in RB I am having issues. From research I can't pass a vairiant to a .dll or any other external function.

This is a major issue as it is one of the key components of my programming. The output is from the .dll is an array of mostly doubles with a couple of strings so its a mixxed bag to be honest.

Has anyone else had this issue and if so how did they over come it?

Thank you in advance.


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Fri May 11, 2012 12:29 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Quote:
The output is from the .dll is an array of mostly doubles with a couple of strings so its a mixxed bag to be honest.

Declare the parameters as Ptr. Construct a MemoryBlock of the appropriate size and pass it to the function.


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Fri May 11, 2012 4:01 pm 
Offline

Joined: Fri May 11, 2012 7:43 am
Posts: 14
Close, but no cigar,

It is now accepting the declaration but its now saying I "cant pass an expression as a parameter that is defined as Byref"

The function declaration is

Declare function StartJob Lib "c:\TCI Sbank\calcdll.dll" (ByRef p1 As Double, ByRef p2 As Ptr, ByRef p3 as Double) As Boolean

While the code which uses the function is

a = StartJob(aInputData(0), aResult(0), aOptions(0))

I put the required info into the aInputData array and then run the function, the boolean aspect is nonsence, it is the aResults array which I use after this. aOptions is also a bit of nonsence. But as you will be aware, is if you pass an array to a function it needs to be 'Byref' (i understand the logic of value/reference memory allocation etc... I love OOP)

This works perfectly in VB6, I really want to understand how to use RB as i am keen to move across wholesale.


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Fri May 11, 2012 5:52 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
You cannot pass arrays to a declare. Use a MemoryBlock instead. Try it first without ByRef, the ptr already implies one level of dereference.


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Sun May 13, 2012 2:57 am 
Offline

Joined: Fri May 11, 2012 7:43 am
Posts: 14
It seems to be working but the .dll fails to load as a result of failing to load one of its own .dll's. I need to cast all the items to Ptr. Then from Ptr to double or string but other than that it seems to load okay.

Will update when i try the code at work where the .dll is installed formally.


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Mon May 14, 2012 7:28 am 
Offline

Joined: Fri May 11, 2012 7:43 am
Posts: 14
Still failing to call the function.

It crashes every time.

How do you create a functioning memoryblock of size,

Below is my code for the entire procedure.

Also I presume you cant have decimal memoryblock components.

Please bear in mind i am new to RB,

The .dll calls other .dll's of its own itself too which i think maybe cuasing a problem. They may be expecting an array of doubles.

I have tried to declare as soft and without-soft. Also what does soft mean?

Thank you,


Const NINPUTDATA = 100
Const NRESDATA = 100
Const NOPTIONSDATA = 1
Const P60 = 1

soft Declare function StartJob Lib "calcdll.dll" (p1 As Ptr, p2 as Ptr , p3 as Ptr) As Boolean

Dim bErr As Boolean
Dim aInputData(NINPUTDATA) As Ptr
Dim aResult(NRESDATA) As Ptr
Dim aOptions(NOPTIONSDATA) As Ptr
Dim a as Boolean
Dim x,z as string

aInputData(0) = ptr(1)
aInputData(1) = ptr(32)
aInputData(2) = ptr(50)
aInputData(5) = ptr(2000)
aInputData(14) = ptr(4)
aInputData(15) = ptr(10)
aInputData(16) = ptr(2)
aInputData(17) = ptr(4)
aInputData(18) = ptr(500)
aInputData(26) = ptr(7)
aInputData(27) = ptr(12)
aInputData(51) = ptr(0)

a = StartJob(aInputData(0), aResult(0), aOptions(0))
' show data results
' ShowResult(aResult)
Msgbox("Result: " + str(aResult(29)))' Simple result processing

x= "Breakpoint" to view variables during runtime



Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Mon May 14, 2012 7:47 am 
Offline

Joined: Fri May 11, 2012 7:43 am
Posts: 14
This works to store the data but still failing to call the function correctly.

So getting closer.


Dim aInputData As new MemoryBlock(100)
Dim aResult As new MemoryBlock(100)
Dim aOptions As new MemoryBlock(1)

Dim a as Boolean
Dim x,z as string

aInputData.doublevalue(0) = (1)
aInputData.doublevalue(1) = (32)
aInputData.doublevalue(2) = (50)
aInputData.doublevalue(5) = (2000)
aInputData.doublevalue(14) = (4)
aInputData.doublevalue(15) = (10)
aInputData.doublevalue(16) = (2.5)
aInputData.doublevalue(17) = (4)
aInputData.doublevalue(18) = (500)
aInputData.doublevalue(26) = (7)
aInputData.doublevalue(27) = (12)
aInputData.doublevalue(51) = (0)

a = StartJob(aInputData, aResult, aOptions)

' show data results
' ShowResult(aResult)
Msgbox("Result: " + str(aResult.cstring(29)))' Simple result processing


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Mon May 14, 2012 6:51 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Quote:
Declare function StartJob Lib "c:\calcdll.dll" (ByRef p1 As Double, ByRef p2 As Variant, ByRef p3 as Double) As Boolean

The first and third parameters are a single double value, not an array. You can leave them as is. The variant is the one you need to change to Ptr.

(ByRef p1 as Double, p2 as Ptr, ByRef p3 as Double)

When you construct the memoryblock, bear in mind that a double is 8 bytes, so the first double is in position 0, and the second double is in position 8. The third double is in position 16.

Again, from what you said originally, the first parameter is a double value, why are you passing it an array?


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Tue May 15, 2012 3:22 am 
Offline

Joined: Fri May 11, 2012 7:43 am
Posts: 14
The program is for selecting coils,

the .dll is from a supplier who makes the coils. We give it the physical dimentions and thermal criteria and it tells what it is capable of as well as other useful info re: weight, cost etc...

The input array details a coil, the rows, circuits, fin material etc. Which we load as described above.

We then put this array into the function, and via byref, the result array is changed to contain all the resulting data. The results array is all double except from one component.

So to sumise, the input is an array of doubles, the result is an array of mostly doubles with one string. Its a quaint way of doing it before objects were common. (Its old code reused over many, many years without a formal update to OOP)


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Tue May 15, 2012 3:44 am 
Offline

Joined: Fri May 11, 2012 7:43 am
Posts: 14
Just had a quick play with it and it now says it cant accept certain parameters. could that be becuase im trying to pass an array byref?

thank you,


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Tue May 15, 2012 7:25 am 
Offline

Joined: Fri May 11, 2012 7:43 am
Posts: 14
When creating a MemoryBlock do i have to specify somewhere that it is 100 large with doubles except point 29 which is a string or do i compensate for this when i read from it after the StartJob?

I have searched for info on a memory block but nothing seems to explain it in a way i want to use it. Is it possible to do what I'm looking for?

Also the compiler says i cant pass a parameter as defined by byref.


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Tue May 15, 2012 8:04 am 
Offline

Joined: Fri May 11, 2012 7:43 am
Posts: 14
I have changed the memoryblock size to Dim aResult As new MemoryBlock(552) as this should covers the size of the array for doubles and the string.


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Tue May 15, 2012 9:00 am 
Offline

Joined: Wed May 20, 2009 11:02 am
Posts: 423
A memoryblock is just a chunk of memory reserved for whatever you want to throw in. What you need to take care of is the starting byte and length of each of the pieces of data you are storing (as Tim wrote before). Let's say you have 4 doubles (8 bytes each) and a string (of known length!, 6 bytes, for example). Then, if you need to retrieve the doubles you need to read the data starting at positions 0, 8, 16 and 24 like this:

myMemoryBlock.doublevalue(0) 'gives you the double stored starting at 0
myMemoryBlock.doublevalue(8) 'gives you the double stored starting at 8
...

If your string is at the end of the memoryblock you can use mymemoryblock.cstring(32), or you can read it one byte at time and join them in a string (mymemoryblock.byte(32)+mymemoryblock.byte(33)+...+ mymemoryblock.byte(37) for example, although this should be done in a loop). The results of these two methods are not exactly the same, please check the language reference.

Pixe

_________________
Using RS2011r4.3 on Windows7.


Top
 Profile  
Reply with quote  
 Post subject: Re: Declaring a function and using Variants
PostPosted: Tue May 15, 2012 9:03 am 
Offline

Joined: Wed May 20, 2009 11:02 am
Posts: 423
Quote:
Also the compiler says i cant pass a parameter as defined by byref.


Tim suggested to use the pointer byval. Have you tried that? I think at this point it would help if you posted your call to the dll.

Pixe

_________________
Using RS2011r4.3 on Windows7.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group