From cf6eea3f784433a03ec1565b7c66fc6ea1ac0552 Mon Sep 17 00:00:00 2001
From: "SND\\kernelnet_cp"
 <SND\kernelnet_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Thu, 17 Oct 2013 13:34:07 +0000
Subject: [PATCH] [0.2.x] fixed : dbgCommand return unicode string

git-svn-id: https://pykd.svn.codeplex.com/svn@85813 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd/dbgengine.h      |  6 +++---
 pykd/win/dbgeng.cpp   | 10 +++++-----
 pykd/win/dbgio.cpp    | 10 +++++-----
 pykd/win/dbgio.h      | 22 +++++++++++-----------
 snippets/ipython.py   |  2 ++
 snippets/pykdmagic.py | 23 +++++++++++++++++++++++
 6 files changed, 49 insertions(+), 24 deletions(-)
 create mode 100644 snippets/pykdmagic.py

diff --git a/pykd/dbgengine.h b/pykd/dbgengine.h
index 4b6a796..f19ee5d 100644
--- a/pykd/dbgengine.h
+++ b/pykd/dbgengine.h
@@ -23,13 +23,13 @@ void debugGo();
 void debugStep();
 void debugStepIn();
 void debugBreak();
-std::string debugCommand( const std::wstring &command );
+std::wstring debugCommand( const std::wstring &command );
 BaseTypeVariant evaluate( const std::wstring  &expression, bool cplusplus = false );
 
 // debug output
 void dprint( const std::wstring &str, bool dml = false );
 void dprintln( const std::wstring &str, bool dml = false );
-std::string dreadline();
+std::wstring dreadline();
 void eprint( const std::wstring &str );
 void eprintln( const std::wstring &str );
 
@@ -203,7 +203,7 @@ void appendSymbolPath(const std::string &symPath);
 // Extensions
 ULONG64 loadExtension(const std::wstring &extPath );
 void removeExtension( ULONG64 extHandle );
-std::string callExtension( ULONG64 extHandle, const std::wstring command, const std::wstring  &params  );
+std::wstring callExtension( ULONG64 extHandle, const std::wstring command, const std::wstring  &params  );
 
 };
 
diff --git a/pykd/win/dbgeng.cpp b/pykd/win/dbgeng.cpp
index e5f2d0c..17bb778 100644
--- a/pykd/win/dbgeng.cpp
+++ b/pykd/win/dbgeng.cpp
@@ -265,7 +265,7 @@ BaseTypeVariant evaluate( const std::wstring  &expression, bool cplusplus )
 
 ///////////////////////////////////////////////////////////////////////////////
 
