Problem in using COM object in .NET

A

Arsalan Ahmad

Hi all,

I have written a simple COM object in ATL, one of whose methods is:
STDMETHOD(Write)(/*[in]*/BYTE *buffer, /*[in]*/long count,
/*[out,retval]*/long *bytesWrite);

I have implemented the method as:

STDMETHODIMP CMyControl::Read(BYTE *buffer, long count, long *bytesRead)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())

// TODO: Add your implementation code here
strcpy(buffer, "this is a test");
*bytesRead = count;
return S_OK;
}

The problem is when I try to use it in .NET framework (using C#) like this:

byte[] buffer = new buffer[25];
int count;
IMyControl mc = new MyControlLib.CMyControl();
mc.Read(ref buffer, buffer.Length, count);

I am getting following error:
1. The best overloaded method match for MyControlLib.IMyControl.Read(ref
byte, int)' has some invalid arguments
2. Argument '1': cannot convert from 'ref byte[]' to 'ref byte'

Please tell me how can I resolve this issue.

Thanks,

Arsalan Ahmad
 
B

Brian Muth

I don't think you have debugged your ATL COM object at all.

inline....

Arsalan Ahmad said:
Hi all,

I have written a simple COM object in ATL, one of whose methods is:
STDMETHOD(Write)(/*[in]*/BYTE *buffer, /*[in]*/long count,
/*[out,retval]*/long *bytesWrite);

I assume Write should be Read. You need to indicate to the marsheler how
many BYTES are being transferred in the block of BYTES pointed to by buffer.

This should probably be changed to:

HRESULT Read([in, size_is(count)] BYTE* str, [in] long count, [out, retval]
long *bytesRead);

I have implemented the method as:

STDMETHODIMP CMyControl::Read(BYTE *buffer, long count, long *bytesRead)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())

// TODO: Add your implementation code here
strcpy(buffer, "this is a test");

What's going on here? buffer is an input parameter, is it not? Why are you
overwriting it with another string? Why are you using AFX_MANAGE_STATE if
you are not using MFC?
*bytesRead = count;
return S_OK;
}

The problem is when I try to use it in .NET framework (using C#) like this:

byte[] buffer = new buffer[25];
int count;
IMyControl mc = new MyControlLib.CMyControl();
mc.Read(ref buffer, buffer.Length, count);

I am getting following error:
1. The best overloaded method match for MyControlLib.IMyControl.Read(ref
byte, int)' has some invalid arguments
2. Argument '1': cannot convert from 'ref byte[]' to 'ref byte'

Once you have changed the IDL, you can code it as:

mc.Read(ref buffer[0], buffer.Length, count);

Quite frankly, I find this quite painful, and I think you should really
stick with automation-compliant interfaces. Is there a reason you are not
using BSTRINGS?

Brian
 

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