Shared Library Tutorial lazarus / 21-General ---------------------------------------------------------------------- Aruna · 2024-11-19 10:48 Hi everyone, I put together a learn-by-doing type of tutorial and was not sure where this may fit in the wiki (if at all). Should I add it as a external link or create a completely new page on the wiki? The tutorial is here: Would appreciate some guidance. Thank you. ---------------------------------------------------------------------- cdbc · 2024-11-19 11:18 Hi I was expecting something ...more. When I read the title 'Mastering...'  Regards Benny ---------------------------------------------------------------------- Aruna · 2024-11-19 11:39 Quote from: cdbc on November 19, 2024, 12:18:53 pmHi I was expecting something ...more. When I read the title 'Mastering...'  Regards Benny Hi Benny, Thanks for your feedback! It sounds like the title set a high bar, and I’m sorry if the content didn’t fully meet your expectations. Could you please share a bit more about what you were hoping to see? That would help me improve and better align with readers' needs in the future. Looking forward to hearing your thoughts! Best regards, Aruna ---------------------------------------------------------------------- gidesa · 2024-11-19 12:04 Hello, good idea. I note that: - the tutorial is limited to Lazarus on Linux; - it's about directly writing a C library, and using that; no informations on using external C libraries, where you have only the headers, not the source. - it is not about writing shared libraries just in Lazarus/Fpc. So the title should be more informative. Expecially if you want to add to wiki. ---------------------------------------------------------------------- Aruna · 2024-11-19 12:16 Quote from: gidesa on November 19, 2024, 01:04:16 pmHello, good idea. I note that: - the tutorial is limited to Lazarus on Linux; - it's about directly writing a C library, and using that; no informations on using external C libraries, where you have only the headers, not the source. - it is not about writing shared libraries just in Lazarus/Fpc. So the title should be more informative. Expecially if you want to add to wiki. Hello @gidesa Thank you for your thoughtful feedback! You’ve raised some excellent points. It’s clear the title and content could be refined for better clarity and coverage. I have removed the word 'Mastering' from the title. Here’s what I’m thinking:     Expanding the tutorial to include examples of using external C libraries with only headers available.    Covering how to write shared libraries entirely in Lazarus/FPC.    Updating the title to accurately reflect the scope, especially for inclusion in a wiki. Your suggestions are invaluable for improving the tutorial—thank you for taking the time to share them! If you have any additional ideas or specific use cases you’d like to see covered, I’d love to hear them. What would 'you' like to see for a title? Best regards, Aruna ---------------------------------------------------------------------- ackarwow · 2024-11-19 12:20 @Aruna Maybe word "Introduction" could be better for the title... ? ---------------------------------------------------------------------- TRon · 2024-11-19 12:28 note that there is also some information on libraries available in the programmer's manual (https://www.freepascal.org/docs- html/prog/progch12.html#progse55.html). That  doesn't necessarily imply the effort from Aruna is in vain (if even for his/her own understanding). ---------------------------------------------------------------------- Aruna · 2024-11-19 12:31 Quote from: ackarwow on November 19, 2024, 01:20:34 pm@Aruna Maybe word "Introduction" could be better for the title... ? Thank you @ackarwow I have changed the title. ---------------------------------------------------------------------- Aruna · 2024-11-19 12:34 Quote from: TRon on November 19, 2024, 01:28:32 pmnote that there is also some information on libraries available in the programmer's manual (https://www.freepascal.org/docs- html/prog/progch12.html#progse55.html). I had no idea.. thanks @TRon. Quote from: TRon on November 19, 2024, 01:28:32 pmThat  doesn't necessarily imply the effort from Aruna is in vain (if even for his/her own understanding). Soo.. keep moving forward with the tutorial or drop it like a hot potato? It was initially for my own (his/male) understanding but later I thought why not share what I found? ---------------------------------------------------------------------- TRon · 2024-11-19 12:56 Quote from: Aruna on November 19, 2024, 01:34:24 pmSoo.. keep moving forward with the tutorial or drop it like a hot potato? I find such a question always difficult to answer. Reason is that as long as you learn(ed) something from it then it has meaning (if even for the writing of the tutorial itself, no matter the subject). It is always difficult to add something to a topic that has been addressed extensively, f.e. see wiki. There is so much details in those wiki pages that it is difficult to compete with that. And having said that imho it still lacks some (practical) approaches. f.i. a small overview, like you did, might come in handy to be used as a quick reference (so that you do not need to go through all these articles in case someone only needs a small refresh on the topic) QuoteIt was initially for my own (his/male) understanding but later I thought why not share what I found? Sorry for the pronouns: Not meant to offend. I consider it good character that in case you've learned something that you would like to share what you've just learned. So, I can only applaud that. Just give it a good thought yourself so that you would be able to decide yourself if there is anything in your own tutorial that adds to that what already exists. In case there is something seriously missing (either in the manual or wiki) then consider contributing. As an example there a few people that have written a complete tutorial/manual on how Free Pascal works and is (can be) used. Is it redundant ? for sure it is. But they do target another audience so there is an actual use (also inspired by the many requests that are made over the years). ---------------------------------------------------------------------- gidesa · 2024-11-19 13:30 Quote from: Aruna on November 19, 2024, 01:16:11 pm Thank you for your thoughtful feedback! You’ve raised some excellent points. It’s clear the title and content could be refined for better clarity and coverage. I have removed the word 'Mastering' from the title. Here’s what I’m thinking:     Expanding the tutorial to include examples of using external C libraries with only headers available.    Covering how to write shared libraries entirely in Lazarus/FPC.    Updating the title to accurately reflect the scope, especially for inclusion in a wiki. Your suggestions are invaluable for improving the tutorial—thank you for taking the time to share them! If you have any additional ideas or specific use cases you’d like to see covered, I’d love to hear them. What would 'you' like to see for a title? Hello, glad that my opinion is useful. In a tutorial I would: - add a (very) short introduction about what are shared libraries and their advantages/disadvantages. Maybe two lines with a link to a wikipedia article, or similar? - begin with an entirely Pascal solution: write the library in FPC, and then write a FPC program that uses it. Because why Pascal programmers as first thing would write C library if they can write Pascal library?? - add the case of your library written in C, and client program written in FPC; your tutorial just addresses this part - add the broader case of external C library, describing how to extract Pascal function signatures from C headers, using the nice tool h2pas (and maybe using modern AI tools??) As others noted, there are wiki informations on these subjects (indeed the manual section is not super-detailed. The wiki article IS super- detailed). Perhaps you can "add value" merging wiki informations in a more complete tutorial (I do not mean "cut and paste"! :-)) Or maybe the previous subjects could be a "check list" for you to learn by yourself. ---------------------------------------------------------------------- cdbc · 2024-11-19 15:17 Hi Have you given the loading of dyn-libs any more thoughts?!? I'm thinking how brilliantly 'Advanced Records', fit that bill... E.g.:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit libloader;{$mode ObjFPC}{$H+}{$modeswitch advancedrecords}interfaceuses Classes, SysUtils,dynlibs;type  {$i sharedata_types.inc}  { TSharedataLoader }   TSharedataLoader = record    const sdlName: string = 'libsharedbuf.so';    var      hLib: TLibHandle;      sdlInit: Tshdt_Init;      sdlFini: Tshdt_Fini;    function LoadLib(const aPath: string): boolean;    function Loaded: boolean;    procedure UnloadLib;  end; var  shdtLoader: TSharedataLoader; implementation { TSharedataLoader }function TSharedataLoader.LoadLib(const aPath: string): boolean;begin  if aPath = '' then exit(false);  hLib:= LoadLibrary(IncludeTrailingPathDelimiter(aPath)+sdlName);  Result:= (hLib <> NilHandle);  if Result then begin    pointer(sdlInit):= GetProcAddress(hLib,'shdt_Init');    pointer(sdlFini):= GetProcAddress(hLib,'shdt_Fini');    Result:= ((sdlInit <> nil) and (sdlFini <> nil));  end;  if not Result then UnloadLib;end; function TSharedataLoader.Loaded: boolean;begin  Result:= ((hLib <> NilHandle) and (sdlInit <> nil) and (sdlFini <> nil));end; procedure TSharedataLoader.UnloadLib;begin  if hLib <> NilHandle then begin    sdlInit:= nil;    sdlFini:= nil;    if UnloadLibrary(hLib) then hLib:= NilHandle;  end;end; end. the use of include files to share api with the application, e.g.:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$interfaces corba}  IShareData = interface['IShareData']    function get_Handle: ptruint;    function get_Name: shortstring;    procedure set_Name(aValue: shortstring);    { if data is available for your handle or 0-handle(anybody), returns datalength in bytes,      if no data are waiting for your handle or 0-handle, returns 0 (zero) }  function IsDataAvailable(aHandle: ptruint): ptrint;    { reads data into an allocated buffer of size designated by a call to 'IsDataAvailable',      returns the number of bytes read, or 0 if 'aData' = nil or 'aLen' < available count }    function ReadData(aHandle: ptruint;aData: pointer;aLen: ptrint): ptrint;    { writes data to the buffer, marked with the destination-handle or 0,    returns the number of bytes written or 0 on failure }    function WriteData(aHandle,aDest: ptruint;aData: pointer;aLen: ptrint): ptrint;    property Handle: ptruint read get_Handle;    property Name: shortstring read get_Name write set_Name;  end;{$interfaces com}///// function prototypes /////  Tshdt_Init = function(aHandle: ptruint;aName: shortstring): IShareData;  Tshdt_Fini = procedure(aHandle: ptruint);(*Library exports 2 funcs / procs: - function shdt_Init(aHandle: ptruint;aName: shortstring): IShareData;- procedure shdt_Fini(aHandle: ptruint); BOTH ARE MANDATORY!!!In return you get an interface to the shared data buffer within.*)...So you see, my expectations were somewhat higher, but I too like the idea of sharing what you've learnt with others  Regards Benny ---------------------------------------------------------------------- Roland57 · 2024-11-19 15:52 @Aruna Thanks for the tutorial. Please continue it, if you have time (and desire).  All successfully tested here (Linux Mageia). ---------------------------------------------------------------------- indydev · 2024-11-19 16:35 Here are some good example articles on the subject that, unfortunately, are VERY Borland/Embarcardero specific.  Much of the code and explanation rely on Windows, the specific C++ compiler, and COM only Interfaces.  When these were "originally" written, cross compiling may not even have been possible, so the specificity to Windows is understandable. There are asides for generalizing, but something like this for Free Pascal/Lazarus would be nice. http://rvelthuis.de/articles/articles-cobjs.html http://rvelthuis.de/articles/articles-cppobjs.html http://rvelthuis.de/programs/convertpack.html http://rvelthuis.de/articles/articles-convert.html#macros ---------------------------------------------------------------------- Tony Stone · 2024-11-19 20:59 Quote from: Aruna on November 19, 2024, 01:34:24 pmSoo.. keep moving forward with the tutorial or drop it like a hot potato? It was initially for my own (his/male) understanding but later I thought why not share what I found? I would say make all the tutorials/articles you want whether you are an expert or not is not a valid factor if you are providing useful information!  Just remember there is always other people out there on the same skill level as yourself so to have anyone willing to try and put out educational info for others is huge IMO.  Like others said, if it was useful for you it will probably be useful to others!  Putting together quality tutorials is time consuming and sort of a skill in itself so if you like making tutorials DO IT!  They will only get better with time. ---------------------------------------------------------------------- Aruna · 2024-11-20 03:23 Quote from: TRon on November 19, 2024, 01:56:54 pmQuote from: Aruna on November 19, 2024, 01:34:24 pmSoo.. keep moving forward with the tutorial or drop it like a hot potato? I find such a question always difficult to answer. Reason is that as long as you learn(ed) something from it then it has meaning (if even for the writing of the tutorial itself, no matter the subject). It is always difficult to add something to a topic that has been addressed extensively, f.e. see wiki. There is so much details in those wiki pages that it is difficult to compete with that. And having said that imho it still lacks some (practical) approaches. f.i. a small overview, like you did, might come in handy to be used as a quick reference (so that you do not need to go through all these articles in case someone only needs a small refresh on the topic)Alright I will give this more thought and do more research before proceeding. Thank you for all the advice. Quote from: TRon on November 19, 2024, 01:56:54 pmQuoteIt was initially for my own (his/male) understanding but later I thought why not share what I found? Sorry for the pronouns: Not meant to offend. No offence taken I just wanted to clarify my gender as others have also thought I was a female Quote from: TRon on November 19, 2024, 01:56:54 pmI consider it good character that in case you've learned something that you would like to share what you've just learned. So, I can only applaud that. Just give it a good thought yourself so that you would be able to decide yourself if there is anything in your own tutorial that adds to that what already exists. In case there is something seriously missing (either in the manual or wiki) then consider contributing. As an example there a few people that have written a complete tutorial/manual on how Free Pascal works and is (can be) used. Is it redundant ? for sure it is. But they do target another audience so there is an actual use (also inspired by the many requests that are made over the years). I will think this through more carefully and would you have a link to this complete tutorial/manual on how Free Pascal works please? ---------------------------------------------------------------------- Aruna · 2024-11-20 03:25 Quote from: gidesa on November 19, 2024, 02:30:56 pmQuote from: Aruna on November 19, 2024, 01:16:11 pm Thank you for your thoughtful feedback! You’ve raised some excellent points. It’s clear the title and content could be refined for better clarity and coverage. I have removed the word 'Mastering' from the title. Here’s what I’m thinking:     Expanding the tutorial to include examples of using external C libraries with only headers available.    Covering how to write shared libraries entirely in Lazarus/FPC.    Updating the title to accurately reflect the scope, especially for inclusion in a wiki. Your suggestions are invaluable for improving the tutorial—thank you for taking the time to share them! If you have any additional ideas or specific use cases you’d like to see covered, I’d love to hear them. What would 'you' like to see for a title? Hello, glad that my opinion is useful. In a tutorial I would: - add a (very) short introduction about what are shared libraries and their advantages/disadvantages. Maybe two lines with a link to a wikipedia article, or similar? - begin with an entirely Pascal solution: write the library in FPC, and then write a FPC program that uses it. Because why Pascal programmers as first thing would write C library if they can write Pascal library?? - add the case of your library written in C, and client program written in FPC; your tutorial just addresses this part - add the broader case of external C library, describing how to extract Pascal function signatures from C headers, using the nice tool h2pas (and maybe using modern AI tools??) As others noted, there are wiki informations on these subjects (indeed the manual section is not super-detailed. The wiki article IS super- detailed). Perhaps you can "add value" merging wiki informations in a more complete tutorial (I do not mean "cut and paste"! :-)) Or maybe the previous subjects could be a "check list" for you to learn by yourself. Hello @gidesa I think you've raised some excellent points to enhance the tutorial! Thank you so much for taking the time. I will try to slowly incorporate and put together tutorials on everything you have mentioned. ---------------------------------------------------------------------- Aruna · 2024-11-20 03:34 Quote from: cdbc on November 19, 2024, 04:17:35 pmHi Have you given the loading of dyn-libs any more thoughts?!? I'm thinking how brilliantly 'Advanced Records', fit that bill... Hello Benny, I am trying to read up about dyn-libs so yes. Quote from: cdbc on November 19, 2024, 04:17:35 pm...So you see, my expectations were somewhat higher, but I too like the idea of sharing what you've learnt with others  Regards BennyThat code was way over my current level of knowledge so I asked AI to explain and breakdown and heavily comment things for me. The result is here. I would be very grateful if you go through at your convenience and give me some feedback on if AI got things right? If correct, I suppose we could add this to the tutorial in time? Once I fully understand what it is you have done, right now it is voo-doo magic to me ---------------------------------------------------------------------- Aruna · 2024-11-20 03:37 Quote from: Roland57 on November 19, 2024, 04:52:24 pm@Aruna Thanks for the tutorial. Please continue it, if you have time (and desire).  All successfully tested here (Linux Mageia). Hi @Roland57, Thank you for your feedback! I'm glad to hear that everything worked well on Linux Mageia and that the tutorial has been useful. I'll definitely try to continue the tutorial, time and desire permitting. I appreciate your encouragement! If you have any specific areas you'd like to see covered in the continuation, feel free to let me know. Your input will help me shape it to be even more helpful for others as well! ---------------------------------------------------------------------- Aruna · 2024-11-20 03:38 Quote from: indydev on November 19, 2024, 05:35:55 pmHere are some good example articles on the subject that, unfortunately, are VERY Borland/Embarcardero specific.  Much of the code and explanation rely on Windows, the specific C++ compiler, and COM only Interfaces.  When these were "originally" written, cross compiling may not even have been possible, so the specificity to Windows is understandable. There are asides for generalizing, but something like this for Free Pascal/Lazarus would be nice. http://rvelthuis.de/articles/articles-cobjs.html http://rvelthuis.de/articles/articles-cppobjs.html http://rvelthuis.de/programs/convertpack.html http://rvelthuis.de/articles/articles-convert.html#macros Hello @indydev thank you for sharing the links. I will certainly try to port these over to Lazarus. ---------------------------------------------------------------------- Aruna · 2024-11-20 03:43 Quote from: Tony Stone on November 19, 2024, 09:59:15 pmQuote from: Aruna on November 19, 2024, 01:34:24 pmSoo.. keep moving forward with the tutorial or drop it like a hot potato? It was initially for my own (his/male) understanding but later I thought why not share what I found? I would say make all the tutorials/articles you want whether you are an expert or not is not a valid factor if you are providing useful information!  Just remember there is always other people out there on the same skill level as yourself so to have anyone willing to try and put out educational info for others is huge IMO.  Like others said, if it was useful for you it will probably be useful to others!  Putting together quality tutorials is time consuming and sort of a skill in itself so if you like making tutorials DO IT!  They will only get better with time. Thank you very much for the encouragement and the kind words. It's about providing practical information that others can learn from, and often, those who are not "experts" can bring a fresh perspective or insight. Plus, the willingness to share knowledge in any form I believe is a huge contribution to the community. As you continue, feedback from others will help improve and fine-tune your work. This is what 'I' feel. ---------------------------------------------------------------------- cdbc · 2024-11-20 07:46 Hi Aruna Nice that you like it  Regarding the writeup about it, it's very well done. The only thing, would be the definition of CORBA interfaces, which it gets right, but they're used in a slightly different manor in object pascal, we also call them RAW interfaces, which means they don't do 'Automatic disposal via reference counting' and as you can see with this one, is identified by a string and /not/ a Guid, even though, you *can* use a string-Guid, if you'd like a unique string representation. In short, these interfaces, *you* are responsible for the disposal / freeing. I could make the whole little project available to you, to play with, if you'd like that?!? Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-11-20 10:31 Well, Benny, It is broke so I wrote a report for CORBA: https://gitlab.com/freepascal.org/fpc/source/-/issues/41029 Surely you are interested ---------------------------------------------------------------------- cdbc · 2024-11-20 13:08 Hi Thaddy, you've got your opinion, that's fine, but you're wrong! Apart from the manual interfaces amo. used in generics.default, the rest of the interfaces will be implemented by some kind of TObject. TObject sports a procedure, I don't know if you know about it: It's called ...'Free'  All you have to do to free / destroy a CORBA interface, is add this one method to your CORBA interface 'procedure Free;' I can't believe you didn't know that. Why the h*** would you want to involve generics into interfaces base?!? Beats me... Personally I think your proposal is completely off the rails. Regards Benny ---------------------------------------------------------------------- cdbc · 2024-11-20 13:10 ...Thaddy, you could just wait to see the rest of the code, then you'll be wiser on how to do stuff with interfaces! ---------------------------------------------------------------------- Thaddy · 2024-11-20 13:32 I can figure out how to work with interfaces Benny. Both CORBA and COM. Didn't you see what happens? If you instantiate a class with a corba interface to an interface variable you have an irrecoverable memory leak..... That is the point. With corba this should not be allowed at all. There is no way to free the instance... BUG. (and I am even a bit of an expert) Check the bug report again and run the code with -glh... BTW where di you see any generics in the bug report code? There isn't any. It is simply a bug and the compiler should not allow it for corba as I well demonstrated. I can see you did not test! Shame on you. ---------------------------------------------------------------------- cdbc · 2024-11-20 13:45 Hi Thaddy I don't need to, I know how to write code with corba, so I can free it and get at the underlying class, with 1 method. You just stick to COM- programming and let the compiler do the tricky stuff for you  No need to f*** up years of code and programming, just for a silly idea of yours, that "CORBA is stupid" Tsk, tsk, tsk... Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-11-20 13:47 Quote from: cdbc on November 20, 2024, 02:45:49 pmHi Thaddy I don't need to, I know how to write code with corba, so I can free it and get at the underlying class Show me.... CORBA interfaces should be handled like I showed in the second part of the bug report code. You are very, very wrooong or to tired to test and read properly. Bit disappointed... ---------------------------------------------------------------------- cdbc · 2024-11-20 14:02 Hi QuoteCORBA interfaces should be handled like I showed in the second part of the bug report code.Complete and utter nonsense! With your proposal, you'll render COM-interfaces invalid too, but I see you haven't thought that far... again, tsk ,tsk, tsk. I actually hold your experience and knowledge in esteem, but on this, sorry, You've completely lost it, I hope you find /it/ again... Btw. here's the test code...(just copy the lib into app folder) regards Benny eta: Thaddy you're right, I mistook your variable name T for generics, it isn't. I do apologise. post edited. ---------------------------------------------------------------------- cdbc · 2024-11-20 14:07 Hi again From your /bug/ report:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  ICorba = interface  ['this_must_be_a_string']    procedure Free; // <--- ADD THIS AND YOU CAN FREE!!!    function MakeResult:string;  end; ...or you can be clever and instead 'Free', implement 'function Obj: TObject;' That way you also get access to the underlying object/class. Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-11-20 14:28 Yes, I know that, but it is still a big bug. What you propose is a work-around. The compiler should not accept it that a corba interface can be instantiated to an interface variable. It should only accept instantiating through a class variable: it is in essence an impossible construct. In 99% of the cases people do instantiate to a class var, it is about the 1% that don't. Free, nor obtaining the class instance from the interface instance are not part of a CORBA interface. So it should be forbidden by the compiler. ---------------------------------------------------------------------- cdbc · 2024-11-20 14:49 Hi Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CreateICorba(): ICorba;begin  TCorba.Create.GetInterface(ICorba,Result);end; Fought a lot of windmills lately?!? Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-11-20 15:06 I know that too, same answer. You can but should not be able to instantiate to a corba interface variable. Here you are instantiating to a class instance. This bug is specific to the case in the bug report. Hotel california, you can enter any time you like but you can never leave. Again, show me how to free my example? You can't. ---------------------------------------------------------------------- cdbc · 2024-11-20 15:18 Errrhhmmm Thaddy Why not /assume/ that the 1% know what they're doing?!? Afterall, the interfaces have been this way, for a long time. Regards Benny edit: Your specific example(without adding Obj or Free), you can only free through the class instance, not through the interface alone. Mind you, in this setting I'd use COM. ---------------------------------------------------------------------- Thaddy · 2024-11-20 15:19 Again, show me how to free my example? You can't. It is also not intended. It is a bug in the compiler. ---------------------------------------------------------------------- Thaddy · 2024-11-20 16:15 Added the official CORBA specs to the bug report. (Not that the core developers do not know these.., but you don't ) Friendly, thaddy Also: it is a bad idea to extend fpc behavior over what is defined as a standard. There are no exceptions. ---------------------------------------------------------------------- cdbc · 2024-11-20 16:32 Hi Yup, dry stuff those specs  I read a bit further and there are mentions of shutdown and release, respectively, dealing with both single and multithreaded shutdown ~ freeing of resources... So you mean, because the class is boxed as an interface, it shouldn't be possible to free it that way(through the interface), thus no need to instatiate the interface /alone/?!? Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-11-20 16:40 Well, table 7.10 explains it all. Confirmed? It is a rather nasty issue, but claiming CORBA compliance is just not right and points people in the wrong direction. Hence I am rewriting the wiki (not finished) to explain the difference between COM and CORBA. As I see it, it will break little code and that little code was stupid anyway. And filed the report that CORBA in fpc does NOT comply and showed why. That is the original report and the core devels will see it immediately. ---------------------------------------------------------------------- cdbc · 2024-11-20 16:57 Hi Hmmm, can't deny that table, a no is a no. Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-11-20 17:04 Tnx. We will still be friends? ---------------------------------------------------------------------- cdbc · 2024-11-20 17:29 Hi Thaddy Yup, sure we're still friends, just friendly bickering between peers  That's how stuff gets found out... Regards Benny ---------------------------------------------------------------------- Aruna · 2024-11-21 03:00 Quote from: cdbc on November 20, 2024, 08:46:01 amHi Aruna Nice that you like it  Regarding the writeup about it, it's very well done.Hi Benny, That was actually generated by chatGPT. I made some very minor changes is all Quote from: cdbc on November 20, 2024, 08:46:01 amThe only thing, would be the definition of CORBA interfaces, which it gets right, but they're used in a slightly different manor in object pascal, we also call them RAW interfaces, which means they don't do 'Automatic disposal via reference counting' and as you can see with this one, is identified by a string and /not/ a Guid, even though, you *can* use a string-Guid, if you'd like a unique string representation. In short, these interfaces, *you* are responsible for the disposal / freeing. I have to learn about CORBA I at this moment in time have no clue what it even stands for. Quote from: cdbc on November 20, 2024, 08:46:01 amI could make the whole little project available to you, to play with, if you'd like that?!? Regards Benny Yes please I would very much like that and thanks! ---------------------------------------------------------------------- cdbc · 2024-11-21 06:19 Hi Aruna QuoteYes please I would very much like that and thanks!I've attached the test project to post #28 in this thread  QuoteI have to learn about CORBA I at this moment in time have no clue what it even stands for.CORBA interfaces have a bit of misleading name, they're actually RAW interfaces, without the automatic memory- management, we also call them RAW interfaces, it's typekind is 'tkInterfaceRaw' and I'm told, they're very much akin to the interfaces found in Java. BEWARE: CORBA-Interfaces do NOT auto-free!!! For the time being, if you instantiate a class directly to a corba-interface, you have to include at least, a method 'procedure Free;' in the interface declaration, so that you may free it again & avoid memory-leaks. The normal way, is to create a class and then use the interface off of that class, so that you can free the class, when done. In my little test-project, the library code includes the use of both COM & CORBA and creating / freeing of the class behind the corba interface. Have fun playing with it... Btw, the host app is just a crude console- app. Thaddy's and my discussion, was about the 'not being able to free a CORBA interface', I see his point, but I'm just using interfaces so much, that it's second nature to me, to include a method 'function Obj: TObject;', in the interface declaration of a CORBA intf. I then use that method to get at the underlying class, in order to free it. HTH Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-11-21 07:37 QuoteBEWARE: CORBA-Interfaces do NOT auto-free!!! For the time being, if you instantiate a class directly to a corba-interface, you have to include at least, a method 'procedure Free;' in the interface declaration, so that you may free it again & avoid memory-leaks. The normal way, is to create a class and then use the interface off of that class, so that you can free the class, when done.This will be fixed so you can only instantiate through a class: then it is the only way. Instantiating to an interface is called "boxing",  meaning you can use the class and its interface fully interchangeably. CORBA interfaces do not suppport boxing, hence it is wrong to instantiate to an interface, even if Benny's solution to implement free works at the moment it will not work in the future. (unless it is fixed with a legacy option, which is unlikely, because it is a bug in the first place) I am reworking the documentation for interfaces, also with examples. CORBA always instantiates to a class (or in the case of e.g. a C++ class functions as an overlay, the latter is C++ compiler dependent and is not guaranteed to always work) COM interfaces can be instantiated in two ways: to an interface var, which means automatic memory management, but also to a class var, which means you have to free it manually. COM interfaces that do not implement Iunknown must also be free'd manually and instantiated to a class var. In the latter case the compiler will throw an error, because for "boxing" to work, queryinterface support is needed and _addref and _Release must be supported for automatic memory management. That means that COM is handled correct by the compiler. That error is the same error or similar as the compiler should throw for CORBA. You can not instantiate to an interface if IUnknown is not implemented, E.g.:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}type  { valid com interface but without IUnknown support, a raw COM interface }  Imyinterface = interface  ['{6D414016-D4FC-4ED1-9B52-5D3D56DA0BEF}']  end;  { COM class with RAW type interface, so do not use TinterfacedObject }  TSomeClass = class(Tobject,Imyinterface)  end; var a:IMyInterface; b:TSomeclass;begin{ this will neatly fail. Does not compile} a:=TSomeclass.create;{ and this works } b:=TSomeclass.create; end.This is a COM example but illustrates how the compiler should behave for CORBA too. The fun part start if you add {$interfaces corba}, then it will compile and cause the bug! ---------------------------------------------------------------------- cdbc · 2024-11-21 08:42 Hi @Thaddy: That's a good writeup, both here and in the bugtracker =^ Regards Benny ---------------------------------------------------------------------- korba812 · 2024-11-21 10:43 In FPC, every interface of type COM inherits from IUnknown. Attempting to compile your example fails: Code: [Select]project1.lpr(8,16) Error: No matching implementation for interface method "QueryInterface(constref TGuid;out ):System.LongInt; StdCall;" found project1.lpr(8,16) Error: No matching implementation for interface method "_AddRef:System.LongInt; StdCall;" found project1.lpr(8,16) Error: No matching implementation for interface method "_Release:System.LongInt; StdCall;" found ---------------------------------------------------------------------- cdbc · 2024-11-21 11:07 Hi Yup, as it should be... To compile that, you have to: 1) Descend the class from 'TInterfacedObject', to create a COM interface. 2) Descend the class from 'TNoRefcountObject' to essentially create a RAW-     interface, that *you* have to free!!! I.e.: NO refcount. 3) Implement the 3 methods yourself in a descendant of TObject... 4) Pst. last possibility is to manually build the interface like in generics.default Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-11-21 12:24 Quote from: korba812 on November 21, 2024, 11:43:04 amIn FPC, every interface of type COM inherits from IUnknown. Attempting to compile your example fails: If that is the case, time for another bug report:The COM specification allows interfaces other than derived from IUnknown, a.k.a. raw com interfaces. This may depend on the compiler version, I will check that. BTW example is intended to fail... did you really miss that? ---------------------------------------------------------------------- korba812 · 2024-11-21 12:51 Quote from: Thaddy on November 21, 2024, 01:24:21 pmIf that is the case, time for another bug report:The COM specification allows interfaces other than derived from IUnknown, a.k.a. raw com interfaces. Please provide a link to some documentation that confirms this. Quote from: Thaddy on November 21, 2024, 01:24:21 pmBTW example is intended to fail... did you really miss that? Yes, however, failing for a different reason than the one you pointed out. ---------------------------------------------------------------------- Thaddy · 2024-11-21 17:09 Of course. It is not common, but allowed. See https://learn.microsoft.com/en-us/windows/win32/com/iunknown-and- interface-inheritance and http://download.microsoft.com/download/6/b/e/6be6ffaa-6c65-48a7-a172- 103469a33a1a/COM1598B.ZIP But as explained, in that case the interfaces are RAW interfaces and you will loose all advantages that IUnKnown provides: - no boxing (value type to reference type and vice versa conversion: IS and AS both ways ) - no refcount - no automatic memory management Actually the rtl itself uses it in some places: Interfaces that are COM but not refcounted. Note the remark about advanced scenario's in the first link. It also requires that those interfaces can not be instantiated to an interface variable or that addref and release are overridden. It requires boxing and that is not available for raw interfaces. An example in the rtl is IFPObserver and IFPObserved which are COM interfaces, but not refcounted. (Although they simply override addref and release, It could be solved in the other way I described.) COM explicitly allows raw interfaces, but it is discouraged. Note the specs are a very difficult read if you are not a CS. It is absolutely not suited for average programmers, however good they are. ---------------------------------------------------------------------- korba812 · 2024-11-21 17:39 From the page you pointed out: QuoteAll predefined interfaces (and any custom interfaces you define) inherit their definitions from the important interface IUnknown Another page of this documentation: QuoteAll COM interfaces inherit from the IUnknown interface. The IUnknown interface contains the fundamental COM operations for polymorphism and instance lifetime management.https://learn.microsoft.com/en-us/windows/win32/com/com- technical-overview#the-iunknown-interface Quote from: Thaddy on November 21, 2024, 06:09:10 pmAn example in the rtl is IFPObserver and IFPObserved which are COM interfaces, but not refcounted.Actually, these are CORBA type interfaces https://gitlab.com/freepascal.org/fpc/source/- /blob/main/rtl/objpas/classes/classesh.inc?ref_type=heads#L172 ---------------------------------------------------------------------- Thaddy · 2024-11-21 17:48 That was not always the case but indeed they are now corba anyway, just because of what I described. ---------------------------------------------------------------------- PascalDragon · 2024-11-21 19:57 Quote from: Thaddy on November 21, 2024, 01:24:21 pmQuote from: korba812 on November 21, 2024, 11:43:04 amIn FPC, every interface of type COM inherits from IUnknown. Attempting to compile your example fails: If that is the case, time for another bug report:The COM specification allows interfaces other than derived from IUnknown, a.k.a. raw com interfaces. This may depend on the compiler version, I will check that. COM- aka Reference Counted interfaces always inherit from IUnknown. Period. End of discussion. It does not matter what the COM specification says, because COM-interfaces exist this way due to how Delphi implemented them. If one needs an interface that does not do that, then use CORBA- aka Raw interfaces. ---------------------------------------------------------------------- indydev · 2024-11-22 03:07 Quote from: Aruna on November 20, 2024, 04:38:31 amQuote from: indydev on November 19, 2024, 05:35:55 pmHere are some good example articles on the subject that, unfortunately, are VERY Borland/Embarcardero specific.  Much of the code and explanation rely on Windows, the specific C++ compiler, and COM only Interfaces.  When these were "originally" written, cross compiling may not even have been possible, so the specificity to Windows is understandable. There are asides for generalizing, but something like this for Free Pascal/Lazarus would be nice. http://rvelthuis.de/articles/articles-cobjs.html http://rvelthuis.de/articles/articles-cppobjs.html http://rvelthuis.de/programs/convertpack.html http://rvelthuis.de/articles/articles-convert.html#macros Hello @indydev thank you for sharing the links. I will certainly try to port these over to Lazarus. Those articles are good because they explain the difficulties involved. Unfortunately his solutions involve using Borland/Embarcadero C++, which has very specialized ways of getting around the problems, and is Windows only.  Kudos to you if you are able to rewrite these. He provides some links to other people's solutions that don't require a specific C++ compiler, but then I don't think you will be able to find a C++ compiler (outside of Windows) that supports COM interfaces -- but maybe I am wrong about that. So I suspect the only real option is to flatten to C, then build a Free Pascal Interface of those C functions that mirror the C++ objects. Right now there is a project to build c_libtorch  https://github.com/lighttransport/c-libtorch which, with a Free Pascal interface would give us access to the code behind PyTorch, opening doors to a lot of AI work. ---------------------------------------------------------------------- korba812 · 2024-11-24 00:43 Quote from: PascalDragon on November 21, 2024, 08:57:25 pmIf one needs an interface that does not do that, then use CORBA- aka Raw interfaces. But what if someone wanted to get an instance of an object from an CORBA interface variable, as in the case of a COM interface? Why is this not possible? ---------------------------------------------------------------------- cdbc · 2024-11-24 01:09 Hi In your (corba only) interface declaration add this method:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Obj: TObject; and the in the implementing class, write it like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TCorbaClass.Obj: TObjectbegin  Result:= Self;end;This way, you can always free your corba-intf like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ICorbaClass.Obj.Free;ICorbaClass:= nil; //<- avoid dangling refs...and for getting the implementing class you just:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lcc:= TCorbaClass(ICorbaClass.Obj);...and back again, TObject implements 'GetInterface'... HTH Regards Benny ---------------------------------------------------------------------- korba812 · 2024-11-24 01:19 Of course, however, I would like to know why I can't get an instance from a CORBA type interface variable. ---------------------------------------------------------------------- cdbc · 2024-11-24 01:45 Hi That's because CORBA interfaces don't utilize a GUID as *unique* identifier, but identifies itself with a string. AFAICR, anyway... If you need the exact tech/reasoning behind this, you would probably need to ask @PascalDragon, as he/she is our resident expert  Regards Benny ---------------------------------------------------------------------- cdbc · 2024-11-24 01:47 Hi ...Or you could fiddle with something like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i writeln(TObject(PtrInt(T) - 2 * SizeOf(Pointer)).ClassName);TObject(PtrInt(T) - 2 * SizeOf(Pointer)).Free; Regards Benny ---------------------------------------------------------------------- PascalDragon · 2024-11-24 15:19 Quote from: korba812 on November 24, 2024, 01:43:17 amQuote from: PascalDragon on November 21, 2024, 08:57:25 pmIf one needs an interface that does not do that, then use CORBA- aka Raw interfaces. But what if someone wanted to get an instance of an object from an CORBA interface variable, as in the case of a COM interface? Why is this not possible? Because for COM-interfaces there is the QueryInterface method which is "abused" to implement the conversion from interface instance to class instance by implementing a pseudo GUID that represents the class instance (this way the cast can also fail if you do this on an interface that does not provide the correct GUID). Raw interfaces don't have this method thus one simply can't implement it generally. Quote from: cdbc on November 24, 2024, 02:47:59 amHi ...Or you could fiddle with something like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i writeln(TObject(PtrInt(T) - 2 * SizeOf(Pointer)).ClassName);TObject(PtrInt(T) - 2 * SizeOf(Pointer)).Free;  This is wrong, because the offset depends on the number of interfaces the class implements and the position in the list of interfaces of the class in question. E.g. if the class implements two interfaces and you do this for the first interface in the inheritance list, your code will crash. And even if the compiler can determine the offset for you it would not be possible to do this conversion in a safe way when you don't know what kind of class instance had originially been converted to the interface. ---------------------------------------------------------------------- cdbc · 2024-11-24 17:49 Hi Thank you @PascalDragon, I knew we needed you on the scene. Very nice and thorough explanation  Regards Benny