AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

COM Helper
Goto page Previous  1, 2, 3, 4, 5 ... 9, 10, 11  Next
 
This topic is locked: you cannot edit posts or make replies.    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Sun May 20, 2007 10:50 pm    Post subject: Reply with quote

tfcahm wrote:
The iTunes Create Folder method expects a Unicode string as input instead of a pointer to the string. Though it looks right to me, this code does not work.

Ansi2Unicode() is not enough in iTunes case as the COM server lives in another process.
Try this instead:

Code:
#NoEnv
#Include CoHelper.ahk
CoInitialize()
CLSID_iTunesApp := "{DC0C2640-1415-4644-875C-6F4D769839BA}"
 IID_IiTunes    := "{9DD6680B-3EDC-40DB-A771-E6FE4832E34A}"
piT := CreateObject(CLSID_iTunesApp, IID_IiTunes)

sFldr := "MyNewFldr"
pFldr := SysAllocString(sFldr)
DllCall(VTable(piT, 84), "Uint", piT, "Uint", pFldr, "UintP", pPlist)  ; Create Folder
SysFreeString(pFldr)

Release(piT)
CoUninitialize()
Back to top
View user's profile Send private message
tfcahm



Joined: 20 May 2007
Posts: 48

PostPosted: Mon May 21, 2007 2:36 am    Post subject: Reply with quote

That's perfect! Thanks Sean for your help now and all you've done developing the COM Helper.
Back to top
View user's profile Send private message
tfcahm



Joined: 20 May 2007
Posts: 48

PostPosted: Fri May 25, 2007 12:46 am    Post subject: Reply with quote

Hi Sean,

Need your help again. I'm working with a program that expects parameters to be passed by reference so that they can be changed in the method call. This allows the program's methods to return multiple values. The problem is that I can put parameters in but cannot figure out how to get them back out.

This is the function code as shown by TypeLib Browser.
Code:
' ****************************************************************************************
' GenerateHTML method
' Interface name = IDataServer
' VTable offset = 40 [&H28]
' DispID = 203 [&H000000CB]
' ****************************************************************************************
FUNCTION BookCATIDataServer_GenerateHTML ALIAS "BookCATIDataServer_GenerateHTML" ( _
    BYVAL pthis AS DWORD PTR _                   ' %VT_DISPATCH <dispinterface>
  , BYVAL XSLTemplate AS STRING _                ' %VT_BSTR <DYNAMIC UNICODE STRING> [in]
  , BYVAL Filter AS STRING _                     ' %VT_BSTR <DYNAMIC UNICODE STRING> [in]
  , BYVAL StripLinks AS INTEGER _                ' %VT_BOOL <INTEGER> [in]
  , BYREF prmFilename AS STRING _                ' *%VT_BSTR <DYNAMIC UNICODE STRING> [in]
  , BYREF RecordsetGUID AS STRING _              ' *%VT_BSTR <DYNAMIC UNICODE STRING> [in]
  , BYREF Result AS LONG _                       ' *%VT_I4 <LONG> [out]
    ) EXPORT AS LONG                             ' %VT_HRESULT <LONG>

    LOCAL HRESULT AS LONG
    XSLTemplate = UCODE$(XSLTemplate)
    Filter = UCODE$(Filter)
    IF pthis = 0 THEN FUNCTION = %E_POINTER : EXIT FUNCTION
    CALL DWORD @@pthis[10] USING BookCATIDataServer_GenerateHTML(pthis, XSLTemplate, Filter, StripLinks, prmFilename, RecordsetGUID, Result) TO HRESULT
    FUNCTION = HRESULT
    prmFilename = ACODE$(prmFilename)
    RecordsetGUID = ACODE$(RecordsetGUID)

END FUNCTION

And this is the AHK code I'm trying to get to function. The server application correctly sees prmFilename or RecordsetGUID when they are valid strings. If either prmFilename or RecordsetGUID are empty strings the server should write back a value. I know the server is working properly because accessing it with a Perl based application works perfectly. In short, this AHK code works except for reading back the values written by the server application. Thanks for any help you can give me to figure out how to do this with AHK and CoHelper.

Code:
#NoEnv
#Include CoHelper.ahk

CoInitialize()

; CreateObject( "BookCAT_DataServer" )
GUID4String(CLSID_DataServer, "{19BA681F-C11A-4885-9C70-775DC7D8A6F7}")
GUID4String( IID_IDataServer, "{49FA5CA3-2B7E-49FF-AD87-EFE7953636DD}")

