[Python.NET] BadImageFormatException or FileNotFoundException- pythonnet and AlphaVSS

Steven Manross <[email protected]> Sun, 10 Jul 2022 23:21:13 +0000
Newsgroups gmane.comp.python.dotnet
Message-ID <[email protected]>
--===============8363049007069948884==
Content-Language: en-US
Content-Type: multipart/alternative;
	boundary="_000_43d7f3405e73499485056fad5a7c121cmanrossnet_"

--_000_43d7f3405e73499485056fad5a7c121cmanrossnet_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Howdy all,

I am somewhat new to pythonnet and .NET programming, and just joined the ma=
iling list.  You have my apologies if I am missing something simple.  I am =
aware that BadImageFormatException suggests that I'm trying to open an x64 =
DLL on a 32-bit OS or python installation (or vice versa), but neither seem=
 to be the case (that I can tell).  Depending on exactly how I call the cod=
e, I get a File Not Found or Bad Image Exception.

The File Not Found is interesting since the files live in the same director=
y as a working DLL (AlphaVSS.Common works - built to run on any architectur=
e, AlphaVSS.x64 doesn't).

Specifics:
* Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 6=
4 bit (AMD64)] on win32
* uninstalled Python 3.10 temporarily so I cant get you that detail (sorry)
* And the Windows version clearly states it's Windows Server 2016 x64

I'm doing my testing from ipython.

The code below was tested with Python 3.9 x64 (pythonnet 2.5.2 via wheel fi=
le) and Python 3.10 x64 (installed pythonnet 3.0.0-dev1 from git master a f=
ew days ago) on Windows Server 2016 x64 Evaluation patched for current .NET=
 Frameworks 2022-06 and both produce an error on the following code.

AlphaVSS DLLs were generated via powershell/nuget, and I just copied the DL=
Ls from my powershellmodules dir to the "clr_dir" below for testing (docume=
ntation suggests you copy the files to the same directory for use).  This D=
LL was created for .NET Framework 4.5 and I've installed Framework 4.8 and =
this Cumulative update:


  *   2022-06 Cumulative Update for .NET Framework 4.8 for Windows Server 2=
016 for x64 (KB5014630)

This code is eventually intended to manipulate the Volume Shadow Copy Servi=
ce on Windows.  I am aware that there is limited functionality for ShadowCo=
pies from DISKSHADOW and WMI, but wanted to get more functionality from thi=
s VSS .NET Library (specifically Shadow Copy Sets), and pythonnet seemed li=
ke a great way to implement this code in my favorite scripting language.

Here is the documentation for the DLLs (version 2.0.0) if anyone wants that=
:
https://alphavss.alphaleonis.com/api/Alphaleonis.Win32.Vss.html

git source:
https://github.com/alphaleonis/AlphaVSS

I double checked permissions on the DLLs and I have full control as the adm=
inistrator.  UAC is completely turned off on this machine (if it matters).

Any suggestions would be appreciated.  This is a test system, so I can do j=
ust about anything to it including rebuilding it from scratch as it was bui=
lt specifically for testing this.  This is a VM on Hyper-V 2016 (I doubt th=
at matters).

Thank you for your time!

Steven
#   ipython dir output
In [16]: ! dir "c:\users\administrator\desktop\alphavss"
Volume in drive C has no label.
Volume Serial Number is 8C70-F8A3

Directory of c:\users\administrator\desktop\alphavss

07/09/2022  03:10 AM    <DIR>          .
07/09/2022  03:10 AM    <DIR>          ..
12/03/2019  08:37 PM            69,632 AlphaVSS.Common.dll
12/03/2019  08:37 PM            21,408 AlphaVSS.Common.pdb
12/03/2019  08:37 PM           618,522 AlphaVSS.Common.xml
12/03/2019  08:37 PM           325,120 AlphaVSS.x64.dll
12/03/2019  08:37 PM           271,872 AlphaVSS.x86.dll
12/03/2019  08:29 PM           342,392 Ijwhost.dll
07/09/2022  03:09 AM    <DIR>          runtimes
07/09/2022  03:10 AM    <DIR>          win-x64
07/09/2022  03:10 AM    <DIR>          win-x86

