COM concept help - order of functions or parameters
-
Hi guys, as I recently dig into the world of COM by reading Inside COM by Dale Rogerson, I encounter this question in the early chapters. In the book it says you should always make a new version of interface when - Order of functions in an interface changes - Order of pamaters in an function changes What exactly do this imply? The order as in the position order the fuctions or parameters listed out? For example:
interface IX : IUnknown { virtual void __stdcall Fx() = 0; virtual void __stdcall Fy() = 0; };
changes tointerface IX : IUnknown { virtual void __stdcall Fy() = 0; virtual void __stdcall Fx() = 0; };
if it is what the book means, when it says that a new version is required, is it because that the virtual function table are no longer the same for the above two interfaces, where the functions (or in other case parameters) would reside in different memory block addresses?:confused: Thanks~ -
Hi guys, as I recently dig into the world of COM by reading Inside COM by Dale Rogerson, I encounter this question in the early chapters. In the book it says you should always make a new version of interface when - Order of functions in an interface changes - Order of pamaters in an function changes What exactly do this imply? The order as in the position order the fuctions or parameters listed out? For example:
interface IX : IUnknown { virtual void __stdcall Fx() = 0; virtual void __stdcall Fy() = 0; };
changes tointerface IX : IUnknown { virtual void __stdcall Fy() = 0; virtual void __stdcall Fx() = 0; };
if it is what the book means, when it says that a new version is required, is it because that the virtual function table are no longer the same for the above two interfaces, where the functions (or in other case parameters) would reside in different memory block addresses?:confused: Thanks~Yes. If you change the order of the functions in the declaration, their positions in the interface will shift. When you derive from IUnknown, for example, you rely on the compiler putting in QueryInterface as func#0, AddRef as func#1, and Release as func#2. When you declare the first version, Fx is func#3, and Fy is func#4. By rewriting as the second version, the positions of Fx and Fy are swapped. This matters because COM is a binary standard, for cross-language use. If you have a client that calls methods on this interface, it is quite likely doing so by locating the function in a table. If you change the order, the table is invalid, and a call to Fx by the client would be a call to Fy instead. Replace the names 'Fx' with 'Save' and 'Fy' with 'Delete' and you'll see why this is important. You could get around this problem by recompiling all clients, but if you haven't got source for all of them, you wouldn't be able to do this, of course. Similarly, if you alter the order of parameters (or types, or add/remove parameters), again, this alters the shape of the interface, so you should use a new one. Of course, while you're developing the first version, it's quite conceivable that halfway through, you'll need to change the interface, and within reason, you're able to do that without generating a new UUID (or IID) for it, but as soon as you've published it (which in general terms means installed on other than development kit), the interface should be regarded as immutable. Steve S Developer for hire
-
Yes. If you change the order of the functions in the declaration, their positions in the interface will shift. When you derive from IUnknown, for example, you rely on the compiler putting in QueryInterface as func#0, AddRef as func#1, and Release as func#2. When you declare the first version, Fx is func#3, and Fy is func#4. By rewriting as the second version, the positions of Fx and Fy are swapped. This matters because COM is a binary standard, for cross-language use. If you have a client that calls methods on this interface, it is quite likely doing so by locating the function in a table. If you change the order, the table is invalid, and a call to Fx by the client would be a call to Fy instead. Replace the names 'Fx' with 'Save' and 'Fy' with 'Delete' and you'll see why this is important. You could get around this problem by recompiling all clients, but if you haven't got source for all of them, you wouldn't be able to do this, of course. Similarly, if you alter the order of parameters (or types, or add/remove parameters), again, this alters the shape of the interface, so you should use a new one. Of course, while you're developing the first version, it's quite conceivable that halfway through, you'll need to change the interface, and within reason, you're able to do that without generating a new UUID (or IID) for it, but as soon as you've published it (which in general terms means installed on other than development kit), the interface should be regarded as immutable. Steve S Developer for hire