Visual Basic/VB >> Function Return Values

by Jeff Roberts » Fri, 08 Aug 2003 05:38:04 GMT

I have a DLL written in C++ that has 3 functions. The
first function takes the full file path to an image file
as a string and returns an integer value when it validates
the image. This works fine, returning a 1.

The second function takes that same path as a string, sets
4 variables based on the properties of the image (number
of pixels etc.), and returns and integer (0 or 1) based on
rather it was successful in setting these 4 variables.

I'm fairly new to VB and my question is, is it possible to
set variables within a function that will be accessible to
other procedures of the project? If so how do I access
them. When I call this function it returns a 0 indicating
it was not successful at setting these parameters.

As you can see I'm a little confused.

Regards,
Jeff


Visual Basic/VB >> Function Return Values

by Ben Taylor » Fri, 08 Aug 2003 06:37:30 GMT


What, you mean you can't pass strings into a C++ DLL and have it modify
them? Or do you mean that ByVal doesn't actually quite mean ByVal in the
true VB sense when you have an export that takes a 'ByVal ... as String'
parameter? Because if the former, I think you can because I've done it. It
was ByRef, granted, and it did take a lot of fiddling around converting it
from LPCTSTR to LPTSTR and then it would only work if it had been through a
CString and what not, but it did work in the end.












Visual Basic/VB >> Function Return Values

by Grinder » Fri, 08 Aug 2003 06:53:29 GMT



it modify

No. If that had been the case, I would have said:

"You cannot pass strings into a C++ DLL have it modify them."

in the
as String'
I've done it. It
converting it
been through a

I just wanted to warn the user the the method I outlined would
not work "as is" for strings. So, if this is their
requirement, I would have to elaborate.





Function Return Values

by Ben Taylor » Fri, 08 Aug 2003 18:18:10 GMT

:-).
I just wanted to make sure the alarm bells that tell you
to warn people that 'weird behaviour may be expected' had
kicked in on passing strings 'byval' to APIs.



and have
them."
ByVal
a 'ByVal ...
because
around
it had
would


Function Return Values

by jeff roberts » Wed, 13 Aug 2003 23:49:18 GMT


Thanks for your help. This is exactly what I needed to do. One problem
still remains however. One of the arguments I need to pass to the C++
Dll is an array of type double. I've declared the array in VB, assigned
it arbitrary values, and passed it byRef to the DLL but I get one of two
undesirealbe results: either it returns a subscript out of range error
when I try to check the resultant values or they remain unchanged. Are
there any issues with passing arrays as arguments in this situation?

Thanks Again for your help,
Jeff

Heres the code I'm using to test the function.

Public Declare Function QueryImageInfo Lib "msiadc.dll" (ByVal pathParam
As String, _
ByRef nbandsParam As Long, _
ByRef widthParam As Long, _
ByRef heightParam As Long, _
ByRef mbrParam() As Double) As Integer

Private Sub Command2_Click()
Dim path As String
Dim lret As Integer
Dim numBands As Long
Dim imgWidth As Long
Dim imgHeight As Long
Dim coords(3) As Double

path = "D:\Projects\ADCencryption\MDMO_Detail_0.adc"

numBands = 1
imgWidth = 1
imgHeight = 1
coords(0) = 1
coords(1) = 1
coords(2) = 1
coords(3) = 1

lret = QueryImageInfo(path, numBands, imgWidth, imgHeight, coords())

MsgBox lret
MsgBox numBands & " " & imgWidth & " " & imgHeight
MsgBox coords(0)
MsgBox coords(1)
MsgBox coords(2)
MsgBox coords(3)
End Sub

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Function Return Values

by Bob Butler » Wed, 13 Aug 2003 23:57:57 GMT




<cut>
<cut>

try calling it like this:
lret = QueryImageInfo(path, numBands, imgWidth, imgHeight,
coords(LBound(coords)))

passing the first element of the array ByRef effectively passes the starting
address of the array



Function Return Values

by Bob Butler » Thu, 14 Aug 2003 00:06:17 GMT







one other note.... if the C++ procedure is returning an 'int' then the
function should be "As Long", not "As Integer". a VB Integer is 16 bits and
while a C++ int is 32 bits.



Function Return Values

by jeff roberts » Thu, 14 Aug 2003 00:26:48 GMT


I had read that passing the first element of the array may be the way to
go but when I try coords(Lbound(coords)) or coords(0) I receive the
following error:

Type mismatch: Array or user-defined type expected.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Function Return Values

by Bob Butler » Thu, 14 Aug 2003 00:34:27 GMT





sorry, I overlooked that. Change the declare to send just 1 Double:

Public Declare Function QueryImageInfo Lib "msiadc.dll" (ByVal
pathParam As String, _
ByRef nbandsParam As Long, _
ByRef widthParam As Long, _
ByRef heightParam As Long, _
ByRef mbrParamArray As Double) As Integer

Since the rest of the array follows the first element in memory you can
still get at it all in the DLL. Note that the DLL code must have some way
to know how long the array is. Your other choice is to recode the DLL to
expect a SAFEARRAY structure and you can then go back to passing the array
instead of the first element from VB.



function return values

by JM » Thu, 02 Oct 2003 01:48:01 GMT