-std::string debugCommand( const std::wstring &command )
+std::wstring debugCommand( const std::wstring &command )
 {
     PyThread_StateRestore pyThreadRestore( g_dbgEng->pystate );
 
@@ -275,9 +275,9 @@ std::string debugCommand( const std::wstring &command )
     hres = g_dbgEng->control->ExecuteWide( DEBUG_OUTCTL_THIS_CLIENT, command.c_str(), 0 );
 
     if ( FAILED( hres ) )
-        throw  DbgException( "IDebugControl::Execute  failed" ); 
+        throw  DbgException( "IDebugControl::ExecuteWide  failed" ); 
 
-    return std::string( outReader.Line() ); 
+    return outReader.Line();
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -1314,7 +1314,7 @@ void removeExtension( ULONG64 extHandle )
 
 ///////////////////////////////////////////////////////////////////////////////
 
-std::string callExtension( ULONG64 extHandle, const std::wstring command, const std::wstring  &params  )
+std::wstring callExtension( ULONG64 extHandle, const std::wstring command, const std::wstring  &params  )
 {
     PyThread_StateRestore pyThreadRestore( g_dbgEng->pystate );
 
@@ -1327,7 +1327,7 @@ std::string callExtension( ULONG64 extHandle, const std::wstring command, const
     if ( FAILED( hres ) )
         throw  DbgException( "IDebugControl::CallExtension  failed" ); 
         
-    return std::string( outReader.Line() );
+    return std::wstring( outReader.Line() );
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/pykd/win/dbgio.cpp b/pykd/win/dbgio.cpp
index 6e40704..b03080f 100644
--- a/pykd/win/dbgio.cpp
+++ b/pykd/win/dbgio.cpp
@@ -67,16 +67,16 @@ void eprintln( const std::wstring &str )
 
 ///////////////////////////////////////////////////////////////////////////////////
 
-std::string dreadline()
+std::wstring dreadline()
 {
     PyThread_StateRestore pyThreadRestore( g_dbgEng->pystate );
 
-    char    str[0x100];
-    ULONG   inputSize = 0;
+    wchar_t    str[0x100];
+    ULONG     inputSize = 0;
 
-    g_dbgEng->control->Input( str, sizeof(str), &inputSize );
+    g_dbgEng->control->InputWide( str, sizeof(str), &inputSize );
 
-    return std::string( str ) + "\n";
+    return std::wstring( str ) + L"\n";
 }
 
 ///////////////////////////////////////////////////////////////////////////////////
diff --git a/pykd/win/dbgio.h b/pykd/win/dbgio.h
index 121b143..7892829 100644
--- a/pykd/win/dbgio.h
+++ b/pykd/win/dbgio.h
@@ -54,7 +54,7 @@ class DbgIn {
 
 public:
 
-    std::string
+    std::wstring
     readline() {
         return dreadline();
     }
@@ -68,7 +68,7 @@ public:
 
 ///////////////////////////////////////////////////////////////////////////////
 
-class OutputReader : public IDebugOutputCallbacks, private boost::noncopyable {
+class OutputReader : public IDebugOutputCallbacksWide, private boost::noncopyable {
 
 public:
 
@@ -78,21 +78,21 @@ public:
 
         m_client = client;
 
-        hres = m_client->GetOutputCallbacks( &m_previousCallback );
+        hres = m_client->GetOutputCallbacksWide( &m_previousCallback );
         if ( FAILED( hres ) )
             throw DbgException( "IDebugClient::GetOutputCallbacks failed" );
 
-        hres = m_client->SetOutputCallbacks( this );
+        hres = m_client->SetOutputCallbacksWide( this );
         if ( FAILED( hres ) )
             throw DbgException( "IDebugClient::GetOutputCallbacks failed" );
     }
 
     ~OutputReader() 
     {
-        m_client->SetOutputCallbacks( m_previousCallback );
+        m_client->SetOutputCallbacksWide( m_previousCallback );
     }
 
-    const std::string&
+    const std::wstring&
     Line() const {
         return  m_readLine;
     }
@@ -117,22 +117,22 @@ private:
 
    STDMETHOD(Output)(
         __in ULONG Mask,
-        __in PCSTR Text )
+        __in PCWSTR Text )
    {
         if ( Mask == DEBUG_OUTPUT_NORMAL )
         {
-            m_readLine += std::string( Text );
+            m_readLine += std::wstring( Text );
         }
        return S_OK;
    }
 
 private:
 
-    std::string                         m_readLine;
+    std::wstring                        m_readLine;
 
-    CComPtr<IDebugOutputCallbacks>      m_previousCallback;
+    CComPtr<IDebugOutputCallbacksWide>  m_previousCallback;
 
-    CComPtr<IDebugClient4>              m_client;
+    CComQIPtr<IDebugClient5>            m_client;
 };
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/snippets/ipython.py b/snippets/ipython.py
index 630890f..c1e9109 100644
--- a/snippets/ipython.py
+++ b/snippets/ipython.py
@@ -15,6 +15,8 @@ cfg.PromptManager.in_template = 'In <\\#>: '
 cfg.PromptManager.in2_template = '   .\\D.: '
 cfg.PromptManager.out_template = 'Out<\\#>: '
 
+cfg.InteractiveShellApp.extensions = [ 'pykdmagic' ]
+
 ipshell = InteractiveShellEmbed(config=cfg)
 
 ipshell()    
\ No newline at end of file
diff --git a/snippets/pykdmagic.py b/snippets/pykdmagic.py
new file mode 100644
index 0000000..a000ad0
--- /dev/null
+++ b/snippets/pykdmagic.py
@@ -0,0 +1,23 @@
+
+from IPython.core.magic import Magics, magics_class, line_magic, cell_magic, line_cell_magic
+import pykd
+
+@magics_class
+class PykdMagic (Magics):
+
+    @line_magic
+    def kd(self,line):
+        "any windbg command"
+        try:
+            pykd.dprintln( pykd.dbgCommand(line) )
+        except pykd.BaseException:
+            pykd.dprintln("invalid windbg syntax")
+        return None
+
+def load_ipython_extension(ipython):
+    ipython.register_magics(PykdMagic)   
+
+def unload_ipython_extension(ipython):
+    pass 
+        
+