-1

I'm writing a standalone app for Solidworks using API in Unmanaged C++ and facing some kind of a problem. There're methods for obtaining annotations' position coordinates on the drawing sheet - IGetPosition and GetPosition. Both a stated to have arrays of 3 elements as RetVals. But Visual Studio does not accept me to pass array of doubles to IGetPosition.

Pic1

Meanwhile, API Help has examples for definitions in C# and C++/CLI:

System.double IGetPosition(); 

System.double SW API IGetPosition As I have figured out, System.double is not jast a double, but a struct in Managed C++. I am not famailiar with this stuff as I'm just a beginner. So the question is: How can I represent System.double in Unmanaged code to be accepted as an IGetPosition &parameter?

BTW: GetPosition method takes a VARIANT, but returns a safearray of rank1, which seems not to be compatible with 3 values of XYZ. Also found out something about Marshalization. Is that the right way? If so, would you be so kind to provide me with an example?

Sergey Kolesnik
  • 3,009
  • 1
  • 8
  • 28
  • 2
    What is _unmanaged C++_? Please [clarify](https://stackoverflow.com/posts/48807688/edit). There is a type `double` in standard C++. – Ron Feb 15 '18 at 12:55
  • 1
    In C# there is `double` and `System.Double` there is no `System.double`. – keith Feb 15 '18 at 13:02
  • When not specified, C++ is unmanaged C++, compiled to machine code. In unmanaged C++ you must manage memory allocation manually. ©https://stackoverflow.com/questions/114238/difference-between-managed-c-and-c – Sergey Kolesnik Feb 15 '18 at 13:02
  • @keith I do not use C#, System.double is stated as I wrote it in SW API help. I've updated the post with an url and screenshot. – Sergey Kolesnik Feb 15 '18 at 13:08
  • @Sergey Kolesnik, that documentation is probably wrong. It likely means `double` which is a *value type*. `System.Double` is not a value type, it's a `struct`. `System.double` is just erroneous: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/double. As @Ron said, use C++ `double`. – keith Feb 15 '18 at 13:13
  • @Ron of course there is. And the method accepts double as a parameter, yet it also returns only 1 value, when I need 3. It doesn't accept an array of doubles (see Pic1). Since Sytem::Double is a struct, it can store 3 values. – Sergey Kolesnik Feb 15 '18 at 13:13
  • @keith Documentation says this method to return an Array of XYZ, and having only one double returned for 3 coordinates is no go. System.Double is a struct - I've figured it out and wrote in the post. in C# it takes a struct System.Double and returns a struck, probably. With XYZ. So how can I implement it in usual c++? – Sergey Kolesnik Feb 15 '18 at 13:19
  • @Sergey Kolesnik, you have declared `double XYZ[3]`. `&XYZ` is of type `double**`. Try `XYZ` or `&XYZ[0]` if you must. – keith Feb 15 '18 at 13:21

1 Answers1

1

The problem here is that C++ doesn't allow arrays as function parameters or return value. However, it allows pointers, and arrays decay to pointers. In fact, new double[3] creates an array and returns a pointer.

In particular, arrays decay to to a pointer to their first element. That is to say, double XYZ[3] decays to a double* pointing to XYZ[0].

You used &XYZ. That is also a valid expression, but it gives you a pointer to the array - not to its first element. Just drop the &, and use pAnnf->IGetPosition(XYZ).

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Thanks for an explanation, I didn't know that about arrays. It accepted XYZ as a parameter. But it didn't make things better. It retirns an Array of 0.1 0 0. Firstly, why would it change the precision, because earlier it was 0.12**** (if it did). Secondly, returning 1 value of 3 may confirm that this lib is actually messed up, but still it is doubtfull. – Sergey Kolesnik Feb 15 '18 at 19:44