Using VBA in Acesss 2000.

Is it possible for a function to return two values? I am
looping through an ADO recordset that has an unparsed name
field. I would like to call a function from within the
loop that parses names into first and last. Can I call
the function once and have the parsed first and last names
returned?

I have tried to write the function to return an array with
the first and last names but how would I captrue the names
in variables with out calling the function twice?

Thanks!

JM


function return values

by Ken Halter » Thu, 02 Oct 2003 03:10:18 GMT

Not sure about VBA... but this works in VB6
'==========
Option Explicit

Private Function MyFunc() As String()
Dim a(1) As String
a(0) = "Ken"
a(1) = "Halter"
MyFunc = a
End Function

Private Sub Command1_Click()
Dim sFirst As String
Dim sLast As String
Dim AnArray() As String
AnArray = MyFunc
sFirst = AnArray(0)
sLast = AnArray(1)
MsgBox "First: " & sFirst & vbCrLf _
& "Last: " & sLast
End Sub
'==========

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..








function return values

by Grinder » Thu, 02 Oct 2003 03:27:15 GMT





Here's a subroutine that returns two values:

Public Sub Swap(ByRef A As Variant, ByRef B as Variant)
Dim T As Variant

T = A
A = B
B = T
End Sub




function return values

by Rick Rothstein » Thu, 02 Oct 2003 03:41:25 GMT

> Is it possible for a function to return two values? I am

Ken has given you the "Function returning an array" solution which, of
course, can conveniently return far more than two values if required.
Something like the following method could be used (depending on what you
have and how it is processed)... it uses a subroutine. The FName and LName
arguments are passed ByRef (the default in VB6 and lower):

Sub MySub(ByVal StringToParse As String, _
FName As String, LName As String)
' Split the string by any means you know how to
' For example, this would work after definining
' the delimiter character or characters.
FName = Split(StringToParse, Delimiter)(0)
LName = Split(StringToParse, Delimiter)(1)
End Sub

Rick - MVP




function return values

by Justin » Thu, 02 Oct 2003 05:09:08 GMT

JM,

I'm going to throw my two-cents in for what's it worth.

Going back a ways, from different languages, and previous beatings on the
head; what is the definition of a function but to return one and only one
value. If you needed to return more than one value, you would use a
subroutine.

Yes, I know that every C routine is a "Function" and there is no specific
subroutine. However, C does also emulate a subroutine by being defined as a
void function().

Your routine may return a boolean value indicating a successfull retrieval
and then use a property or subroutine to actually return the data.

If you take the approach that to return a variant or array, you would assign
the values to something and then processing it, subsequently defeating the
purpose of a function.


Justin

Reply to Newsgroups so that all can learn.







function return values

by Jane Ransom » Thu, 02 Oct 2003 15:12:38 GMT

In article <1c1c101c38844$28aa50f0$ XXXX@XXXXX.COM >, JM
< XXXX@XXXXX.COM > writes
Why not make a 'type' containing two strings, and make the function
return that 'type'.
--
Jane Ransom in Lancaster.
If you need to email me for any other reason, put ransoms
at jandg dot demon dot co dot uk where you see XXXX@XXXXX.COM




Similar Threads

1. scalar function return value into a VBA variable - ADP:Access

2. Filtering on a VB function return value

I've built a VBA function that returns a TRUE/FALSE value.  I've defined the 
return data type of the function as boolean.

In a query I pass certain values from a table to the VBA function.  It works 
just fine returning a result that is displayed as 0 or -1.  However, if I try 
to place a value in the criteria portion of the query, such as TRUE, I get a 
message that it has an incompatible data type.  If get the same message if I 
use either a 0 or -1 for criteria.

In addition, if I run the query without the criteria, and then right-click 
on the field and select either Filter by Selection or Filter Excluding 
Selection, I get the same error message.  In other words, it appears that for 
a function return value in a query no possible filter is allowed.

Does anyone have a work around?  Thanks.

Regards,
Leif

3. Function return values - VB.Net

4. Using sub with byref Param or Function return value

Hi! I'm maybe from old school but, for me, whebn it's time to call a method
from DCOM, remoting or web service returning a
string or anything else I use a function. It's more verbose.

In my new company, much of my coworker works with Public sub ( byval Itemx
as integer, byref returnCode as Integer).

I don't understand why they use this kind of sub and i'm wondering if this
kind of sub take more time to response?

So, my question is, I have to debate this point tomorrow PM, is there any
good raison why we should prefer function to sub with a byref when byref
variable only seve to return a value?

Tks


5. Function return values revisited - VB.Net

6. Date Function Return Values

If i use the date function within a vbscript the value returned is the
system date, but the function does not return the date the way that
the date cmd returns it.
 
Date function in VBscript: 9/10/2003
date command in command shell: 09/10/2003
 
I am going to use the value returned from the Date function to
generate a filename in the form of yyyymmdd. Is there any way that I
can force the Date function to return a two digit value for month and
day, i.e. put the 0 (zero) in there?
 
 
Thanks for any suggestion,
 
 
Darren
 
 
Currently I am just using an If to manipulate the string, but i was
hoping for a option within the function to return the 2 digit values

7. Simple assignment of function return value fails - vbs Rusty

8. Perform OR operation on function return values