Listing WIA devices in C++/CLI fails with "cannot convert from 'int' to 'System::Object ^%'" - c++-cli

I'm trying to list all WIA devices with C++/CLI. I'm fairly new to C++/CLI (although I consider myself an intermediate C++ programmer), but I keep getting this error:
error C2664: 'WIA::IDeviceInfos::default::get' : cannot convert parameter 1 from 'int' to 'System::Object ^%'
when using the following code snippet:
DeviceManager^ dm = (gcnew WIA::DeviceManager());
for (int i = 1; i <= dm->DeviceInfos->Count; i++)
{
String^ deviceName = dm->DeviceInfos[i].Properties("Name")->get_Value()->ToString();
this->devices->Items->Add(deviceName);
}
Why should I treat that int as an Object? In Managed C++ there was the concept of boxing, but it doesn't work here and anyway I thought C++/CLI was introduced in order to get rid of it?

The Value property needs some non-obvious code to get it out. Try this:
WIA::DeviceInfo ^ info = dm->DeviceInfos[gcnew System::Int32(i)];
WIA::Property ^ propName = info->Properties[gcnew System::String(L"Name")];
String ^ strName = propName->default->ToString();

Related

How to fill an xtensor array, with complex data type

Paraphrasing here, but this is the problem
xt::pyarray< std::complex<double> > output;
output = 0; // fails to compile
output = double(0); // also fails to compile
output = complex<double>(0); // succeeds
and yet this is fine
std::complex<double> foo;
foo = double(0);
The problem is I'm writing a function which accepts arbitrary xtensor array, eg.
template<typename xtarray_t>
void my_function(xtarray_t &output, const xtarray_t &input) {
output = 0;
}
so in general this
output = complex<double>(0);
would fail. I've seen this, but am not sure if this was implemented, or how to use it, couldn't find the documentation. In general though, I think it should still work with std::complex.
thanks for any help!
compiler output:
xtensor-python/include/xtensor-python/pyarray.hpp:360:20: note: candidate function not viable: no known conversion from 'double' to 'const xt::pyarray<std::__1::complex<double>, xt::layout_type::dynamic>::self_type' (aka 'const pyarray<std::__1::complex<double>, (xt::layout_type)0>') for 1st argument
self_type& operator=(const self_type& rhs);
xtensor-python/include/xtensor-python/pyarray.hpp:363:20: note: candidate function not viable: no known conversion from 'double' to 'xt::pyarray<std::__1::complex<double>, xt::layout_type::dynamic>::self_type' (aka 'pyarray<std::__1::complex<double>, (xt::layout_type)0>') for 1st argument
self_type& operator=(self_type&& e) = default;
xtensor-python/include/xtensor-python/pyarray.hpp:369:20: note: candidate template ignored: could not match 'xexpression<type-parameter-0-0>' against 'double'
self_type& operator=(const xexpression<E>& e);
Answer is to do this
using value_t = typename xtarray_t::value_type;
output = value_t(0);

Have a memset issue in C++/CLI

I'm a newbie currently using c++/cli to wrap a few classes that i have used in my .lib file. And I am in dire need of using "memset" in my c++/cli. Anyone here knows how to use memset in c++/cli code?
The c++ code I'm trying to use in my c++/cli code:
memset(&DeviceInfo, 0, sizeof(FS_DEVICE_INFO));
Here's my c++/cli Code where I get the error when I try to use the same memset line from my c++ code:
bool newIFSWDevice::GetDeviceInfo(PFS_DEVICE_INFO pDevInfo)
{
IFSDevice* pDeviceWheel = nullptr;
FS_DEVICE_INFO DeviceInfo;
int x = 0;
while (nullptr != (pDeviceWheel = newFSDeviceEnumerator::EnumerateInstance(x++)))
{
memset(&DeviceInfo, 0, sizeof(FS_DEVICE_INFO)); //error line
pDeviceWheel->GetDeviceInfo(&DeviceInfo);
if (0 == wcscmp(DeviceInfo.Name, FS_DEVICE_WHEEL_PORSCHE_NAME)
break;
I tried using a for loop instead...
for (int i = 0; i <= sizeof(FS_DEVICE_INFO); i++)
FS_DEVICE_INFO[i] = 0;
But it still gives me an error "expression must have a constant value". Help would be much appreciated! :)
As noted in the comments, you're missing the header file #include <string.h>. See the documentation.
It's also worth noting that your for loop to do the clearing has several problems:
for (int i = 0; i <= sizeof(FS_DEVICE_INFO); i++)
FS_DEVICE_INFO[i] = 0;
sizeof(FS_DEVICE_INFO) gives you the size of that struct in bytes, but FS_DEVICE_INFO[i] indexes into an array of structs: [1] would be the second struct in the array, not the second byte! You would need to cast the pointer to char or something similar.
i <= sizeof(FS_DEVICE_INFO): The <= is incorrect. If the struct is 10 bytes large, you'd end up operating on bytes 0 through 10, which is 11 bytes total, stomping on whatever happened to be after the struct.
FS_DEVICE_INFO[i]: FS_DEVICE_INFO is the name of the class, your local variable is DeviceInfo, so this should be DeviceInfo[i]. This is why you're getting the expression must have a constant value error.

Is g++ 4.5.3 broken when it comes to pointers to lamba functions?

I was trying out lambda functions and making a jump table to execute them, but I found g++ didn't recognize the type of the lambda function such that I could assign them to an array or tuple container.
One such attempt was this:
auto x = [](){};
decltype(x) fn = [](){};
decltype(x) jmpTable[] = { [](){}, [](){} };
On compilation I get these errors:
tst.cpp:53:27: error: conversion from ‘main()::<lambda()>’ to non-scalar type ‘main()::<lambda()>’ requested
tst.cpp:54:39: error: conversion from ‘main()::<lambda()>’ to non-scalar type ‘main()::<lambda()>’ requested
Hmmmm, can't convert from type A to non-scalar type A? What's that mean? o.O
I can use std::function to get this to work, but a problem with that is it doesn't seem to work with tuple:
function<void()> jmpTable[] = [](){}; // works
struct { int i; function<void()>> fn; } myTuple = {1, [](){}}; // works
tuple<int, function<void()>> stdTuple1 = {1, [](){}}; // fails
tuple<int, function<void()>> stdTuple2 = make_tuple(1, [](){}); // works
tst.cpp:43:58: error: converting to ‘std::tuple<int, std::function<void()> >’ from initializer list would use explicit constructor ‘std::tuple<_T1, _T2>::tuple(_U1&&, _U2&&) [with _U1 = int, _U2 = main()::<lambda()>, _T1 = int, _T2 = std::function<void()>]’
Constructor marked explicit? Why?
So my question is if I am doing something invalid or is this version just not quite up to the task?
Hmmmm, can't convert from type A to non-scalar type A? What's that mean? o.O
No, that's not a conversion to the same type. Despite having identical bodies, the different lambdas have different types. Newer versions of GCC make this clearer, and give the error message:
error: conversion from '__lambda1' to non-scalar type '__lambda0' requested
clang does even better:
error: no viable conversion from '<lambda at test.cc:2:18>' to 'decltype(x)' (aka '<lambda at test.cc:1:10>')
I can use std::function to get this to work, but a problem with that is it doesn't seem to work with tuple:
It does (with 4.5.4, at least, I don't have 4.5.3 to test), but your initialisation isn't quite right.
tuple<int, function<void()>> stdTuple1 {1, [](){}}; // lose the = to initialise stdTuple1 directly
I'm not sure about the state of n3043 in 4.5.3, but you should be able to use function pointer conversion. If I'm not misunderstanding your usage intentions, this may work for you;
void (*x)();
decltype(x) fn = [](){};
decltype(x) jmpTable[] = { [](){}, [](){} };

how to assign pointer in a managed array of pointers c++/cli?

I am new to managed code and i need to pass array of pointers to different structures to windows form using C++/CLI , but it didn`t work !
My problem is in the managed array, how can i correctly access its elements .
The code sequence :
array<void*> ^ ptr;//here ptr value is undefined , type array<void*> ^
ptr = gcnew array<void*> (2);// length 0x2 , 0x0 and 0x1 values are undefined of type void
class1::struct1 structObj1;
class2::struct2 structObj2;
ptr[0] = &structObj1;// value is empty of type void!!
ptr[1] = &structObj2;//value is empty of type void!!
When i watched ptr , i found the above comments.
Notice that repeating code but using unmanaged array works probably
void* ptr[2];//here ptr value is undefined , type void*[]
class1::struct1 structObj1;
class2::struct2 structObj2;
ptr[0] = &structObj1;// value is address1 of type void*
ptr[1] = &structObj2;//value is address2 of type void*
Can anyone see where is the problem??
Do I need to use unmanaged array then convert to managed? If yes, how can I do it ??
Passing unmanaged pointers in a managed array may be valid C++/CLI, but it's definitely not the ideal way to do things. Do consider creating a custom managed class (ref class in C++/CLI) to hold the structures, instead of passing around pointers.
For this, I'm assuming that struct1 and struct2 are unmanged structs. This answer only applies if that is the case.
Your existing code works for me. Here's my version, with some debugging added in.
public struct struct1 { int foo; };
public struct struct2 { float bar; };
int main(array<System::String ^> ^args)
{
array<void*> ^ ptr;
ptr = gcnew array<void*> (2);
for(int i = 0; i < ptr->Length; i++)
Debug::WriteLine("ptr[{0}] = {1:X8}", i, reinterpret_cast<int>(ptr[i]));
struct1 structObj1;
struct2 structObj2;
ptr[0] = &structObj1;
ptr[1] = &structObj2;
for(int i = 0; i < ptr->Length; i++)
Debug::WriteLine("ptr[{0}] = {1:X8}", i, reinterpret_cast<int>(ptr[i]));
struct1* pointerToStructObj1 = reinterpret_cast<struct1*>(ptr[0]);
structObj1.foo = 4;
Debug::WriteLine("pointerToStructObj1->foo = {0}", pointerToStructObj1->foo);
}
Output:
ptr[0] = 00000000
ptr[1] = 00000000
ptr[0] = 0013F390
ptr[1] = 0013F394
pointerToStructObj1->foo = 4
Edit
To use Debug::WriteLine, add using namespace System::Diagnostics.
The debugger doesn't know how to display the contents of a void*, so it just displays blank. It does display a null pointer differently, though: null shows up as <undefined value>, non-null shows up as just blank.
My philosophy on C++/CLI is: If you're going to write managed code, write managed code. Consider replacing your vector with a managed List. If you still need unmanaged objects, I strongly urge you to consider writing a managed class with properly typed pointers, rather than a void* array.
To implement such a class, create whatever fields you need, just be sure that they're pointers, not direct. (vector<foo>* instead of vector<foo>.) Create the objects with new in the constructor, and delete them in the destructor (which is called on Dispose) & finalizer.

cannot convert from Icollection to Icollection c /cli

i need to fill a collection with another list in c++/cli so the problem that when i try to do that i got an error
error C2664: 'SpaceClaim::Api::V10::InteractionContext::Selection::set' : cannot convert parameter 1 from 'System::Collections::Generic::ICollection ^' to 'System::Collections::Generic::ICollection ^'
and here the code
List<DesignEdge^> ^newEdges = gcnew List<DesignEdge^>();
for each (DesignEdge^edge in onecopiedBody->Edges)
{
if (!edges->Contains(edge))
{
newEdges->Add(edge);
}
}
cstom->InteractionContext->Selection = safe_cast<ICollection<IDesignEdge^> ^>(newEdges); //error here
The problem is that you're trying to cast from ICollection<DesignEdge^>^ to ICollection<IDesignEdge^>^, which is not safe. What you should do is operate in terms of IDesignEdge from the start:
auto newEdges = gcnew List<IDesignEdge^>();