pthis := CreateObject(CLSID_DataServer, IID_IDataServer)

sXSLTemplate   := "BookList.xsl"
sFilter        := "Author=John Grisham"
bStriplinks    := 0
srmFilename    :=     
sRecordsetGuid :=

pXSLTemplate := SysAllocString(sXSLTemplate)
pFilter      := SysAllocString(sFilter)

prmFilename := SysAllocString(srmFilename)
VarSetCapacity(bstr1, 8 * 2, 0)
EncodeInteger(&bstr1 + 0, prmFilename)

pRecordsetGuid := SysAllocString(sRecordsetGuid)
VarSetCapacity(bstr2, 8 * 2, 0)
EncodeInteger(&bstr2 + 0, pRecordsetGuid)

If DllCall(VTable(pthis, 10)       ; GenerateHTML
  , "Uint", pthis                  ; In: connection
  , "UInt", pXSLTemplate           ; In: template
  , "UInt", pFilter                ; In: filter
  , "Int",  bStripLinks            ; In: bool Striplinks
  , "UInt", &bstr1                 ; In/Out Filename
  , "Uint", &bstr2                 ; In/Out Guid
  , "Int64P", pResult)             ; Out: Result
   MsgBox % "Connecting the database FAILED!", Exit

pbstr1 := DecodeInteger(&bstr1 + 0)
Unicode2Ansi(pbstr1, srmFilename)
SysFreeString(pbstr1)

pbstr2 := DecodeInteger(&bstr2 + 0)
Unicode2Ansi(pbstr2, sRecordsetGuid)
SysFreeString(pbstr2)

SysFreeString(pXSLTemplate)
SysFreeString(pFilter)
SysFreeString(prmFilename)
SysFreeString(pRecordsetGuid)

msgbox, % "GenerateHtml`n srmFilename: " . srmFilename . "`n sRecordsetGuid: " . sRecordsetGuid
Release(prs)
CoUnInitialize()
Exitapp 
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Fri May 25, 2007 1:23 am    Post subject: Reply with quote

tfcahm wrote:
And this is the AHK code I'm trying to get to function.

Try this, not tested though as I don't have it in my machine.

Code:
#Include CoHelper.ahk

CoInitialize()

GUID4String(CLSID_DataServer, "{19BA681F-C11A-4885-9C70-775DC7D8A6F7}")
GUID4String( IID_IDataServer, "{49FA5CA3-2B7E-49FF-AD87-EFE7953636DD}")
pds := CreateObject(CLSID_DataServer, IID_IDataServer)

sXSLTemplate := "BookList.xsl"
sFilter      := "Author=John Grisham"
sFilename    := ""
sRecordset   := ""
bStripLinks  := False

pXSLTemplate := SysAllocString(sXSLTemplate)
pFilter      := SysAllocString(sFilter)
pFilename    := SysAllocString(sFilename)
pRecordset   := SysAllocString(sRecordset)

MsgBox, % DllCall(VTable(pds, 10), "Uint", pds, "Uint", pXSLTemplate, "Uint", pFilter, "short", -bStripLinks, "UintP", pFilename, "UintP", pRecordset, "intP", nResult, "Uint") . "|" . ErrorLevel . "|" . A_LastError . "|" . nResult

Unicode2Ansi(pFilename, sFilename)
Unicode2Ansi(pRecordset, sRecordset)

SysFreeString(pXSLTemplate)
SysFreeString(pFilter)
SysFreeString(pFilename)
SysFreeString(pRecordset)

Release(pds)
CoUninitialize()

MsgBox, % "GenerateHtml`n Filename: " . sFilename . "`n Recordset: " . sRecordset


Last edited by Sean on Sat May 26, 2007 3:19 am; edited 8 times in total
Back to top
View user's profile Send private message
tfcahm



Joined: 20 May 2007
Posts: 48

PostPosted: Fri May 25, 2007 1:49 am    Post subject: Reply with quote

Thanks for the fast response. Unfortunately, no joy. The GenerateHTML call fails with the connection error. I'll try to figure out which parameter is causing the problem.

I don't know if you want to go that far, but you can get an functional eval copy of the program here. With a default install, the code I posted will generate an HTML file in the system temp directory.

I'll post back when after finding which parameter causes the error.
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Fri May 25, 2007 1:59 am    Post subject: Reply with quote

