Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

return array of type double from VC to VB 2

Status
Not open for further replies.

gismo

Technical User
Jul 24, 2002
35
0
0
US
I am writing a C dll and need to know how to return an array of type double from visual C NOT C++ to Visual Basic.
Both VC and VB are version 6.0.
 
C++ bit
Code:
void dosomething (double* array)
{
   ...
}
VB bit
Code:
declare sub dosomething lib "whatever.dll" (byref double array)
...
dim trouble(20) as double
...
dosomething trouble(0)
...
If you pass in the first element of the array by reference, C will get the address of the first element and can then fill in the rest of the array.
 
XWB that would work if I were passing an array from VB to C++. What I need is to pass a double array from a C dll not C++ to VB
 
The example xwb provided should work fine, except you'll probably have to pass the number of elements in the array in the arguments, too.

Lee
 
I've just tried it - it works from a C DLL to VB. Remember to do the .def file if you're writing the C DLL as well.
 
Once again I reiterate that the code by XWB passes a double array from visual basic to[\b] C. I need to pass a double array from a C dll to VB. There is no return specified in the C code to return anything. The VB code doesn't specify anything being returned to it. My guess is that a SAFEARRAY has to be built in C and passed to VB but I don't know how to do it. in C++ you can use MFC to build a COleSafeArray as VARIANT and pass it back to VB but I haven't been able to do that in C. Thanks for your efforts XWB and Trollacious
 
How do you return an array of doubles from a C function in C? This is what xwb has done in the example he provided. His example shows how to return the array from a C function to VB, just like you wanted.

Lee
 
THANKS GUYS I FINALLY GET IT[\b] since the array address was set in VB when the array was dimensioned and only the address was passed to C, I can use the the array in VB after the function call and the C modifications will have been applied. I'm a little slow. Thanks Again
 
Even though the function doesn't have a return value (i.e. the C function returns void and in VB it's a Sub instead of a Function), that doesn't mean it doesn't return anything.

The Sub declaration in VB says that the argument "array" is passed by reference (via the ByRef keyword). This means that "array" will be changed when the function exits. Similarly, the actual function definition in C accepts a pointer to the double "array" (in this case an array of doubles) which it assumes points to an already allocated array. When the function modifies the array, it's modifying same instance of the array allocated from the VB program.

So while you're passing the array to the C function, this array gets modified, so it's passing back the same array updated with whatever values the C function puts there. Have you tried this yet? Based on your description, this sounds like what you're looking for. If it's not, maybe you should describe more specifically what you're trying to accomplish so we can better assist you.
 
Just when I thought I was out of the woods I found a problem. Some of the values are shifting the decimal. below is the actual value and the value returned. Does anyone have a clue as to what is going on. I printed the array to a file to make sure the values in the array are correct and they were but what I get returned in VB is slightly off:

Actual Returned to VB
0.495602 0.495602
0.365653 0.365653
0.073563 7.356347
0.054910 5.491073
0.005588 5.588535
 
Are you sure it is 5.588535 not 5.588535E-3. How are you printing the numbers?
 
I had originally printed to a file from C to check the values. In VB I was displaying the numbers in a control array of text boxes. After reading your response I printed to a file from VB and sure enough the values are in scientific notation. Is there an easy way to convert from scientific notation to fixed notation and still display 6 decimal places?
 
Its a VB question so I'll post it in the VB forum. Thanks
 
I figured it out. I'm relatively new new to VB. Thanks again XWB and Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top