473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to receive an array of doubles using VB from a C DLL

Hi all,

Here is a problem I first struck a while back in the Compact Framework. I am
now using the full Framework (v2.0) and wanted to know whether there is
anyway around this issue without building separate shim code in another DLL.

Here is the layout of the external C DLL function:

int thisFunction( [in] double a, [in] int b, [out] double* c)

The double* c argument passes back an array of 6 doubles.

My VB code is like this:

<DllImport("External.DLL")_
Public Shared Function thisFunction( ByVal a As Double, ByVal b As Integer,
ByRef c As Double() ) As Integer
End Function
...
...
...
Dim iRet As Integer
Dim a As Double
Dim b As Integer
Dim c(5) As Double

a = 36.12
b = 4

iRet = thisFunction( a, b, c )
....
....

At the moment, the call to this function is crashing. What is the best
solution for this type of problem on the full Framework?

Regards,
Neville Lang
Oct 23 '06 #1
10 2120
Is that an old C dll, or a newer C++ dll? If it's old, it may be using
a 16 bit int, but you are passing it a 32 bit int. That may be what's
making it crash, rather than the array of doubles.

T

Neville Lang wrote:
>Hi all,

Here is a problem I first struck a while back in the Compact Framework. I am
now using the full Framework (v2.0) and wanted to know whether there is
anyway around this issue without building separate shim code in another DLL.

Here is the layout of the external C DLL function:

int thisFunction( [in] double a, [in] int b, [out] double* c)

The double* c argument passes back an array of 6 doubles.

My VB code is like this:

<DllImport("External.DLL")_
Public Shared Function thisFunction( ByVal a As Double, ByVal b As Integer,
ByRef c As Double() ) As Integer
End Function
..
..
..
Dim iRet As Integer
Dim a As Double
Dim b As Integer
Dim c(5) As Double

a = 36.12
b = 4

iRet = thisFunction( a, b, c )
...
...

At the moment, the call to this function is crashing. What is the best
solution for this type of problem on the full Framework?

Regards,
Neville Lang

Oct 23 '06 #2
The DLL is using 32-bit INTs, if that is any help.

Regards,
Neville Lang

"tomb" <to**@technetcenter.comwrote in message
news:hv***************@bignews5.bellsouth.net...
Is that an old C dll, or a newer C++ dll? If it's old, it may be using a
16 bit int, but you are passing it a 32 bit int. That may be what's
making it crash, rather than the array of doubles.

T

Neville Lang wrote:
>>Hi all,

Here is a problem I first struck a while back in the Compact Framework. I
am now using the full Framework (v2.0) and wanted to know whether there is
anyway around this issue without building separate shim code in another
DLL.

Here is the layout of the external C DLL function:

int thisFunction( [in] double a, [in] int b, [out] double* c)

The double* c argument passes back an array of 6 doubles.

My VB code is like this:

<DllImport("External.DLL")_
Public Shared Function thisFunction( ByVal a As Double, ByVal b As
Integer, ByRef c As Double() ) As Integer
End Function
..
..
..
Dim iRet As Integer
Dim a As Double
Dim b As Integer
Dim c(5) As Double

a = 36.12
b = 4

iRet = thisFunction( a, b, c )
...
...

At the moment, the call to this function is crashing. What is the best
solution for this type of problem on the full Framework?

Regards,
Neville Lang


Oct 23 '06 #3
Neville,
><DllImport("External.DLL")_
Public Shared Function thisFunction( ByVal a As Double, ByVal b As Integer,
ByRef c As Double() ) As Integer
^^^^^

Should be ByVal
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 23 '06 #4
Neville,

You state that 6 doubles are returned but you are only allocating an
array of 5 to receive those 6 values.

Dim c(6) As Double might do the trick...
Neville Lang wrote:
int thisFunction( [in] double a, [in] int b, [out] double* c)

The double* c argument passes back an array of 6 doubles.
Dim c(5) As Double

a = 36.12
b = 4

iRet = thisFunction( a, b, c )
Oct 23 '06 #5
Hi Mattias,

I tried your suggestion but it still gives an error.

I am going to revert to my shim code approach as I did in the Compact
Framework, and will report back if I get it working. I had hoped the full
Framework would not have required separate shim code in another DLL to make
receiving 6 doubles work. ( [out] double * construct in the C++ DLL)