tfcahm wrote:
The GenerateHTML call fails with the connection error. I'll try to figure out which parameter is causing the problem.

Sorry, it was my fault.
I noticed I added "!" in If by accident, so I removed "!" immediately, but looks like you copied it before.
Please recopy it and try again.
Back to top
View user's profile Send private message
tfcahm



Joined: 20 May 2007
Posts: 48

PostPosted: Fri May 25, 2007 5:42 am    Post subject: Reply with quote

Removing the "!" did make the call work, and the HTML file was saved to the system temp directory. However, the result returned for both variables is "0".
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Fri May 25, 2007 7:51 am    Post subject: Reply with quote

tfcahm wrote:
However, the result returned for both variables is "0".

I'm not a psychic, at least you have to tell me what was ErrorLevel and A_LastError.

Anyway, there was one thing I thought was strange.
I neglected it as a glitch, but seems not, judging from the result and what you had said in your previous post.

I updated the script I posted. Hope to work now.
Back to top
View user's profile Send private message
tfcahm



Joined: 20 May 2007
Posts: 48

PostPosted: Fri May 25, 2007 2:21 pm    Post subject: Reply with quote

Thanks for hanging in there with me on this. The msgboxes output from this script now are:

0|0|1008|1

475292 vs 475292 | 475476 vs 475476

GenerateHtml
Filename:
RecordsetGuid:

Sorry, I don't know where to lookup the A_LastError code. For what it is worth, this is the Perl code to do the same thing. The LastError here is 0, and the two variables are modified.
Code:
use Win32::OLE;
use Win32::OLE::Variant;
$pds = Win32::OLE->new('BookCAT.DataServer', 'Quit');

$sFilter="Author=John Grisham";
$sXSLTemplate= "BookList.xsl";
$sFilename = Variant(VT_BSTR|VT_BYREF, "");
$sRecordsetGuid = Variant(VT_BSTR|VT_BYREF, "");

$pds->GenerateHTML($sXSLTemplate, $sFilter, "FALSE", $sFilename, $sRecordsetGuid);
print Win32::OLE->LastError() . " | $sFilename | $sRecordsetGuid";

Thanks again for your help.
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Fri May 25, 2007 3:55 pm    Post subject: Reply with quote

tfcahm wrote:
For what it is worth, this is the Perl code to do the same thing.

This code doesn't accord with the one with tlb, IMO.
Although I don't know about Perl, looks like the Perl code is correct judging from the LastError, and moreover, that you said this code is working.

I updated the script again accordingly, but there still remains one unclear point to me.
Anyway, I chose the safest way except the one unclear to me.
Please inform me about the result.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Fri May 25, 2007 4:26 pm    Post subject: Reply with quote

Sean, you should fix CoHelpher so all functions return value

For instance CoInitialise can fail...
_________________
Back to top
View user's profile Send private message
tfcahm



Joined: 20 May 2007
Posts: 48

PostPosted: Fri May 25, 2007 4:36 pm    Post subject: Reply with quote

Here are the results:

-1073741819|0|0|0

GenerateHtml
Filename: 0
RecordsetGuid: 0

Yes, the Perl code has been used for several months and does work. I don't use Delphi here, but I'm told it works in Delphi too. If it would help I can post the Delphi code.
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Fri May 25, 2007 4:46 pm    Post subject: Reply with quote

majkinetor wrote:
For instance CoInitialise can fail...

The error code of CoInitialize is meaningless, at least from the pracitical point of view. Nothing prevents using COM after this call.
They only mean: it's already initialized or initialized as multi-thread Apartment.

Some COM/OLE feature requires specifically single-thread apartment, however, CoHelper.ahk doesn't provide a way to initialize as MTA as AHK is single-threaded anyway.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Fri May 25, 2007 4:53 pm    Post subject: Reply with quote

Its not good calling the OS function and getting "" as result anyway. U usualy look at MSDN for explanations so U may get confused when U get potato.
_________________
Back to top
View user's profile Send private message
tfcahm



Joined: 20 May 2007
Posts: 48

PostPosted: Fri May 25, 2007 4:56 pm    Post subject: Reply with quote

Sorry, I posted too quickly and should have pointed out the last code does not resullt in an HTML file being created (nResult=0).
Back to top
View user's profile Send private message
Display posts from previous:   
This topic is locked: you cannot edit posts or make replies.    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5 ... 9, 10, 11  Next
Page 4 of 11

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group