Click here to Skip to main content
15,893,814 members
Home / Discussions / C#
   

C#

 
GeneralDataGrid Cell Color - C# Pin
Jo Develper3-Sep-04 2:15
Jo Develper3-Sep-04 2:15 
GeneralRe: DataGrid Cell Color - C# Pin
sreejith ss nair3-Sep-04 2:37
sreejith ss nair3-Sep-04 2:37 
GeneralRe: DataGrid Cell Color - C# Pin
Jo Develper3-Sep-04 5:17
Jo Develper3-Sep-04 5:17 
GeneralRe: DataGrid Cell Color - C# Pin
Not Active3-Sep-04 5:43
mentorNot Active3-Sep-04 5:43 
GeneralRe: DataGrid Cell Color - C# Pin
Jo Develper5-Sep-04 23:16
Jo Develper5-Sep-04 23:16 
Questioncan not return com from c# Pin
umedkurd3-Sep-04 2:11
umedkurd3-Sep-04 2:11 
AnswerRe: can not return com from c# Pin
Heath Stewart3-Sep-04 7:14
protectorHeath Stewart3-Sep-04 7:14 
GeneralRe: can not return com from c# Pin
umedkurd3-Sep-04 7:51
umedkurd3-Sep-04 7:51 
Thanks for your reply.

Both OPCServer & OPCGroups are defined in c# but they are regitered as COM because the OPC protocol defines so and others can be able to create instances of them.

I removed one/both Marshal.Release but it doesn't solve the problem. The problem in readable & formatted style is:

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 corresponding 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);

    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


GeneralRe: can not return com from c# Pin
Heath Stewart3-Sep-04 8:15
protectorHeath Stewart3-Sep-04 8:15 
GeneralRe: can not return com from c# Pin
umedkurd3-Sep-04 18:34
umedkurd3-Sep-04 18:34 
GeneralRe: can not return com from c# Pin
Heath Stewart6-Sep-04 9:10
protectorHeath Stewart6-Sep-04 9:10 
GeneralRe: can not return com from c# Pin
umedkurd7-Sep-04 18:42
umedkurd7-Sep-04 18:42 
GeneralMultiple Document Intrface problem-Ineed help Pin
gijoebijoe3-Sep-04 2:05
gijoebijoe3-Sep-04 2:05 
GeneralRe: Multiple Document Intrface problem-Ineed help Pin
sreejith ss nair3-Sep-04 2:52
sreejith ss nair3-Sep-04 2:52 
GeneralExocortex.DSP question Pin
Shiro19813-Sep-04 1:56
Shiro19813-Sep-04 1:56 
QuestionIt allways prints to my default printer but i want to choose the printer in code? Pin
robmays3-Sep-04 1:55
robmays3-Sep-04 1:55 
GeneralCreating a XSLT File Pin
teo_x3-Sep-04 1:53
teo_x3-Sep-04 1:53 
GeneralRe: Creating a XSLT File Pin
sreejith ss nair3-Sep-04 2:55
sreejith ss nair3-Sep-04 2:55 
QuestionHow to invert pixel color of a bitmap ? Pin
sachinkalse3-Sep-04 0:24
sachinkalse3-Sep-04 0:24 
AnswerRe: How to invert pixel color of a bitmap ? Pin
sreejith ss nair3-Sep-04 2:44
sreejith ss nair3-Sep-04 2:44 
GeneralRe: How to invert pixel color of a bitmap ? Pin
sachinkalse5-Sep-04 22:17
sachinkalse5-Sep-04 22:17 
GeneralPluggin' in the Plug-In - HMOG Pin
Sebastian Schneider3-Sep-04 0:20
Sebastian Schneider3-Sep-04 0:20 
GeneralCant change the Expandable property from property grid Pin
pradeep_pc3-Sep-04 0:15
pradeep_pc3-Sep-04 0:15 
GeneralRe: Cant change the Expandable property from property grid Pin
sreejith ss nair3-Sep-04 2:40
sreejith ss nair3-Sep-04 2:40 
GeneralDisable write in comboBox C# Pin
adnanh752-Sep-04 22:35
adnanh752-Sep-04 22:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.