Search:
Namespaces
Discussions
.NET v1.1
Feedback
Can not return a com object from within c#
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.framework.interop
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Umed Kurdistan via .NET 247 (VIP)
OPC (OLE for Process Control) is a protocol as an interface between applications and industry hardware drivers. It defines two main components (COM) registered in opcserver category : OpcServer & OpcGroup.
I have written two coresponing classes as follows that implement the required Interfaces as defined by OPC.
-----------------------
[Guid( "926EFA52-C020-4259-B879-203512901EAF" )]
[ProgId("FAHM OPC Server 1")]
[ComVisible( true )]
public class OpcServer: IOPCServer, IOPCCommon, IOPCItemProperties, IOPCBrowseServerAddressSpace
{
//Implementd inteface methods
}
[Guid( "075A7104-076F-481f-8CCF-11BA9AE375EB" )]
[ProgId("FAHM OPC Group 1")]
[ClassInterface(
ClassInterfaceType
.None)]
public class OpcGroup: IOPCItemMgt, IOPCAsyncIO2, IOPCSyncIO, IOPCGroupStateMgt
{
//Implementd inteface methods
}
I can in a not managed programming language such as delphi or c++ create an instance of them and call all methods of both of them and QueryInterface them to all implemented Interfaces and see they are working by showing just a messagebox for test.
The problem is that the IOPCServer Interface defines a method AddGroup as follows that returns an instance of OPCGroup and has following signature in c++ and c#:
c++:
virtual HRESULT STDMETHODCALLTYPE AddGroup(
/* [string][in] */ LPCWSTR szName,
/* [in] */ BOOL bActive,
/* [in] */ DWORD dwRequestedUpdateRate,
/* [in] */ OPCHANDLE hClientGroup,
/* [in][unique] */ LONG __RPC_FAR *pTimeBias,
/* [in][unique] */ FLOAT __RPC_FAR *pPercentDeadband,
/* [in] */ DWORD dwLCID,
/* [out] */ OPCHANDLE __RPC_FAR *phServerGroup,
/* [out] */ DWORD __RPC_FAR *pRevisedUpdateRate,
/* [in] */ REFIID riid,
/* [iid_is][out] */ LPUNKNOWN __RPC_FAR *ppUnk) = 0;
c# :
void AddGroup(
[In, MarshalAs(
UnmanagedType
.LPWStr)] string szName,
[In, MarshalAs(
UnmanagedType
.Bool)] bool bActive,
[In] int dwRequestedUpdateRate,
[In] int hClientGroup,
[In] ref int pTimeBias,
[In] ref float pPercentDeadband,
[In] int dwLCID,
[Out] out int phServerGroup,
[Out] out int pRevisedUpdateRate,
[In] ref Guid riid,
[Out, MarshalAs(
UnmanagedType
.IUnknown)] out object ppUnk);
When in the unmanged code call this method of IOPCServer, it seems to be ok, but the returned object in ppUnk that supposed to be a IOPCItemMgt fails to execute any of its methods and raises an memory exception.
I tried following implementation for OPCSerevr.AddGroup but all of them have the same wrong result:
------------------------------------------
impl. 1:
Type
typeofGroup =
Type
.GetTypeFromProgID( "FAHM OPC Group 1" );
if( typeofGroup == null )
throw new
Exception
("OPC Server Not Found");
object grp =
Activator
.CreateInstance( typeofGroup );
IntPtr
pRes =
IntPtr
.Zero;
IntPtr
pUnk =
Marshal
.GetIUnknownForObject(obj);
Marshal
.QueryInterface(pUnk, ref riid, out pRes);
Marshal
.Release(pUnk);
Marshal
.Release(pUnk);
if (!pRes.Equals(
IntPtr
.Zero))
ppUnk = pRes;
else
Marshal
.ThrowExceptionForHR(HResult.E_NOINTERFACE);
------------------------------------------
impl. 2:
object grp = new OpcGroup();
ppUnk =
IntPtr
.Zero;
ppUnk =
Marshal
.GetComInterfaceForObject(grp, typeof(IOPCItemMgt));
------------------------------------------
impl. 3:
object grp = new OpcGroup();
ppUnk = grp;
------------------------------------------
impl. 4:
object grp = new OpcGroup();
ppUnk = (IOPCItemMgt)grp;
And for each of mentioned lines in above implementaions I surrounded it with a try-catch block and showed messageboxes after each successfull or failed block, and it seems that none of them fails, but I don't know why the returned IOPCItemMgt is incorrect.
Can anyone help me?
Special Regards
Umed Kurdistan
--------------------------------
From: Umed Kurdistan
-----------------------
Posted by a user from .NET 247 (
http://www.dotnet247.com/
)
<Id>1Z8SEFKaLUqsPcQgEDjeeA==</Id>
Reply to this message...
Robert Jordan
Hi Umed,
[Original message clipped]
I encountered the same problem some time ago. The solution
was to rename the whole inteferface and made the certain method
return the specific interface:
void AddGroup(
...
[Out] out IOPCItemMgt ppUnk
);
I don't have the sources of the COM-client that was consuming
my C#-COM-object, thus I was not able (or willing) to debug
the problem, but it appears to be a client's problem.
bye
Rob
Reply to this message...
Robert Jordan
Robert Jordan wrote:
[Original message clipped]
I took a look at the revision history of my code:
I was testing 2 variants. Both were working. The another one was:
void AddGroup(
...
[Out] out
IntPtr
ppUnk
);
implementation:
void AddGroup(....)
{
object grp = new OpcGroup();
ppUnk =
Marshal
.GetComInterfaceForObject(grp, typeof(IOPCItemMgt));
}
bye
Rob
Reply to this message...
System.Activator
System.Exception
System.IntPtr
System.Runtime.InteropServices.ClassInterfaceType
System.Runtime.InteropServices.Marshal
System.Runtime.InteropServices.UnmanagedType
System.Type
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com