Regards,
Neville Lang


"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Neville,
>><DllImport("External.DLL")_
Public Shared Function thisFunction( ByVal a As Double, ByVal b As
Integer,
ByRef c As Double() ) As Integer
^^^^^

Should be ByVal
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Oct 24 '06 #6
I tried your suggestion but this did not change anything.

I mainly have a C# background and I understand that in VB .NET, you specify
a value of one less when dimensioning an array as you do in C#. As I
understand it, the figure that is used for the dimension of an array in
VB.NET is the number of the last element - so in my case to get 6 elements
in an array, elements 0 - 5, I dimension an array using "array(5)" in
VB.NET.

As I stated in my reply to Mattias, I will try out my shim code and approach
report back.

Regards,
Neville Lang


"FishingScout" <fi**********@comcast.netwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Neville,

You state that 6 doubles are returned but you are only allocating an
array of 5 to receive those 6 values.

Dim c(6) As Double might do the trick...
Neville Lang wrote:
>int thisFunction( [in] double a, [in] int b, [out] double* c)

The double* c argument passes back an array of 6 doubles.
>Dim c(5) As Double

a = 36.12
b = 4

iRet = thisFunction( a, b, c )

Oct 24 '06 #7
Neville,
In addition to the other comments, I understand that you need to pass the
array ByVal (as Mattias suggests) plus you need to use the MarshalAs
attribute to indicate an array pointer & the size of the array.

Something like:

Public Declare Function thisFunction Lib "External.DLL" ( _
ByVal a As Double, _
ByVal b As Integer, _
<Out(), MarshalAs(UnmanagedType.LPArray, SizeConst:=6)_
ByVal c As Double() _
) As Integer
For details see:

http://msdn2.microsoft.com/en-us/library/z6cfh6e6.aspx
--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Neville Lang" <neville@MAPS_ONnjlsoftware.comwrote in message
news:eI**************@TK2MSFTNGP05.phx.gbl...
Hi all,

Here is a problem I first struck a while back in the Compact Framework. I
am now using the full Framework (v2.0) and wanted to know whether there is
anyway around this issue without building separate shim code in another
DLL.

Here is the layout of the external C DLL function:

int thisFunction( [in] double a, [in] int b, [out] double* c)

The double* c argument passes back an array of 6 doubles.

My VB code is like this:

<DllImport("External.DLL")_
Public Shared Function thisFunction( ByVal a As Double, ByVal b As
Integer, ByRef c As Double() ) As Integer
End Function
..
..
..
Dim iRet As Integer
Dim a As Double
Dim b As Integer
Dim c(5) As Double

a = 36.12
b = 4

iRet = thisFunction( a, b, c )
...
...

At the moment, the call to this function is crashing. What is the best
solution for this type of problem on the full Framework?

Regards,
Neville Lang
Oct 24 '06 #8
Jay,

Your feedback on this topic has fixed my problem.

Thank you also for the link to the MSDN information - it was very useful to
understand what is going on here. I must admit that one of my weaknesses in
..NET programming is in using Platform Invoke and the correct construction of
the arguments.

I slightly departed from your code after reading MSDN - I did not use the
Out() attribute - only the MarshalAs() was all that was needed. I also found
that if I only typed in the <MarshalAs(...)attribute followed by the
argument name, VB.NET automatically put in the "ByVal".

I can now move on...

Regards,
Neville Lang

"Jay B. Harlow" <Ja************@tsbradley.netwrote in message
news:AE**********************************@microsof t.com...
Neville,
In addition to the other comments, I understand that you need to pass the
array ByVal (as Mattias suggests) plus you need to use the MarshalAs
attribute to indicate an array pointer & the size of the array.

Something like:

Public Declare Function thisFunction Lib "External.DLL" ( _
ByVal a As Double, _
ByVal b As Integer, _
<Out(), MarshalAs(UnmanagedType.LPArray, SizeConst:=6)_
ByVal c As Double() _
) As Integer
For details see:

http://msdn2.microsoft.com/en-us/library/z6cfh6e6.aspx
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Neville Lang" <neville@MAPS_ONnjlsoftware.comwrote in message
news:eI**************@TK2MSFTNGP05.phx.gbl...
>Hi all,

