import comtypes
import ctypes
import comtypes.server.automation
from comtypes.hresult import *

from comtypes.client import GetModule
GetModule("searchhook.tlb")

from comtypes.gen import AGCoreLib
import logging
logging.basicConfig(level=logging.DEBUG,
                    filename=r"c:\error.log")
logger = logging.getLogger("main")

# ../comtypes/gen/AGCoreLib.py
# ../comtypes/gen/_90C026EB_0B99_491A_BD5B_0EB86B533608_0_1_0.py

class LILWSearchHook(AGCoreLib.AGSearchHook):
    
    _reg_threading_ = "Both"
    _reg_progid_ = "AGCoreLib.AGSearchHook.1"
    _reg_novers_progid_ = "AGCoreLib.AGSearchHook"
    _reg_desc_ = ""
    _reg_clsctx_ = comtypes.CLSCTX_INPROC_SERVER | comtypes.CLSCTX_LOCAL_SERVER

#    def TranslateWithSearchContext(self, lpwszSearchURL, cchBufferSize, pSearchContext):
#        logger.info('Got a search request %s' % lpwszSearchURL)
#        logger.info(type(lpwszSearchURL))
#        logger.info(dir(lpwszSearchURL))
#        
#        #return searchgenerator.GenSearchUrl(lpwszSearchURL)
#        return "http://cnn.com"

    def TranslateWithSearchContext(self, this, lpwszSearchURL, cchBufferSize, pSearchContext):
        logger.info('Got a search request %s' % lpwszSearchURL)
        logger.info(type(lpwszSearchURL))
        logger.info(dir(lpwszSearchURL))
        
        #lpwszSearchURL[0] = searchgenerator.GenSearchUrl(lpwszSearchURL)
        lpwszSearchURL[0] = "http://cnn.com"
        return S_OK

if __name__ == "__main__":
    try:
        from comtypes.server.register import UseCommandLine
##    logging.basicConfig(level=logging.DEBUG)
        UseCommandLine(LILWSearchHook)
    except Exception:
        import traceback
        traceback.print_exc()
 
