Page 1 of 1

BStr with PureDispHelper

Posted: Tue Mar 18, 2008 11:51 am
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

Posted: Tue Mar 18, 2008 1:20 pm
by ts-soft
Use the unicode type, should work

Re: BStr with PureDispHelper

Posted: Tue Mar 18, 2008 1:58 pm
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:

Posted: Tue Mar 18, 2008 2:31 pm
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,@...)

Posted: Tue Mar 18, 2008 3:30 pm
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

Posted: Tue Mar 18, 2008 3:45 pm
by Demivec
Post removed. (I decided it was redundant)

Posted: Tue Mar 18, 2008 3:52 pm
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

Posted: Tue Mar 18, 2008 3:59 pm
by Fluid Byte
Removed. :o (here we go again ...)

Posted: Tue Mar 18, 2008 5:50 pm
by Stefan Schnell
Thanks to all for your help, it works very fine.
Cheers
Stefan