Here is a problem I first struck a while back in the Compact Framework. I
am now using the full Framework (v2.0) and wanted to know whether there
is anyway around this issue without building separate shim code in
another DLL.

Here is the layout of the external C DLL function:

int thisFunction( [in] double a, [in] int b, [out] double* c)

The double* c argument passes back an array of 6 doubles.

My VB code is like this:

<DllImport("External.DLL")_
Public Shared Function thisFunction( ByVal a As Double, ByVal b As
Integer, ByRef c As Double() ) As Integer
End Function
..
..
..
Dim iRet As Integer
Dim a As Double
Dim b As Integer
Dim c(5) As Double

a = 36.12
b = 4

iRet = thisFunction( a, b, c )
...
...

At the moment, the call to this function is crashing. What is the best
solution for this type of problem on the full Framework?

Regards,
Neville Lang

Oct 24 '06 #9
Neville,
The <Outis a hint to the marshaller on what direction the parameter's data
is moving. I included it as your original C prototype included it.
I must admit that one of my weaknesses in .NET programming is in using
Platform Invoke and the correct construction of the arguments.
I find www.pinvoke.net to be invaluable when working with P/Invoke. I also
recommend Adam Nathan's book ".NET and COM - The Complete Interoperability
Guide" from SAMS press if you are doing a lot of P/Invoke.

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Neville Lang" <neville@MAPS_ONnjlsoftware.comwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
Jay,

Your feedback on this topic has fixed my problem.

Thank you also for the link to the MSDN information - it was very useful
to understand what is going on here. I must admit that one of my
weaknesses in .NET programming is in using Platform Invoke and the correct
construction of the arguments.

I slightly departed from your code after reading MSDN - I did not use the
Out() attribute - only the MarshalAs() was all that was needed. I also
found that if I only typed in the <MarshalAs(...)attribute followed by
the argument name, VB.NET automatically put in the "ByVal".

I can now move on...

Regards,
Neville Lang

"Jay B. Harlow" <Ja************@tsbradley.netwrote in message
news:AE**********************************@microsof t.com...
>Neville,
In addition to the other comments, I understand that you need to pass the
array ByVal (as Mattias suggests) plus you need to use the MarshalAs
attribute to indicate an array pointer & the size of the array.

Something like:

Public Declare Function thisFunction Lib "External.DLL" ( _
ByVal a As Double, _
ByVal b As Integer, _
<Out(), MarshalAs(UnmanagedType.LPArray, SizeConst:=6)_
ByVal c As Double() _
) As Integer
For details see:

http://msdn2.microsoft.com/en-us/library/z6cfh6e6.aspx
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Neville Lang" <neville@MAPS_ONnjlsoftware.comwrote in message
news:eI**************@TK2MSFTNGP05.phx.gbl...
>>Hi all,

Here is a problem I first struck a while back in the Compact Framework.
I am now using the full Framework (v2.0) and wanted to know whether
there is anyway around this issue without building separate shim code in
another DLL.

Here is the layout of the external C DLL function:

int thisFunction( [in] double a, [in] int b, [out] double* c)

The double* c argument passes back an array of 6 doubles.

My VB code is like this:

<DllImport("External.DLL")_
Public Shared Function thisFunction( ByVal a As Double, ByVal b As
Integer, ByRef c As Double() ) As Integer
End Function
..
..
..
Dim iRet As Integer
Dim a As Double
Dim b As Integer
Dim c(5) As Double

a = 36.12
b = 4

iRet = thisFunction( a, b, c )
...
...

At the moment, the call to this function is crashing. What is the best
solution for this type of problem on the full Framework?

Regards,
Neville Lang

Oct 24 '06 #10
Jay,

Thank you for the PInvoke.net link and the reference to the book. I have a
number of .NET books but not that one.

I have been working with C# on the Compact Framework for the last few years
so using VB.NET on the full Framework has taken me a bit more time to used
to the VB.NET style. I gave up on VB when it was about version 4 so this
project has given me the opportunity to get back up to speed with the VB
..NET version.

