By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,338 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,338 IT Pros & Developers. It's quick & easy.

COM interop problem.

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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]
- ni**************@exisconsulting.com

"Ashish" <ab*@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 13 '05 #2

P: n/a
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 [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:#1**************@TK2MSFTNGP12.phx.gbl...
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]
- ni**************@exisconsulting.com

"Ashish" <ab*@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Nov 13 '05 #3

P: n/a
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" <sa********@yahoo.com> wrote in message
news:#b**************@TK2MSFTNGP12.phx.gbl...
Thanks... will give it a try and let u know...
"Ashish" <ab*@hotmail.com> wrote in message
news:#r**************@TK2MSFTNGP12.phx.gbl...
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


Nov 13 '05 #4

P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.