ArrayList interop

G

Guest

Hi,

I've got the following problem: I need to create a special Dll that is based
on MFC, but also includes also managed code (which works fine and isn't a
problem). The problem occurrs in the following:

struct TRANSFER {
BYTE data;
__int64 hits;
};

void MyTestFunc(ArrayList* list)
{
TRANSFER a;
a.data = 12; //just some test data
a.hits = 13;
list->Add(a); //compiler error
}

Now, when I try to compile this I receive the following error:

C:\Interface.cpp(24): error C2664: 'System::Collections::ArrayList::Add' :
cannot convert parameter 1 from 'TRANSFER' to 'System::Object __gc *'

But how can I marshal this correctly? Any ideas?

Thanks,

Peter
 
J

Jochen Kalmbach [MVP]

Hi Peter!
struct TRANSFER {
BYTE data;
__int64 hits;
};

void MyTestFunc(ArrayList* list)
{
TRANSFER a;
a.data = 12; //just some test data
a.hits = 13;
list->Add(a); //compiler error
}

Now, when I try to compile this I receive the following error:

C:\Interface.cpp(24): error C2664: 'System::Collections::ArrayList::Add' :
cannot convert parameter 1 from 'TRANSFER' to 'System::Object __gc *'

What do you want to store in the ArrayList? Only managed data can be
stored in the ArrayList, so you need to declare a managed-type for your
structure:

public __value struct Transfer
{
System::Byte data;
System::Int64 hits;
};

void MyTestFunc(ArrayList* list)
{
TRANSFER a;
a.data = 12; //just some test data
a.hits = 13;

// marshal the data to the managed struct:
Transfer t;
t.data = a.data;
t.hits = a.hits;
al->Add(__box(t));
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top