BStr with PureDispHelper

Just starting out? Need help? Post your questions and find answers here.
User avatar
Stefan Schnell
User
User
Posts: 85
Joined: Wed May 07, 2003 2:53 pm
Location: Germany - Oberirsen
Contact:

BStr with PureDispHelper

Post by Stefan Schnell »

Hello community,
I want to use the library PureDispHelper and one of the functions need a BStr as argument. Does anybody knows how I can define a BStr variable and give them to a PureDispHelper function?
In the examples I found nothing.
Thanks for tips.
Cheers
Stefan
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Use the unicode type, should work
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: BStr with PureDispHelper

Post by Demivec »

Stefan Schnell wrote:Hello community,
I want to use the library PureDispHelper and one of the functions need a BStr as argument. Does anybody knows how I can define a BStr variable and give them to a PureDispHelper function?
In the examples I found nothing.
Thanks for tips.
Cheers
Stefan
I have not used PureDispHelper but here is a link to some ideas that may help:
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post by Seldon »

Talking about PureDispHelper, I'm trying to put Date values in some cells of an Excel document... the argument in the function is %D ( Date ) and the parameter must be passed ByRef . Which type of variable should I use with PureBasic ? I tried a string, but it doesn't work. Any hints ? Thanks. :)

Code: Select all

dhPutValue(COMObject,"Cells(%d, %d).Value = %D",1,1,@...)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post by Shardik »

Stefan Schnell wrote: Does anybody knows how I can define a BStr variable and give them to a PureDispHelper function?
freak has published the following code to convert a PureBASIC string into BSTR format on page 11 of his excellent COM tutorial (http://freak.purearea.net/help/COMTutorial.pdf)

Code: Select all

Procedure.l MakeBSTR(String.s)
  Protected Result = 0
  CompilerIf #PB_Compiler_Unicode
    Result = SysAllocString_(@String)
  CompilerElse
    Protected *Buffer = AllocateMemory(Len(String)*2 + 2)
    If *Buffer
      PokeS(*Buffer, String, -1, #PB_Unicode)
      Result = SysAllocString_(*Buffer)
      FreeMemory(*Buffer)
    EndIf
  CompilerEndIf

  ProcedureReturn Result
EndProcedure
With the above procedure you should be able to call your function with

Code: Select all

If dhCallMethod(*YourObject, YourFunction(%B), MakeBSTR(YourString)) = #S_OK
  ...
End If
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Post removed. (I decided it was redundant)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post by Shardik »

Seldon wrote:Talking about PureDispHelper, I'm trying to put Date values in some cells of an Excel document... the argument in the function is %D ( Date ) and the parameter must be passed ByRef . Which type of variable should I use with PureBasic ? I tried a string, but it doesn't work. Any hints ? Thanks. :)
Take a look into mk-soft's and ts-soft's VariantHelper_Include.pb. They have declared lots of macros to help in conversions. This one converts a PB date into a double value which you can insert into Excel:

Code: Select all

Procedure.d T_DATE(pbDate) ; Result Date from PB-Date
 
  Protected date.d
 
  date = pbDate / 86400.0 + 25569.0
  ProcedureReturn date
 
EndProcedure
This little code example (modified from Kiffi's Excel example) does the job:

Code: Select all

EnableExplicit

Define.D CurrentDate
Define.L ExcelApp
Define.L Workbook

CurrentDate = Date() / 86400.0 + 25569.0

dhToggleExceptions(#True)

ExcelApp = dhCreateObject("Excel.Application")

If ExcelApp
  dhPutValue(ExcelApp, ".Visible = %b", #True)
  dhGetValue("%o", @Workbook, ExcelApp, ".Workbooks.Add")
  dhPutValue(ExcelApp, "Cells(%d, %d).Value = %D", 1, 1, @CurrentDate)

  MessageRequester("Date Insertion into Excel", "Click OK to close Excel")

  dhCallMethod(ExcelApp, ".Quit")
  dhReleaseObject(Workbook) : Workbook = 0
  dhReleaseObject(ExcelApp) : ExcelApp = 0
Else
  MessageRequester("PureDispHelper-ExcelDemo", "Couldn't create Excel-Object")
EndIf
Last edited by Shardik on Tue Mar 18, 2008 6:28 pm, edited 2 times in total.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Removed. :o (here we go again ...)
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
Stefan Schnell
User
User
Posts: 85
Joined: Wed May 07, 2003 2:53 pm
Location: Germany - Oberirsen
Contact:

Post by Stefan Schnell »

Thanks to all for your help, it works very fine.
Cheers
Stefan
Post Reply