.NETGURU
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 Guru (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 Guru (http://www.dot-net-guru.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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification