Thomas, Thanks for your help... Worked like a charm... I am not sure why your IE was calling translate from IURLSearchHook instead of TranslateWithSearchContext on IURLSearchHook2 on your machine, but we made the suggested changes to the generated file to a POINTER(c_wchar) and we started getting calls. We had to make a few changes to your suggestion for clearing the buffer and writing it back out because the slice operations weren't working right:
def TranslateWithSearchContext(self, this, lpwszSearchURL, cchBufferSize, pSearchContext): # lpwszSearchURL is a POINTER(c_wchar) instance. rawdata = lpwszSearchURL[:cchBufferSize] # rawdata is now a unicode string of length cchBufferSize, filled with NUL characters # at the end. The [in] unicode string can probably be get by this: url = rawdata.split('\0')[0] # Maybe ctypes.wstring_at(lpwszSearchURL) would also work. # The [out] string must be written into the lpwszSearchURL buffer, maybe this works: for i in range(cchBufferSize): lpwszSearchURL[i] = '\0'# clear it urlout = searchgenerator.GenSearchUrl(url, {'f':'web', 'sbs':'1', 'from':'ie'}) for i in range(len(urlout)): if i >= cchBufferSize: break lpwszSearchURL[i] = urlout[i] return S_OK Does codegenerator.py need to be changed to look for 'out' parameters for strings and change them to POINTER(c_wchar) and POINTER(c_char)? In the meantime we can keep the generated .py file in another place and use it explicity correct? Thanks for all your help, Mike -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Thomas Heller Sent: Wednesday, July 30, 2008 3:49 PM To: comtypes-users@lists.sourceforge.net Subject: Re: [comtypes-users] in,out parameters in comtypes 0.5 (trunk) MR Michael Robellard (5314) schrieb: > Hello, > > I am trying to implement the IURLSearchHook2 interface in comtypes and > I am running into some issues. > > which generate the following in the comtypes.gen .py files: > class IURLSearchHook2(IURLSearchHook): > _case_insensitive_ = True > _iid_ = GUID('{5EE44DA4-6D32-46E3-86BC-07540DEDD0E0}') > _idlflags_ = [] > IURLSearchHook2._methods_ = [ > COMMETHOD([], HRESULT, 'TranslateWithSearchContext', > ( ['in', 'out'], WSTRING, 'lpwszSearchURL' ), > ( ['in'], c_ulong, 'cchBufferSize' ), > ( ['in'], POINTER(ISearchContext), 'pSearchContext' )), > ] > I can get the data into the method and print it out fine. > The problem is when I try and return a string out. It just plain hangs. > If anyone has any thoughts. Is this even possible? > I have tried both the "high level" and "low level" approach as > discussed in Thomas' recent post Mike, here are my thoughts on your problem. The high level comtypes method implementation can only work for automation-compatible argument types, and WSTRING is not one of those. WSTRING is defined as c_wchar_p in the generated file. c_wchar_p is interpreted as a zero-terminated wide character string, but what COM really passes is a buffer with space for 'cchBufferSize' wide characters. Which contains a zero-terminated string that you are supposed to change. comtypes assumes c_wchar_p as 'const' strings which you cannot/should not change. I think you should change the 'TranslateWithSearchContext' method definition to something like this: > IURLSearchHook2._methods_ = [ > COMMETHOD([], HRESULT, 'TranslateWithSearchContext', > ( ['in', 'out'], POINTER(c_wchar), 'lpwszSearchURL' ), > ( ['in'], c_ulong, 'cchBufferSize' ), > ( ['in'], POINTER(ISearchContext), 'pSearchContext' )), > ] and write the (low-level) implementation like so: def TranslateWithSearchContext(self, this, lpwszSearchURL, cchBufferSize, pSearchContext): # lpwszSearchURL is a POINTER(c_wchar) instance. rawdata = lpwszSearchURL[:cchBufferSize] # rawdata is now a unicode string of length cchBufferSize, filled with NUL characters # at the end. The [in] unicode string can probably be get by this: url = rawdata.split('\0')[0] # Maybe ctypes.wstring_at(lpwszSearchURL) would also work. # The [out] string must be written into the lpwszSearchURL buffer, maybe this works: lpwszSearchURL[:cchBufferSize] = '\0' * cchBufferSize # clear it urlout = "http://web.somewhere.whatdoIknow" lpwszSearchURL[:len(urlout)] = urlout return S_OK Thomas ------------------------------------------------------------------------ - This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ comtypes-users mailing list comtypes-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/comtypes-users ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ comtypes-users mailing list comtypes-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/comtypes-users