Sunday, 15 July 2012

how i can create a delegate for an Office Com Proxy method in C#? -



how i can create a delegate for an Office Com Proxy method in C#? -

the question related next thread: access com vtable c#

what have calling method com object trough method pointer c#

i know method pointer not necessary in special case is.

why need method pointer?

i want accessing setplaceholdertext method word.content command via latebinding. latebind features in .net->com utilize invoke method idispatch interface, implemented com object. invoke method in word.contentcontrol class has bug! works bind beacuse interop runtime avoid invoke , utilize method pointers. (thats want handle problem). impossible utilize method vb latebinding, c# dynamics or gettype().invoke...

i utilize code linked posting template:

class="lang-cs prettyprint-override">[unmanagedfunctionpointer(callingconvention.stdcall)] public delegate void setplaceholdertextcallback( [marshalas(28)] [in] object buildingblock = null, [marshalas(28)] [in] object range = null, [marshalas(19)] [in] string text = ""); object contentcontrol = getcontentcontrolprox(); intptr comptr = marshal.getcominterfaceforobject(contentcontrol, typeof(contentcontrol)); intptr vtable = marshal.readintptr(comptr); int start = marshal.getstartcomslot(typeof(contentcontrol)); int end = marshal.getendcomslot(typeof(contentcontrol)); setplaceholdertextcallback invoker = null; commembertype mtype = membertypes.method; (int = start; < end; i++) { system.reflection.memberinfo mi = marshal.getmethodinfoforcomslot(typeof(contentcontrol), i, ref mtype); if (mi.name == "setplaceholdertext") { intptr methodpointer = marshal.readintptr(vtable, * marshal.sizeof(typeof(intptr))); invoker = marshal.getdelegateforfunctionpointer(methodpointer, typeof(setplaceholdertextcallback)) setplaceholdertextcallback; break; } } invoker(type.missing, type.missing, "helloworld");

the invoke fails accessviolationexception. have no thought @ moment....

seems problem resolved. blog entry jochen manns give me answer. first parameter must intptr class intance.

<!-- language: lang-cs --> intptr adresspointer = marshal.getcominterfaceforobject(control, typeof(word.contentcontrol)); invoker(adresspointer, null, null, "helloworld"); <!-- language: lang-cs -->

works !!!

c# com interop

No comments:

Post a Comment