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

Re: Problem in using COM object in .NET by Brian

Brian
Sun Feb 22 23:44:51 CST 2004

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

inline....

"Arsalan Ahmad" <arsal__@hotmail.com> wrote in message
news:eSOuFcc#DHA.3068@tk2msftngp13.phx.gbl...
> 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



Re: Problem in using COM object in .NET by Simon

Simon
Mon Feb 23 09:19:52 CST 2004

.. or implementing IStream if that is more appropriate?

S.

"Brian Muth" <bmuth@mvps.org> wrote in message
news:%23z%23O79c%23DHA.3820@TK2MSFTNGP09.phx.gbl...
> 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
>
>