c# - Reordering methods in ComImport interfaces throws COMException (0x80041001) -


consider following code com interop internet shortcuts:

[comimport] [interfacetype(cominterfacetype.interfaceisiunknown)] [guid("cabb0da0-da57-11cf-9974-0020afd79762")] public interface iuniformresourcelocatorw {     void seturl([marshalas(unmanagedtype.lpwstr)] string pcszurl, int dwinflags);     void geturl([marshalas(unmanagedtype.lpwstr)] out stringbuilder ppszurl);     void invokecommand(intptr purlici); }  [comimport] [guid("fbf23b40-e3f0-101b-8488-00aa003e56f8")] public class internetshortcut { }  

the following works expected:

var ishort = new internetshortcut(); ((ipersistfile)ishort).load("mylink.url", 0); ((iuniformresourcelocatorw)ishort).geturl(out url); 

however:

  • if comment out iuniformresourcelocatorw.seturl (which not using), iuniformresourcelocatorw.geturl throws comexception (hresult 0x80041001).
  • if switch between iuniformresourcelocatorw.seturl , iuniformresourcelocatorw.geturl (that place former below latter) same exception thrown
  • if comment out iuniformresourcelocatorw.invokecommand code runs fine.

it's if order has preserved "up to" invoked method. behavior design? documented somewhere? i'm asking because com interfaces composed of many methods possibly many supporting types , i'd rather avoid defining don't need if possible.

order very, very, very important. interface pointer in com pointer table of addresses of functions. first 3 function pointers iunknown method implements (addref, release, queryinterface). 4th address of server's seturl() method, 5th geturl(), etc.

by removing seturl() declaration, clr thinks should call 4th function in table when program calls geturl(). doesn't call geturl() @ all, ends calling seturl() instead. gets wrong arguments, pcszurl empty string , dwinflags contains random junk. kaboom! when seturl fails on kind of wmi call garbage values.

you cannot remove methods interface. can use placeholder, donotcall().

otherwise strong lesson in kind of dll hell can modifying interfaces.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -