COM interop problem.

A

Ashish

Hi All,

I have a COM dll that has a method:
[id(3), helpstring("method Add")] HRESULT Add([in] DOUBLE nFirst, DOUBLE
nSecond, [out,retval] DOUBLE* nSum);

STDMETHODIMP CTest::Add(DOUBLE nFirst, DOUBLE nSecond, DOUBLE* nSum)
{
// TODO: Add your implementation code here
if (nSum = NULL)
{
return S_FALSE;
}

*nSum = nFirst + nSecond;
return S_OK;
}

From my C# client I use use it via a ref. Code looks like:

COM.ITest a = new COM.CTestClass();

Double x,y,z;

x = new Double();

y = new Double();

z = new Double();

x = 10;

y = 20;

z = 0;

z = a.Add(x,y);

Console.Out.WriteLine("{0}",z);

I am getting an error : Object reference not set to an instance of an
object.

Does anyone know why ?

Thanks

Ashish
 
N

Nicholas Paldino [.NET/C# MVP]

Ashish,

If you set z = 0 before you make the call to Add, does it work?

Also, in your COM code, you should not return S_FALSE. The S_FALSE
method indicates that the method actually succeeded. You want to return
E_POINTER.

Hope this helps.
 
A

Ashish

Hi Nicholas,
I tried z = 0, but in vein. I found that if i do new in the COM method, then
exception is gone (result of addition for some reason is always 0 :-s)but I
suspect there would be memory leak. E_POINTER does not fix this..
Thanks
Ashish

Nicholas Paldino said:
Ashish,

If you set z = 0 before you make the call to Add, does it work?

Also, in your COM code, you should not return S_FALSE. The S_FALSE
method indicates that the method actually succeeded. You want to return
E_POINTER.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ashish said:
Hi All,

I have a COM dll that has a method:
[id(3), helpstring("method Add")] HRESULT Add([in] DOUBLE nFirst, DOUBLE
nSecond, [out,retval] DOUBLE* nSum);

STDMETHODIMP CTest::Add(DOUBLE nFirst, DOUBLE nSecond, DOUBLE* nSum)
{
// TODO: Add your implementation code here
if (nSum = NULL)
{
return S_FALSE;
}

*nSum = nFirst + nSecond;
return S_OK;
}

From my C# client I use use it via a ref. Code looks like:

COM.ITest a = new COM.CTestClass();

Double x,y,z;

x = new Double();

y = new Double();

z = new Double();

x = 10;

y = 20;

z = 0;

z = a.Add(x,y);

Console.Out.WriteLine("{0}",z);

I am getting an error : Object reference not set to an instance of an
object.

Does anyone know why ?

Thanks

Ashish
 
A

Ashish

Yes it did work.. Thanks.. Can you tell me why the same thing doesn't work
for BSTR ?
i.e on performing *<BSTR_var> = L"Hello World"; still does not perform the
assignment. In my clinet I get an empty string.. Looks like one of those
cases where value is lost when vars are passed by value (but this is an
out,retval BSTR* :-s).
Thanks
Ashish

Ashish said:
Thanks... will give it a try and let u know...
Ashish said:
Hi All,

I have a COM dll that has a method:
[id(3), helpstring("method Add")] HRESULT Add([in] DOUBLE nFirst, DOUBLE
nSecond, [out,retval] DOUBLE* nSum);

STDMETHODIMP CTest::Add(DOUBLE nFirst, DOUBLE nSecond, DOUBLE* nSum)
{
// TODO: Add your implementation code here
if (nSum = NULL)
{
return S_FALSE;
}

*nSum = nFirst + nSecond;
return S_OK;
}

From my C# client I use use it via a ref. Code looks like:

COM.ITest a = new COM.CTestClass();

Double x,y,z;

x = new Double();

y = new Double();

z = new Double();

x = 10;

y = 20;

z = 0;

z = a.Add(x,y);

Console.Out.WriteLine("{0}",z);

I am getting an error : Object reference not set to an instance of an
object.

Does anyone know why ?

Thanks

Ashish
 
M

Mattias Sjögren

i.e on performing *<BSTR_var> = L"Hello World";

You should allocate BSTRs with the SysAllocString API (or use _bstr_t
os similar wrapper type). BSTRs need to be length prefixed.

*<BSTR_var> = SysAllocString(L"Hello World");



Mattias
 

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