Problem in using COM object in .NET

  • Thread starter Thread starter Arsalan Ahmad
  • Start date Start date
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
 
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
 
Back
Top