[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Pass long* to VC++COM object from C#

Pix

10/3/2007 10:18:00 AM

I'm using in a c# project an activeX component (which I can't modify)
with these 3 methods:

public void PutData(ref int pData, int nBytes);
public int GetDataSize();
public void GetData(ref int pData);

The same functions, in a c++ project are:

void PutData(long* pData, long nBytes);
long GetDataSize();
void GetData(long* pData);


With the first function I pass a buffer to the component and the
component copies it in its own buffer.
With the third function I pass a buffer to the component and the
component copies in my buffer the data that it previously stored in
its own buffer.

I can't get the component work properly, because it seems to copy only
the first 4 bytes (= the first integer).

It seems the problem is in a mis-interpreted parameter; I found this
(http://www.dotnet247.com/247reference/msgs/55/2...) discussion
about a similar problem, but when I run the program I receive a type
mismatch runtime error.
I'm probably modifying the interfaces in a wrong way, but I can't find
what's the right variable type to replace.

4 Answers

Rexxowski

10/3/2007 12:46:00 PM

0

If you pass some kind of managed array like byte[], try following:

declare void PutData(IntPtr pData, int nBytes);
// you must pin the array to avoid moving the array within GC heap
GCHandle pinnedArr = GCHandle.Alloc(your byte array);
IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(your byte array, 0);
PutData(pData, (your byte array).Length);
pinnedArr.Free();

if you have other kind of buffer, you probably must use a version of
Marshal.Copy() method.


Pix napsal(a):
> I'm using in a c# project an activeX component (which I can't modify)
> with these 3 methods:
>
> public void PutData(ref int pData, int nBytes);
> public int GetDataSize();
> public void GetData(ref int pData);
>
> The same functions, in a c++ project are:
>
> void PutData(long* pData, long nBytes);
> long GetDataSize();
> void GetData(long* pData);
>
>
> With the first function I pass a buffer to the component and the
> component copies it in its own buffer.
> With the third function I pass a buffer to the component and the
> component copies in my buffer the data that it previously stored in
> its own buffer.
>
> I can't get the component work properly, because it seems to copy only
> the first 4 bytes (= the first integer).
>
> It seems the problem is in a mis-interpreted parameter; I found this
> (http://www.dotnet247.com/247reference/msgs/55/2...) discussion
> about a similar problem, but when I run the program I receive a type
> mismatch runtime error.
> I'm probably modifying the interfaces in a wrong way, but I can't find
> what's the right variable type to replace.
>

Pix

10/4/2007 11:14:00 AM

0

On Oct 3, 2:45 pm, Rexxowski <mrexxow...@hotmail.com> wrote:
> If you pass some kind of managed array like byte[], try following:
>
> declare void PutData(IntPtr pData, int nBytes);

The problem is that I can't change the declaration ;)
I've only the ActiveX's binary code, and when I try changing the
generated c# interface, I receive a mismatch error.

Rexxowski

10/4/2007 11:57:00 AM

0

Pix napsal(a):
> On Oct 3, 2:45 pm, Rexxowski <mrexxow...@hotmail.com> wrote:
>> If you pass some kind of managed array like byte[], try following:
>>
>> declare void PutData(IntPtr pData, int nBytes);
>
> The problem is that I can't change the declaration ;)
> I've only the ActiveX's binary code, and when I try changing the
> generated c# interface, I receive a mismatch error.
>

What do you mean by that you try to change the generated interface?
Do you use the autogenerated assembly?
In such case I can only give advice to duplicate the declaration
of the interface with needed parameter types and wait until somebody
mentions a more sophisticated solution :)

Pix

10/5/2007 8:46:00 AM

0

> What do you mean by that you try to change the generated interface?
> Do you use the autogenerated assembly?

Yes, I change the generated Interop wrapper
assembly, following the instructions in this message:
http://www.dotnet247.com/247reference/msgs/55/2...


> In such case I can only give advice to duplicate the declaration
> of the interface with needed parameter types and wait until somebody
> mentions a more sophisticated solution :)

Let's wait ;)