473,804 Members | 3,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing a vector of doubles

Hi

Kind of newbie question:

We have a DLL with a function with a prototype like

void mult( int mtxhandle, double* x, double* y, int size );

where "size" is the size of both x and y vectors. What should be the correct
sintax to define its respective DllImport wrapper in order to use it like:

Double x[] = new Double[mysize];
Double y[] = new Double[mysize];

mult( handle, x, y, mysize );

Thank you

Henrique
Nov 16 '05 #1
4 1414
Henrique,
What should be the correct
sintax to define its respective DllImport wrapper


static extern void mult(int mtxhandle, double[] x, double[] y, int
size);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Hi Mattias,

That does not work since the compiler complains with
C2146: syntax error : missing ')' before identifier 'x'

if you try
double x[] instead of double[] x
then you get
C2664: 'mult':cannot convert parameter 2 from 'double __gc[]' to ' double
[]'

I'm looking into
[DllImport("libb mc")]
extern "C" int mult( int handle, System::IntPtr x, System::IntPtr y, int
sign );

GCHandle xh = GCHandle::Alloc ( x, GCHandleType::P inned );
IntPtr xptr = Marshal::Unsafe AddrOfPinnedArr ayElement( x, 0 );
(same for yh)

int result = mult( handle, xptr, yptr, sign );

xh.Free();
yh.Free();

but not sure if this is the right way to do it. Indeed, it looks like the
pair GCHandle::Alloc and GCHandle::Free will have a big impact on
performance.

Henrique

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:O7******** ******@TK2MSFTN GP11.phx.gbl...
Henrique,
What should be the correct
sintax to define its respective DllImport wrapper


static extern void mult(int mtxhandle, double[] x, double[] y, int
size);

Mattias

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

Nov 16 '05 #3
Henrique,

Since the name of this group is
microsoft.publi c.dotnet.langua ges.csharp, I assumed you were coding in
C#. Now I see you are actually writing managed C++ code.

I believe the MC++ syntax would be

int mult( int handle, Double x[], Double y[], int sign );

or

int mult( int handle, double x __gc[], Double y __gc[], int sign );

But you may want to post your question in
microsoft.publi c.dotnet.langua ges.vc instead to get a definite answer.

I'm looking into
[DllImport("libb mc")]
extern "C" int mult( int handle, System::IntPtr x, System::IntPtr y, int
sign );

GCHandle xh = GCHandle::Alloc ( x, GCHandleType::P inned );
IntPtr xptr = Marshal::Unsafe AddrOfPinnedArr ayElement( x, 0 );
(same for yh)

int result = mult( handle, xptr, yptr, sign );

xh.Free();
yh.Free();


Pinning can be easier than that C++. You should also be able to do

extern "C" int mult( int handle, double *x, double *y, int sign );

and then

Double __pin *xp = &x[0];
Double __pin *yp = &y[0];
int result = mult( handle, xp, yp, sign );

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #4
So, there's no need for explicitly acquiring a handle to this memory?
The simplicity of the c# wrapping was simply astonishing. I was expecting
something a lot more complicated.... :)
that's good though
thanks a lot!
Henrique
Nov 16 '05 #5

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

Similar topics

5
12626
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
8
3971
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
5
3195
by: {AGUT2} {H}-IWIK | last post by:
Hi, I'm trying to pass my vector to a function external to my main(), but my compiler is complaining. FYI, the struct for the facetInfo works perfectly, it's just the vector passing. A quick overview... I have four sets of co-ordinates in each 'facetInfo', and a vector of 'facetInfo's. I want to strip those 'facetInfo' objects that have y-values of zero (.v1y, .v2y and .v3y), and return a 'facetInfo' vector with the remaining...
12
2348
by: BCC | last post by:
If I create a vector of vectors of double: std::vector< std::vector<double> > table1; Are my vectors of doubles uninitialized? Do I have to loop through table1 and initialize each vector of doubles using new? And in cleaning up, manually delete each of these vectors of doubles? Thanks,
8
25434
by: Markus Dehmann | last post by:
I am wondering what is the "best" code to print out the elements of a vector. Assume they contain strings. With "best", I mean shortest and most readable at the same time. Of course, a for loop can be done. But I guess one can do it more elegantly. Something like this would be possible:
5
9024
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the Python code found here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 //beginning #include <vector>
5
3917
by: Michael | last post by:
Hi, once I read here that it is not 'a good idea' to pass variables that are not initialized to a function. I have void something ( double *vector ); ....
2
4160
by: fred_stevens | last post by:
Hi all you C boffins: I need to sort a vector of doubles is ascending order. Qsort will return the sorted vector, but I need a vector of the indices of the sorted vector, not the actual sorted vector. Any ideas? Thanks in advance, Fred.
14
1780
by: ds | last post by:
Hi all, I have to pass an array of doubles to a legacy C function that copies some data using memcpy. The code would look like this: extern "C"{ void legacyCFunctionFill(void* arg); } .... int number=5;
4
5534
by: Anonymous | last post by:
I ahve a vector of doubles taht I need to extract values from. I was just about to use the STL find() algo, but I have a couple of questions: first: can you specify the tolerance threshold to match on doubles? second: if yes, how may this be done (i.e. how many one specify the tolerance threshold for comparing doubles?)
0
9584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.