I only occasionally need to do some P/Invoke things and when I do, it is an
area I seem to get caught on. I am quite comfortable in the Compact
Framework (CF) for Pocket PCs and the need to write shim code in a separate
DLL for some calls to make some things work, due to the limited number of
things the Marshal class can do in CF.

Until recently, I have not done much on the full Framework and when this
P/Invoke came up, I just knew that there should have been an easier way to
solve my problem with the larger number of features in marshaling in the
full Framework.

Thanks again.

Regards,
Neville Lang


"Jay B. Harlow" <Ja************@tsbradley.netwrote in message
news:16**********************************@microsof t.com...
Neville,
The <Outis a hint to the marshaller on what direction the parameter's
data is moving. I included it as your original C prototype included it.
> I must admit that one of my weaknesses in .NET programming is in using
Platform Invoke and the correct construction of the arguments.
I find www.pinvoke.net to be invaluable when working with P/Invoke. I also
recommend Adam Nathan's book ".NET and COM - The Complete Interoperability
Guide" from SAMS press if you are doing a lot of P/Invoke.

--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Neville Lang" <neville@MAPS_ONnjlsoftware.comwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
>Jay,

Your feedback on this topic has fixed my problem.

Thank you also for the link to the MSDN information - it was very useful
to understand what is going on here. I must admit that one of my
weaknesses in .NET programming is in using Platform Invoke and the
correct construction of the arguments.

I slightly departed from your code after reading MSDN - I did not use the
Out() attribute - only the MarshalAs() was all that was needed. I also
found that if I only typed in the <MarshalAs(...)attribute followed by
the argument name, VB.NET automatically put in the "ByVal".

I can now move on...

Regards,
Neville Lang

"Jay B. Harlow" <Ja************@tsbradley.netwrote in message
news:AE**********************************@microso ft.com...
>>Neville,
In addition to the other comments, I understand that you need to pass
the array ByVal (as Mattias suggests) plus you need to use the MarshalAs
attribute to indicate an array pointer & the size of the array.

Something like:

Public Declare Function thisFunction Lib "External.DLL" ( _
ByVal a As Double, _
ByVal b As Integer, _
<Out(), MarshalAs(UnmanagedType.LPArray, SizeConst:=6)_
ByVal c As Double() _
) As Integer
For details see:

http://msdn2.microsoft.com/en-us/library/z6cfh6e6.aspx
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Neville Lang" <neville@MAPS_ONnjlsoftware.comwrote in message
news:eI**************@TK2MSFTNGP05.phx.gbl...
Hi all,

Here is a problem I first struck a while back in the Compact Framework.
I am now using the full Framework (v2.0) and wanted to know whether
there is anyway around this issue without building separate shim code
in another DLL.

Here is the layout of the external C DLL function:

int thisFunction( [in] double a, [in] int b, [out] double* c)

The double* c argument passes back an array of 6 doubles.

My VB code is like this:

<DllImport("External.DLL")_
Public Shared Function thisFunction( ByVal a As Double, ByVal b As
Integer, ByRef c As Double() ) As Integer
End Function
..
..
..
Dim iRet As Integer
Dim a As Double
Dim b As Integer
Dim c(5) As Double

a = 36.12
b = 4

iRet = thisFunction( a, b, c )
...
...

At the moment, the call to this function is crashing. What is the best
solution for this type of problem on the full Framework?

Regards,
Neville Lang



Oct 25 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: bsder | last post by:
Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { {...
3
by: Joakim Hove | last post by:
Hello, I have an array of doubles, allocated with dbarr = malloc(N * sizeof(double)); I then want to set all the elements of the array to zero, this is currently done with for (i=0; i <...
1
by: Adi Lazar | last post by:
Hi, I'm using an ActiveX control that has a read/write property named InsertionPoint (type = object). In the ActiveX documentation : Variant (three-element array of doubles); I can write to...
5
by: John Dumais | last post by:
Hello, I have been trying to figure out how to write an array of doubles (in this specific case) to a binary stream without using a loop. What I have been doing is... foreach(double d in...
5
by: Tales Normando | last post by:
The title says it all. Anyone?
2
by: jccorreu | last post by:
I'm taking in data from multiple files that represents complex numbers for charateristics of different elements. I begin with arrays of doubles and interpolate to get standard values in real and...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
2
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.