Recent

Author Topic: [SOLVED]String and OLE (com)  (Read 4203 times)

Demi

  • New member
  • *
  • Posts: 7
[SOLVED]String and OLE (com)
« on: July 05, 2017, 09:19:39 am »
Hey.
I use ole (com) to control Adobe Indesign.

Code: Pascal  [Select][+][-]
  1. var
  2.    IDApp,IDDoc,IDText,IDPage:Variant;
  3.    i,j:integer;
  4.    st:String ;
  5. begin
  6. IDApp:=CreateOleObject('Indesign.Application');
  7. IDDoc:= IDApp.Documents.Add;
  8. IDPage:= IDDoc.pages.add;
  9. IDText:=IDPage.textFrames.add();
  10. IDText.geometricBounds:=VarArrayOf(['0','10','10','60']);
  11. IDText.contents :='Test';  //             Ok, the test is displayed on the page
  12.  

Code: Pascal  [Select][+][-]
  1. var
  2.    IDApp,IDDoc,IDText,IDPage:Variant;
  3.    i,j:integer;
  4.    st:String ;
  5. begin
  6. st:='test';
  7. IDApp:=CreateOleObject('Indesign.Application');
  8. IDDoc:= IDApp.Documents.Add;
  9. IDPage:= IDDoc.pages.add;
  10. IDText:=IDPage.textFrames.add();
  11. IDText.geometricBounds:=VarArrayOf(['0','10','10','60']);
  12. IDText.contents :=st;  //             Nothing is displayed on the sheet
  13.  

AnsiToUtf8 and Utf8toAnsi It does not help

The question is, in what format is the line in LAZARUS by default, and how to print the string?

« Last Edit: July 05, 2017, 09:22:26 am by Demi »

Demi

  • New member
  • *
  • Posts: 7
Re: String and OLE (com)
« Reply #1 on: July 05, 2017, 09:21:53 am »
sorry.

Mission complite!

Code: Pascal  [Select][+][-]
  1. IDText.contents :=WideString(st);

Thaddy

  • Hero Member
  • *****
  • Posts: 10703
Re: [SOLVED]String and OLE (com)
« Reply #2 on: July 05, 2017, 10:00:46 am »
Although widestring works, use UnicodeString instead. Or use {$mode DelphiUnicode}
It is also better to declare the strings as UnicodeString (or Widestring) in the first place.
The cast has side effects: copy
« Last Edit: July 05, 2017, 10:02:37 am by Thaddy »

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: [SOLVED]String and OLE (com)
« Reply #3 on: July 05, 2017, 11:26:01 am »
Although widestring works, use UnicodeString instead. Or use {$mode DelphiUnicode}
It is also better to declare the strings as UnicodeString (or Widestring) in the first place.
The cast has side effects: copy
I wouldn't use unicodestring olestrings are not reference counted and copy is what you want when passing them to external source. Use widestring only to avoid headaches.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 10703
Re: [SOLVED]String and OLE (com)
« Reply #4 on: July 05, 2017, 11:29:45 am »
Although widestring works, use UnicodeString instead. Or use {$mode DelphiUnicode}
It is also better to declare the strings as UnicodeString (or Widestring) in the first place.
The cast has side effects: copy
I wouldn't use unicodestring olestrings are not reference counted and copy is what you want when passing them to external source. Use widestring only to avoid headaches.
Ole strings ARE reference counted, just not by FPC but by the COM marshaler on Windows. To be cross platform, use unicodestring. There's no headache. Copy is NOT what you want, silly.

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: [SOLVED]String and OLE (com)
« Reply #5 on: July 05, 2017, 11:33:47 am »
Although widestring works, use UnicodeString instead. Or use {$mode DelphiUnicode}
It is also better to declare the strings as UnicodeString (or Widestring) in the first place.
The cast has side effects: copy
I wouldn't use unicodestring olestrings are not reference counted and copy is what you want when passing them to external source. Use widestring only to avoid headaches.
Ole strings ARE reference counted, just not by FPC but by the COM marshaler on Windows. To be cross platform, use unicodestring. There's no headache. Copy is NOT what you want, silly.
Sorry I do not see it.
https://msdn.microsoft.com/en-us/library/ms221069.aspx
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 10703
Re: [SOLVED]String and OLE (com)
« Reply #6 on: July 05, 2017, 12:57:38 pm »
You missed, from your own link,  "BSTRs are allocated using COM memory allocation functions, so they can be returned from methods without concern for memory allocation."?
Now what does that mean......................... >:D O:-) COM memory is kind of reference counted by the COM marshaler (trust me on that one, otherwise you need to read at least "Essential COM" by Don Box, I won't torture you with the real stuff - yes, there is worse to come if you insist...)
Just use unicodestring.
« Last Edit: July 05, 2017, 01:10:02 pm by Thaddy »

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: [SOLVED]String and OLE (com)
« Reply #7 on: July 05, 2017, 01:15:25 pm »
You missed "BSTRs are allocated using COM memory allocation functions, so they can be returned from methods without concern for memory allocation."?
Now what does that mean......................... >:D O:-) COM memory is kind of reference counted by the COM marshaler (trust me on that one, otherwise you need to read at least "Essential COM" by Don Box, I won't torture you with the real stuff)
I think you are confusing common memory manager with reference counting. In the first case calling sysfreestring will release the string immediately and every reference count you might have kept is out the door at that point, in the second case the struct it self has .... well I'm pretty sure you know how it works. I'm also sure that you already know all that and I'm thinking you are pulling my leg for some reason. In any case I think I'll carefully pull back in to the shadows and watch from there how things unfold.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 10703
Re: [SOLVED]String and OLE (com)
« Reply #8 on: July 05, 2017, 01:28:47 pm »
You missed "BSTRs are allocated using COM memory allocation functions, so they can be returned from methods without concern for memory allocation."?
Now what does that mean......................... >:D O:-) COM memory is kind of reference counted by the COM marshaler (trust me on that one, otherwise you need to read at least "Essential COM" by Don Box, I won't torture you with the real stuff)
I think you are confusing common memory manager with reference counting. In the first case calling sysfreestring will release the string immediately and every reference count you might have kept is out the door at that point, in the second case the struct it self has .... well I'm pretty sure you know how it works. I'm also sure that you already know all that and I'm thinking you are pulling my leg for some reason. In any case I think I'll carefully pull back in to the shadows and watch from there how things unfold.

Sigh. When you use COM you WILL use COM memory management. Now that's enough. It has NOTHING to do with FPC's memory management it has to do with the BSTR type.
Now READ THAT BOOK. I demand it! Maybe we can get a more informed answer later...< this is not fun, extremely grumpy about such idiocy  >:D >:D >:D >:D >:D >:D>

You are not a beginner so stop behaving like one.
« Last Edit: July 05, 2017, 01:30:45 pm by Thaddy »

ASerge

  • Hero Member
  • *****
  • Posts: 1736
Re: [SOLVED]String and OLE (com)
« Reply #9 on: July 05, 2017, 08:14:28 pm »
I agree with @taazz. WideString is the BSTR - native COM string type. And the use of UnicodeString in this case leads to extra copying. UnicodeString is better to use when NOT required to interact with COM, when their RefCount will be useful.

 

TinyPortal © 2005-2018