{"id":4278,"date":"2014-03-30T09:40:23","date_gmt":"2014-03-30T09:40:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/python-ctypes-and-dll-that-uses-a-com-object-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:40:23","modified_gmt":"2014-03-30T09:40:23","slug":"python-ctypes-and-dll-that-uses-a-com-object-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/python-ctypes-and-dll-that-uses-a-com-object-collection-of-common-programming-errors\/","title":{"rendered":"Python ctypes and DLL that uses a COM object-Collection of common programming errors"},"content":{"rendered":"<p>Under Windows, I&#8217;m trying to use a 3rd party DLL (<code>SomeLib.dll<\/code>) programmed in C++ from Python 2.7 using <code>ctypes<\/code>. For some of its features, this library uses another COM DLL (<code>SomeCOMlib.dll<\/code>), which itself uses other DLL (<code>LibA.dll<\/code>).<\/p>\n<p><em>Please note that this isn&#8217;t about using a COM DLL directly from Python, but it&#8217;s about using a DLL that uses it from Python.<\/em><\/p>\n<p>To make the integration with Python easier, I&#8217;ve grouped the calls I want to use into my own functions in a new DLL (<code>MyLib.dll<\/code>), also programmed in C++, simply to make the calls from <code>ctypes<\/code> more convenient (using <code>extern \"C\"<\/code> and tailoring the functions for specific scenarios). Essentially, my library exposes 2 functions: <code>doSomethingSimple()<\/code>, <code>doSomethingWithCOMobj()<\/code> (all returning <code>void<\/code>, no parameters).<\/p>\n<p>The &#8220;effective&#8221; dependency hierarchy is as follows:<\/p>\n<pre><code>MyLib.dll\n  SomeLib.dll\n    SomeCOMlib.dll\n      LibA.dll\n<\/code><\/pre>\n<p>I&#8217;m able to write a simple C++ console application (Visual C++) that uses <code>MyLib.dll<\/code> and makes those 2 consecutive calls without problem.<\/p>\n<p>Using Python\/ctypes, the first call works fine, but the call that makes use of COM throws a <code>WindowsError: [Error -529697949] Windows Error 0xE06D7363<\/code>. From the rest of the library behaviour, I can see that the problem comes exactly where that COM call is made.<\/p>\n<p>(The simple test C++ application also fails more or less at the same place if <code>LibA.dll<\/code> is missing, but I&#8217;m not sure if it&#8217;s related.)<\/p>\n<p>I&#8217;ve looked at the dependency hierarchy using Dependency Walker. <code>SomeCOMlib.dll<\/code> isn&#8217;t listed as a dependency of <code>SomeLib.dll<\/code>, although it&#8217;s clearly required, and <code>LibA.dll<\/code> isn&#8217;t listed as a dependency of <code>SomeCOMlib.dll<\/code>, although it&#8217;s also clearly required at run time.<\/p>\n<p>I&#8217;m running everything from the command line, from within the directory where these DLLs are (and the C++ sample executable works fine). I&#8217;ve tried to force the PATH to include that directory, and I&#8217;ve also tried to copy the DLLs to various places where I guessed they might be picked up (<code>C:\\Windows\\System32<\/code> and <code>C:\\Python27\\DLLs<\/code>), without success. <code>SomeCOMlib.dll<\/code> was also registered with <code>regasm.exe<\/code>.<\/p>\n<p><strong>What could cause this difference between using this DLL from a plain C++ application and from Python&#8217;s <code>ctypes<\/code> when it comes to its own usage of the COM mechanism (and possibly the subsequent loading of other DLLs there)?<\/strong><\/p>\n<p><strong>Which steps would give at least a bit more information than <code>Windows Error 0xE06D7363<\/code> from Python, to be able to investigate the problem further?<\/strong><\/p>\n<p>The Python code looks like this:<\/p>\n<pre><code>import ctypes\nmyDll = ctypes.WinDLL(\"MyLib.dll\")\nmyDll.doSomethingSimple()\nmyDll.doSomethingWithCOMobj() # This statement throws the Windows Error\n<\/code><\/pre>\n<p>(The test C++ standalone application that works, linked to <code>MyLib.dll<\/code> makes exactly the same calls within <code>main<\/code>.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Under Windows, I&#8217;m trying to use a 3rd party DLL (SomeLib.dll) programmed in C++ from Python 2.7 using ctypes. For some of its features, this library uses another COM DLL (SomeCOMlib.dll), which itself uses other DLL (LibA.dll). Please note that this isn&#8217;t about using a COM DLL directly from Python, but it&#8217;s about using a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4278","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4278"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4278\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}