# code
import sys
import clr
import System
# from System import Reflection

clr_dir =3D "C:\\Users\\Administrator\\Desktop\\AlphaVSS"
if clr_dir not in sys.path:
    sys.path.append(clr_dir)

# common_vss =3D Reflection.Assembly.LoadFile(f'{clr_dir}\\AlphaVSS.Common.=
dll')
clr.AddReference("AlphaVSS.Common") #pylint:disable=3DI1101

import Alphaleonis.Win32.Vss as alphavss


# clr.AddReference("AlphaVSS.x64") #pylint:disable=3DI1101
vss =3D alphavss.VssFactoryProvider.Default
# x64_vss =3D Reflection.Assembly.LoadFile(f'{clr_dir}\\AlphaVSS.x64.dll')
# BadImageFormatException: An attempt was made to load a program with an in=
correct format. (Exception from HRESULT: 0x8007000B)

try:
    factory =3D vss.GetVssFactory()
except System.BadImageFormatException as e:
    print(f'Bad Image Format: {e}')
except OSError as e:
    # FileNotFoundException: Could not load file or assembly 'AlphaVSS.x64,=
 Version=3D2.0.0.0, Culture=3Dneutral, PublicKeyToken=3D959d3993561034e3' o=
r one of its dependencies. The system cannot find the file specified.
    print(f'File Not Found: {e}')

--_000_43d7f3405e73499485056fad5a7c121cmanrossnet_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
	{font-family:Wingdings;
	panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
	{mso-style-priority:34;
	margin-top:0in;
	margin-right:0in;
	margin-bottom:0in;
	margin-left:.5in;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:"Calibri",sans-serif;
	color:windowtext;}
.MsoChpDefault
	{mso-style-type:export-only;
	font-family:"Calibri",sans-serif;}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
/* List Definitions */
@list l0
	{mso-list-id:365447193;
	mso-list-type:hybrid;
	mso-list-template-ids:572553306 1616653276 67698691 67698693 67698689 6769=
8691 67698693 67698689 67698691 67698693;}
@list l0:level1
	{mso-level-start-at:0;
	mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Symbol;
	mso-fareast-font-family:Calibri;
	mso-bidi-font-family:"Times New Roman";}
@list l0:level2
	{mso-level-number-format:bullet;
	mso-level-text:o;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:"Courier New";}
@list l0:level3
	{mso-level-number-format:bullet;
	mso-level-text:\F0A7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Wingdings;}
@list l0:level4
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Symbol;}
@list l0:level5
	{mso-level-number-format:bullet;
	mso-level-text:o;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:"Courier New";}
@list l0:level6
	{mso-level-number-format:bullet;
	mso-level-text:\F0A7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Wingdings;}
@list l0:level7
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Symbol;}
@list l0:level8
	{mso-level-number-format:bullet;
	mso-level-text:o;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:"Courier New";}
@list l0:level9
	{mso-level-number-format:bullet;
	mso-level-text:\F0A7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Wingdings;}
ol
	{margin-bottom:0in;}
ul
	{margin-bottom:0in;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=3D"EN-US" link=3D"#0563C1" vlink=3D"#954F72" style=3D"word-wrap:=
break-word">
<div class=3D"WordSection1">
<p class=3D"MsoNormal">Howdy all,<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">I am somewhat new to pythonnet and .NET programming,=
 and just joined the mailing list.&nbsp; You have my apologies if I am miss=
ing something simple.&nbsp; I am aware that BadImageFormatException suggest=
s that I&#8217;m trying to open an x64 DLL on a 32-bit
 OS or python installation (or vice versa), but neither seem to be the case=
 (that I can tell).&nbsp; Depending on exactly how I call the code, I get a=
 File Not Found or Bad Image Exception.<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">The File Not Found is interesting since the files li=
ve in the same directory as a working DLL (AlphaVSS.Common works - built to=
 run on any architecture, AlphaVSS.x64 doesn&#8217;t).<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Specifics:<o:p></o:p></p>
<p class=3D"MsoNormal">* Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, =
16:36:42) [MSC v.1929 64 bit (AMD64)] on win32<o:p></o:p></p>
<p class=3D"MsoNormal">* uninstalled Python 3.10 temporarily so I cant get =
you that detail (sorry)<o:p></o:p></p>
<p class=3D"MsoNormal">* And the Windows version clearly states it&#8217;s =
Windows Server 2016 x64<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">I&#8217;m doing my testing from ipython.<o:p></o:p><=
/p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">The code below was tested with Python 3.9 x64 (pytho=
nnet 2.5.2 via wheel file) and Python 3.10 x64 (installed pythonnet 3.0.0-d=
ev1 from git master a few days ago) on Windows Server 2016 x64 Evaluation p=
atched for current .NET Frameworks
 2022-06 and both produce an error on the following code. <o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">AlphaVSS DLLs were generated via powershell/nuget, a=
nd I just copied the DLLs from my powershellmodules dir to the &#8220;clr_d=
ir&#8221; below for testing (documentation suggests you copy the files to t=
he same directory for use).&nbsp; This DLL was created
 for .NET Framework 4.5 and I&#8217;ve installed Framework 4.8 and this Cum=
ulative update:<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<ul style=3D"margin-top:0in" type=3D"disc">
<li class=3D"MsoListParagraph" style=3D"margin-left:0in;mso-list:l0 level1 =
lfo1">2022-06 Cumulative Update for .NET Framework 4.8 for Windows Server 2=
016 for x64 (KB5014630)<o:p></o:p></li></ul>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">This code is eventually intended to manipulate the V=
olume Shadow Copy Service on Windows.&nbsp; I am aware that there is limite=
d functionality for ShadowCopies from DISKSHADOW and WMI, but wanted to get=
 more functionality from this VSS .NET
 Library (specifically Shadow Copy Sets), and pythonnet seemed like a great=
 way to implement this code in my favorite scripting language.
<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Here is the documentation for the DLLs (version 2.0.=
0) if anyone wants that:<o:p></o:p></p>
<p class=3D"MsoNormal">https://alphavss.alphaleonis.com/api/Alphaleonis.Win=
32.Vss.html<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">git source:<o:p></o:p></p>
<p class=3D"MsoNormal">https://github.com/alphaleonis/AlphaVSS<o:p></o:p></=
p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">I double checked permissions on the DLLs and I have =
full control as the administrator.&nbsp; UAC is completely turned off on th=
is machine (if it matters).<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Any suggestions would be appreciated.&nbsp; This is =
a test system, so I can do just about anything to it including rebuilding i=
t from scratch as it was built specifically for testing this.&nbsp; This is=
 a VM on Hyper-V 2016 (I doubt that matters).<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Thank you for your time!<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Steven<o:p></o:p></p>
<p class=3D"MsoNormal"># &nbsp;&nbsp;ipython dir output<br>
In [16]: ! dir &quot;c:\users\administrator\desktop\alphavss&quot;<o:p></o:=
p></p>
<p class=3D"MsoNormal">Volume in drive C has no label.<o:p></o:p></p>
<p class=3D"MsoNormal">Volume Serial Number is 8C70-F8A3<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Directory of c:\users\administrator\desktop\alphavss=
<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">07/09/2022&nbsp; 03:10 AM&nbsp;&nbsp;&nbsp; &lt;DIR&=
gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .<o:p></o:p></p>
<p class=3D"MsoNormal">07/09/2022&nbsp; 03:10 AM&nbsp;&nbsp;&nbsp; &lt;DIR&=
gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ..<o:p></o:p></p>
<p class=3D"MsoNormal">12/03/2019&nbsp; 08:37 PM&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 69,632 AlphaVSS.Common.dll<o:p></o:=
p></p>
<p class=3D"MsoNormal">12/03/2019&nbsp; 08:37 PM&nbsp;&nbsp; &nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;21,408 AlphaVSS.Common.pdb<o:p></o:=
p></p>
<p class=3D"MsoNormal">12/03/2019&nbsp; 08:37 PM&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 618,522 AlphaVSS.Common.xml<o:p></o:p></p=
>
<p class=3D"MsoNormal">12/03/2019&nbsp; 08:37 PM&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 325,120 AlphaVSS.x64.dll<o:p></o:p></p>
<p class=3D"MsoNormal">12/03/2019&nbsp; 08:37 PM&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 271,872 AlphaVSS.x86.dll<o:p></o:p></p>
<p class=3D"MsoNormal">12/03/2019&nbsp; 08:29 PM&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 342,392 Ijwhost.dll<o:p></o:p></p>
<p class=3D"MsoNormal">07/09/2022&nbsp; 03:09 AM&nbsp;&nbsp;&nbsp; &lt;DIR&=
gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; runtimes<o:p></o:=
p></p>
<p class=3D"MsoNormal">07/09/2022&nbsp; 03:10 AM&nbsp;&nbsp;&nbsp; &lt;DIR&=
gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; win-x64<o:p></o:p=
></p>
<p class=3D"MsoNormal">07/09/2022&nbsp; 03:10 AM&nbsp;&nbsp;&nbsp; &lt;DIR&=
gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; win-x86<o:p></o:p=
></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"># code<o:p></o:p></p>
<p class=3D"MsoNormal">import sys<o:p></o:p></p>
<p class=3D"MsoNormal">import clr<o:p></o:p></p>
<p class=3D"MsoNormal">import System<o:p></o:p></p>
<p class=3D"MsoNormal"># from System import Reflection<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">clr_dir =3D &quot;C:\\Users\\Administrator\\Desktop\=
\AlphaVSS&quot; <o:p>
</o:p></p>
<p class=3D"MsoNormal">if clr_dir not in sys.path:<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; sys.path.append(clr_dir)<o:p></o:=
p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"># common_vss =3D Reflection.Assembly.LoadFile(f'{clr=
_dir}\\AlphaVSS.Common.dll')<o:p></o:p></p>
<p class=3D"MsoNormal">clr.AddReference(&quot;AlphaVSS.Common&quot;) #pylin=
t:disable=3DI1101<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">import Alphaleonis.Win32.Vss as alphavss<o:p></o:p><=
/p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"># clr.AddReference(&quot;AlphaVSS.x64&quot;) #pylint=
:disable=3DI1101<o:p></o:p></p>
<p class=3D"MsoNormal">vss =3D alphavss.VssFactoryProvider.Default<o:p></o:=
p></p>
<p class=3D"MsoNormal"># x64_vss =3D Reflection.Assembly.LoadFile(f'{clr_di=
r}\\AlphaVSS.x64.dll')<o:p></o:p></p>
<p class=3D"MsoNormal"># BadImageFormatException: An attempt was made to lo=
ad a program with an incorrect format. (Exception from HRESULT: 0x8007000B)=
<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">try:<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; factory =3D vss.GetVssFactory()<o=
:p></o:p></p>
<p class=3D"MsoNormal">except System.BadImageFormatException as e:<o:p></o:=
p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; print(f'Bad Image Format: {e}')<o=
:p></o:p></p>
<p class=3D"MsoNormal">except OSError as e:<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; # FileNotFoundException: Could no=
t load file or assembly 'AlphaVSS.x64, Version=3D2.0.0.0, Culture=3Dneutral=
, PublicKeyToken=3D959d3993561034e3' or one of its dependencies. The system=
 cannot find the file specified.<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; print(f'File Not Found: {e}')<o:p=
></o:p></p>
</div>
</body>
</html>

--_000_43d7f3405e73499485056fad5a7c121cmanrossnet_--

--===============8363049007069948884==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
PythonNet mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/pythonnet.python.org/
Member address: [email protected]

--===============8363049007069948884==--