COM and C#.net Interoperability
-
Hi all, i am trying to expose C# object to COM and in this process i got a query and not sure how to progress further. so here i am seeking for help. i want to define a interface in C# in such a way the consumer of the COM object should be able to use the method in the following way. comObject.methodName(123) the above statement will return a value, assume where 123 is the parameter which could id or some thing like that comObject.methodName(123) = 100 the above statement should set the value 100 something internal member having id 123 in both the above statement the methodName is the same. so the question is how can we define an interface in C# that can be exposed to COM using regasm.exe. i know it is possible to define such a way in C++ using ATL but not sure how can i achieve the same in C#. [Comvisible(true)] interface ExposeToCom { // don't know what all to add to make the method appear to COM to be get and set int methodName(int); } Thanks for your help,
-
Hi all, i am trying to expose C# object to COM and in this process i got a query and not sure how to progress further. so here i am seeking for help. i want to define a interface in C# in such a way the consumer of the COM object should be able to use the method in the following way. comObject.methodName(123) the above statement will return a value, assume where 123 is the parameter which could id or some thing like that comObject.methodName(123) = 100 the above statement should set the value 100 something internal member having id 123 in both the above statement the methodName is the same. so the question is how can we define an interface in C# that can be exposed to COM using regasm.exe. i know it is possible to define such a way in C++ using ATL but not sure how can i achieve the same in C#. [Comvisible(true)] interface ExposeToCom { // don't know what all to add to make the method appear to COM to be get and set int methodName(int); } Thanks for your help,
-
Hi all, i am trying to expose C# object to COM and in this process i got a query and not sure how to progress further. so here i am seeking for help. i want to define a interface in C# in such a way the consumer of the COM object should be able to use the method in the following way. comObject.methodName(123) the above statement will return a value, assume where 123 is the parameter which could id or some thing like that comObject.methodName(123) = 100 the above statement should set the value 100 something internal member having id 123 in both the above statement the methodName is the same. so the question is how can we define an interface in C# that can be exposed to COM using regasm.exe. i know it is possible to define such a way in C++ using ATL but not sure how can i achieve the same in C#. [Comvisible(true)] interface ExposeToCom { // don't know what all to add to make the method appear to COM to be get and set int methodName(int); } Thanks for your help,
You are use a
method
as if it was aproperty
. You cannot assign a value to a function! Rather think ofcomObject.methodName(123, 100)
, i.e. a function with 2 parameters (key
andvalue
). -
You are use a
method
as if it was aproperty
. You cannot assign a value to a function! Rather think ofcomObject.methodName(123, 100)
, i.e. a function with 2 parameters (key
andvalue
).Thanks for your response. Actually, i am trying re-write a COM component developed in ATL which is production. i want to give the same interface so that consumer has minimal impact. code snippet from my existence code.
STDMETHOD(get\_methodA)(/\*\[in\]\*/ VARIANT storeID, /\*\[out, retval\]\*/ VARIANT \*pVal); STDMETHOD(put\_methodA)(/\*\[in\]\*/ VARIANT storeID, /\*\[in\]\*/ VARIANT newVal); STDMETHOD(get\_methodB)(/\*\[in\]\*/ VARIANT storeID, /\*\[in\]\*/ VARIANT index, /\*\[out, retval\]\*/ VARIANT \*pVal); STDMETHOD(put\_methodB)(/\*\[in\]\*/ VARIANT storeID, /\*\[in\]\*/ VARIANT index, /\*\[in\]\*/ VARIANT newVal);
when i extract the typelib for this
[id(0x00000001), propget, helpstring("property methodA")]
HRESULT methodA([in] VARIANT storeID, [out, retval] VARIANT* pVal);\[id(0x00000001), propput, helpstring("property methodA")\] HRESULT methodA(\[in\] VARIANT storeID, \[in\] VARIANT pVal); \[id(0x00000004), propget, helpstring("property methodB")\] HRESULT methodB(\[in\] VARIANT storeID, \[in\] VARIANT index, \[out, retval\] VARIANT\* pVal); \[id(0x00000004), propput, helpstring("property methodB")\] HRESULT methodB(\[in\] VARIANT storeID, \[in\] VARIANT index, \[in\] VARIANT pVal);
so the consumer can use as methodA(123) -- return a value methodA(123) = 100 -- this will set a value methodB(123, 3) -- return a value methodB(123, 3) = 100 -- this will set a value. i am trying to mimic the same methods defined in my ATL in a C# interface and use .net com interoperability using a regasm. i am not able to find out how achieve this in C#. if this is done in ATL i was thinking there could be a way to achieve this. thanks for your time
-
Thanks for your response. Actually, i am trying re-write a COM component developed in ATL which is production. i want to give the same interface so that consumer has minimal impact. code snippet from my existence code.
STDMETHOD(get\_methodA)(/\*\[in\]\*/ VARIANT storeID, /\*\[out, retval\]\*/ VARIANT \*pVal); STDMETHOD(put\_methodA)(/\*\[in\]\*/ VARIANT storeID, /\*\[in\]\*/ VARIANT newVal); STDMETHOD(get\_methodB)(/\*\[in\]\*/ VARIANT storeID, /\*\[in\]\*/ VARIANT index, /\*\[out, retval\]\*/ VARIANT \*pVal); STDMETHOD(put\_methodB)(/\*\[in\]\*/ VARIANT storeID, /\*\[in\]\*/ VARIANT index, /\*\[in\]\*/ VARIANT newVal);
when i extract the typelib for this
[id(0x00000001), propget, helpstring("property methodA")]
HRESULT methodA([in] VARIANT storeID, [out, retval] VARIANT* pVal);\[id(0x00000001), propput, helpstring("property methodA")\] HRESULT methodA(\[in\] VARIANT storeID, \[in\] VARIANT pVal); \[id(0x00000004), propget, helpstring("property methodB")\] HRESULT methodB(\[in\] VARIANT storeID, \[in\] VARIANT index, \[out, retval\] VARIANT\* pVal); \[id(0x00000004), propput, helpstring("property methodB")\] HRESULT methodB(\[in\] VARIANT storeID, \[in\] VARIANT index, \[in\] VARIANT pVal);
so the consumer can use as methodA(123) -- return a value methodA(123) = 100 -- this will set a value methodB(123, 3) -- return a value methodB(123, 3) = 100 -- this will set a value. i am trying to mimic the same methods defined in my ATL in a C# interface and use .net com interoperability using a regasm. i am not able to find out how achieve this in C#. if this is done in ATL i was thinking there could be a way to achieve this. thanks for your time
If you have a type library, you can convert it into a .NET assembly using tlbimp.
This space for rent
-
Hi all, i am trying to expose C# object to COM and in this process i got a query and not sure how to progress further. so here i am seeking for help. i want to define a interface in C# in such a way the consumer of the COM object should be able to use the method in the following way. comObject.methodName(123) the above statement will return a value, assume where 123 is the parameter which could id or some thing like that comObject.methodName(123) = 100 the above statement should set the value 100 something internal member having id 123 in both the above statement the methodName is the same. so the question is how can we define an interface in C# that can be exposed to COM using regasm.exe. i know it is possible to define such a way in C++ using ATL but not sure how can i achieve the same in C#. [Comvisible(true)] interface ExposeToCom { // don't know what all to add to make the method appear to COM to be get and set int methodName(int); } Thanks for your help,
Not sure, but I believe you have to turn on this feature in the AssemblyInfo page. Then, you have to put it on the Type you want to make visible.
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach