Commit 3fab992e authored by SND\ussrhero_cp's avatar SND\ussrhero_cp Committed by Mikhail I. Izmestev
Browse files

[0.3.x] added : callFunctionRaw ( call function without prototype )

git-svn-id: https://pykd.svn.codeplex.com/svn@91241 9b283d60-5439-405e-af05-b73fd8c4d996
parent 61bf1b83
......@@ -2,7 +2,7 @@
#define PYKD_VERSION_MAJOR 0
#define PYKD_VERSION_MINOR 3
#define PYKD_VERSION_SUBVERSION 2
#define PYKD_VERSION_BUILDNO 6
#define PYKD_VERSION_BUILDNO 7
#define __VER_STR2__(x) #x
#define __VER_STR1__(x) __VER_STR2__(x)
......
......@@ -418,6 +418,7 @@ BOOST_PYTHON_MODULE( pykd )
"Define custom function prototype" ) );
python::def( "callFunctionByPtr", python::raw_function(pykd::callFunctionByVar, 1) );
python::def( "callFunctionByAddr", python::raw_function(pykd::callFunctionByOffset, 2) );
python::def( "callFunctionRaw", python::raw_function(pykd::callFunctionRaw, 1) );
python::def( "getTypeFromSource", &pykd::getTypeFromSource, getTypeFromSource_( python::args("sourceCode", "typeName", "compileOptions"),
"Create typeInfo class from C/C++ source code") );
......
......@@ -159,6 +159,7 @@ python::list TypeInfoAdapter::getElementDir(kdlib::TypeInfo &typeInfo)
///////////////////////////////////////////////////////////////////////////////
python::object callTypedVar(kdlib::TypedVarPtr& funcobj, python::tuple& args)
{
kdlib::TypedValue retVal;
......@@ -176,6 +177,30 @@ python::object callTypedVar(kdlib::TypedVarPtr& funcobj, python::tuple& args)
continue;
}
python::extract<std::wstring> getStrVar(args[i]);
if ( getStrVar.check() )
{
std::wstringstream sstr;
if ( funcobj->getType()->getElement(i)->getName() == L"Char*" )
{
std::string strArg = _bstr_t( getStrVar().c_str() );
sstr << "Char[" << std::dec << strArg.size() + 1 << ']';
argLst.push_back( kdlib::loadTypedVar(sstr.str(), kdlib::getCacheAccessor( strArg.c_str(), strArg.size() + 1 ) ) );
continue;
}
if ( funcobj->getType()->getElement(i)->getName() == L"WChar*" )
{
std::wstring strArg = getStrVar();
sstr << "WChar[" << std::dec << strArg.size() + 1 << ']';
argLst.push_back( kdlib::loadTypedVar(sstr.str(), kdlib::getCacheAccessor( strArg.c_str(), 2*(strArg.size() + 1) ) ) );
continue;
}
throw kdlib::TypeException(L"failed convert string argument");
}
python::extract<kdlib::NumBehavior> getNumVar(args[i]);
if ( getNumVar.check() )
{
......@@ -184,8 +209,15 @@ python::object callTypedVar(kdlib::TypedVarPtr& funcobj, python::tuple& args)
continue;
}
kdlib::NumVariant var= NumVariantAdaptor::convertToVariant(args[i]);
argLst.push_back( var );
if ( python::extract<int>(args[i]).check() )
{
kdlib::NumVariant var= NumVariantAdaptor::convertToVariant(args[i]);
argLst.push_back( var );
continue;
}
throw kdlib::TypeException(L"failed convert argument");
}
{
......@@ -227,4 +259,77 @@ python::object callFunctionByOffset( python::tuple& args, python::dict& kwargs)
///////////////////////////////////////////////////////////////////////////////
python::object callFunctionRaw( python::tuple& args, python::dict& kwargs)
{
kdlib::MEMOFFSET_64 funcaddr = python::extract<kdlib::MEMOFFSET_64>(args[0]);
kdlib::CallingConventionType callingConvention = kdlib::CallingConventionType::CallConv_NearC;
if ( kwargs.has_key("callingConvention") )
callingConvention = python::extract<kdlib::CallingConventionType>(kwargs["callingConvention"]);
size_t argCount = python::len(args);
kdlib::TypedValueList argLst;
for ( size_t i = 0; i < argCount; ++i )
{
python::extract<kdlib::TypedVarPtr> getTypedVar(args[i]);
if ( getTypedVar.check() )
{
argLst.push_back( getTypedVar() );
continue;
}
python::extract<std::string> getStrVar(args[i]);
if ( getStrVar.check() )
{
std::string strArg = getStrVar();
std::wstringstream sstr;
sstr << L"Char[" << std::dec << strArg.size() + 1 << L']';
argLst.push_back( kdlib::loadTypedVar(sstr.str(), kdlib::getCacheAccessor( strArg.c_str(), strArg.size() + 1 ) ) );
continue;
}
python::extract<std::wstring> getWStrVar(args[i]);
if ( getWStrVar.check() )
{
std::wstring strArg = getWStrVar();
std::wstringstream sstr;
sstr << L"WChar[" << std::dec << strArg.size() + 1 << L']';
argLst.push_back( kdlib::loadTypedVar(sstr.str(), kdlib::getCacheAccessor( strArg.c_str(), 2*(strArg.size() + 1) ) ) );
continue;
}
python::extract<kdlib::NumBehavior> getNumVar(args[i]);
if ( getNumVar.check() )
{
kdlib::NumVariant var = getNumVar();
argLst.push_back( var );
continue;
}
if ( python::extract<int>(args[i]).check() )
{
kdlib::NumVariant var= NumVariantAdaptor::convertToVariant(args[i]);
argLst.push_back( var );
continue;
}
throw kdlib::TypeException(L"failed convert argument");
}
kdlib::NumVariant retVal;
{
AutoRestorePyState pystate;
retVal = kdlib::callRaw(funcaddr, callingConvention, argLst);
}
return NumVariantAdaptor::convertToPython(retVal);
}
///////////////////////////////////////////////////////////////////////////////
} // pykd namespace
......@@ -48,6 +48,8 @@ python::object callFunctionByVar( python::tuple& args, python::dict& kwargs );
python::object callFunctionByOffset( python::tuple& args, python::dict& kwargs);
python::object callFunctionRaw( python::tuple& args, python::dict& kwargs);
inline kdlib::TypeInfoPtr getTypeInfoByName( const std::wstring &name )
{
......

import pykd
GENERIC_READ = 0x80000000
......@@ -27,13 +27,10 @@ def main():
CreateFileW_Type.append("dwFlagsAndAttributes", DWORD )
CreateFileW_Type.append("hTemplateFile", HANDLE )
fileNameBuf = pykd.stackAlloc(100)
pykd.writeWStr(fileNameBuf, "C:\\temp\\testfile.txt")
CreateFileW = pykd.typedVar( CreateFileW_Type, kernel32.CreateFileW )
fileHandle = CreateFileW(
fileNameBuf,
"C:\\temp\\testfile.txt",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
......@@ -43,7 +40,5 @@ def main():
print "File Handle", hex(fileHandle)
pykd.stackFree(100)
if __name__ == "__main__":
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment