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

Alex Earl <[email protected]> Mon, 11 Jul 2022 06:38:56 -0700
Newsgroups gmane.comp.python.dotnet
Message-ID <CAPiUgVdiH3DkqjXucsa=h=c2ps3ngXrR1EJ4eNhqVPrVDv6CxQ@mail.gmail.com>
--===============4978035449860873506==
Content-Type: multipart/alternative; boundary="0000000000000ef30805e387ade7"

--0000000000000ef30805e387ade7
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Sometimes the file not found error occurs because a dependency of the dll
can not be found.

On Sun, Jul 10, 2022, 20:06 Steven Manross <[email protected]> wrote:

> Howdy all,
>
>
>
> I am somewhat new to pythonnet and .NET programming, and just joined the
> mailing list.  You have my apologies if I am missing something simple.  I
> am aware that BadImageFormatException suggests that I=E2=80=99m trying to=
 open an
> x64 DLL on a 32-bit OS or python installation (or vice versa), but neithe=
r
> seem to be the case (that I can tell).  Depending on exactly how I call t=
he
> code, I get a File Not Found or Bad Image Exception.
>
>
>
> The File Not Found is interesting since the files live in the same
> directory as a working DLL (AlphaVSS.Common works - built to run on any
> architecture, AlphaVSS.x64 doesn=E2=80=99t).
>
>
>
> Specifics:
>
> * Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929
> 64 bit (AMD64)] on win32
>
> * uninstalled Python 3.10 temporarily so I cant get you that detail (sorr=
y)
>
> * And the Windows version clearly states it=E2=80=99s Windows Server 2016=
 x64
>
>
>
> I=E2=80=99m doing my testing from ipython.
>
>
>
> The code below was tested with Python 3.9 x64 (pythonnet 2.5.2 via wheel
> file) and Python 3.10 x64 (installed pythonnet 3.0.0-dev1 from git master=
 a
> few 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
> DLLs from my powershellmodules dir to the =E2=80=9Cclr_dir=E2=80=9D below=
 for testing
> (documentation suggests you copy the files to the same directory for use)=
.
> This DLL was created for .NET Framework 4.5 and I=E2=80=99ve installed Fr=
amework
> 4.8 and this Cumulative update:
>
>
>
>    - 2022-06 Cumulative Update for .NET Framework 4.8 for Windows Server
>    2016 for x64 (KB5014630)
>
>
>
> This code is eventually intended to manipulate the Volume Shadow Copy
> Service on Windows.  I am aware that there is limited functionality for
> ShadowCopies from DISKSHADOW and WMI, but wanted to get more functionalit=
y
> 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.
>
>
>
> 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
> administrator.  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
> just about anything to it including rebuilding it from scratch as it was
> built specifically for testing this.  This is a VM on Hyper-V 2016 (I dou=
bt
> that 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
> incorrect 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' or one of its dependencies. The system
> cannot find the file specified.
>
>     print(f'File Not Found: {e}')
> _______________________________________________
> 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]
>

--0000000000000ef30805e387ade7
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto">Sometimes the file not found error occurs because a depen=
dency of the dll can not be found.=C2=A0</div><br><div class=3D"gmail_quote=
"><div dir=3D"ltr" class=3D"gmail_attr">On Sun, Jul 10, 2022, 20:06 Steven =
Manross &lt;<a href=3D"mailto:[email protected]">[email protected]</a>&gt=
; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .=
8ex;border-left:1px #ccc solid;padding-left:1ex">





<div lang=3D"EN-US" link=3D"#0563C1" vlink=3D"#954F72" style=3D"word-wrap:b=
reak-word">
<div class=3D"m_-2275646643585513278WordSection1">
<p class=3D"MsoNormal">Howdy all,<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">I am somewhat new to pythonnet and .NET programming,=
 and just joined the mailing list.=C2=A0 You have my apologies if I am miss=
ing something simple.=C2=A0 I am aware that BadImageFormatException suggest=
s that I=E2=80=99m 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).=C2=A0 Depending on exactly how I call the code, I get a=
 File Not Found or Bad Image Exception.<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></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=E2=80=99t).<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">Specifics:<u></u><u></u></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<u></u><u></u></p>
<p class=3D"MsoNormal">* uninstalled Python 3.10 temporarily so I cant get =
you that detail (sorry)<u></u><u></u></p>
<p class=3D"MsoNormal">* And the Windows version clearly states it=E2=80=99=
s Windows Server 2016 x64<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">I=E2=80=99m doing my testing from ipython.<u></u><u>=
</u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></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. <u></u><u></u></p=
>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></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 =E2=80=9Cclr=
_dir=E2=80=9D below for testing (documentation suggests you copy the files =
to the same directory for use).=C2=A0 This DLL was created
 for .NET Framework 4.5 and I=E2=80=99ve installed Framework 4.8 and this C=
umulative update:<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<ul style=3D"margin-top:0in" type=3D"disc">
<li class=3D"m_-2275646643585513278MsoListParagraph" style=3D"margin-left:0=
in">2022-06 Cumulative Update for .NET Framework 4.8 for Windows Server 201=
6 for x64 (KB5014630)<u></u><u></u></li></ul>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">This code is eventually intended to manipulate the V=
olume Shadow Copy Service on Windows.=C2=A0 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.
<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">Here is the documentation for the DLLs (version 2.0.=
0) if anyone wants that:<u></u><u></u></p>
<p class=3D"MsoNormal"><a href=3D"https://alphavss.alphaleonis.com/api/Alph=
aleonis.Win32.Vss.html" target=3D"_blank" rel=3D"noreferrer">https://alphav=
ss.alphaleonis.com/api/Alphaleonis.Win32.Vss.html</a><u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">git source:<u></u><u></u></p>
<p class=3D"MsoNormal"><a href=3D"https://github.com/alphaleonis/AlphaVSS" =
target=3D"_blank" rel=3D"noreferrer">https://github.com/alphaleonis/AlphaVS=
S</a><u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">I double checked permissions on the DLLs and I have =
full control as the administrator.=C2=A0 UAC is completely turned off on th=
is machine (if it matters).<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">Any suggestions would be appreciated.=C2=A0 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.=C2=A0 This is=
 a VM on Hyper-V 2016 (I doubt that matters).<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">Thank you for your time!<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">Steven<u></u><u></u></p>
<p class=3D"MsoNormal"># =C2=A0=C2=A0ipython dir output<br>
In [16]: ! dir &quot;c:\users\administrator\desktop\alphavss&quot;<u></u><u=
></u></p>
<p class=3D"MsoNormal">Volume in drive C has no label.<u></u><u></u></p>
<p class=3D"MsoNormal">Volume Serial Number is 8C70-F8A3<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">Directory of c:\users\administrator\desktop\alphavss=
<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">07/09/2022=C2=A0 03:10 AM=C2=A0=C2=A0=C2=A0 &lt;DIR&=
gt;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 .<u></u><u></u></=
p>
<p class=3D"MsoNormal">07/09/2022=C2=A0 03:10 AM=C2=A0=C2=A0=C2=A0 &lt;DIR&=
gt;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ..<u></u><u></u><=
/p>
<p class=3D"MsoNormal">12/03/2019=C2=A0 08:37 PM=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 69,632 AlphaVSS.Common.dll<u></u><u=
></u></p>
<p class=3D"MsoNormal">12/03/2019=C2=A0 08:37 PM=C2=A0=C2=A0 =C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A021,408 AlphaVSS.Common.pdb<u></u>=
<u></u></p>
<p class=3D"MsoNormal">12/03/2019=C2=A0 08:37 PM=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 618,522 AlphaVSS.Common.xml<u></u><u></u>=
</p>
<p class=3D"MsoNormal">12/03/2019=C2=A0 08:37 PM=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 325,120 AlphaVSS.x64.dll<u></u><u></u></p=
>
<p class=3D"MsoNormal">12/03/2019=C2=A0 08:37 PM=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 271,872 AlphaVSS.x86.dll<u></u><u></u></p=
>
<p class=3D"MsoNormal">12/03/2019=C2=A0 08:29 PM=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 342,392 Ijwhost.dll<u></u><u></u></p>
<p class=3D"MsoNormal">07/09/2022=C2=A0 03:09 AM=C2=A0=C2=A0=C2=A0 &lt;DIR&=
gt;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 runtimes<u></u><u=
></u></p>
<p class=3D"MsoNormal">07/09/2022=C2=A0 03:10 AM=C2=A0=C2=A0=C2=A0 &lt;DIR&=
gt;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 win-x64<u></u><u>=
</u></p>
<p class=3D"MsoNormal">07/09/2022=C2=A0 03:10 AM=C2=A0=C2=A0=C2=A0 &lt;DIR&=
gt;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 win-x86<u></u><u>=
</u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal"># code<u></u><u></u></p>
<p class=3D"MsoNormal">import sys<u></u><u></u></p>
<p class=3D"MsoNormal">import clr<u></u><u></u></p>
<p class=3D"MsoNormal">import System<u></u><u></u></p>
<p class=3D"MsoNormal"># from System import Reflection<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">clr_dir =3D &quot;C:\\Users\\Administrator\\Desktop\=
\AlphaVSS&quot; <u></u>
<u></u></p>
<p class=3D"MsoNormal">if clr_dir not in sys.path:<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 sys.path.append(clr_dir)<u></u><u=
></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal"># common_vss =3D Reflection.Assembly.LoadFile(f&#39;=
{clr_dir}\\AlphaVSS.Common.dll&#39;)<u></u><u></u></p>
<p class=3D"MsoNormal">clr.AddReference(&quot;AlphaVSS.Common&quot;) #pylin=
t:disable=3DI1101<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">import Alphaleonis.Win32.Vss as alphavss<u></u><u></=
u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal"># clr.AddReference(&quot;AlphaVSS.x64&quot;) #pylint=
:disable=3DI1101<u></u><u></u></p>
<p class=3D"MsoNormal">vss =3D alphavss.VssFactoryProvider.Default<u></u><u=
></u></p>
<p class=3D"MsoNormal"># x64_vss =3D Reflection.Assembly.LoadFile(f&#39;{cl=
r_dir}\\AlphaVSS.x64.dll&#39;)<u></u><u></u></p>
<p class=3D"MsoNormal"># BadImageFormatException: An attempt was made to lo=
ad a program with an incorrect format. (Exception from HRESULT: 0x8007000B)=
<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">try:<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 factory =3D vss.GetVssFactory()<u=
></u><u></u></p>
<p class=3D"MsoNormal">except System.BadImageFormatException as e:<u></u><u=
></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 print(f&#39;Bad Image Format: {e}=
&#39;)<u></u><u></u></p>
<p class=3D"MsoNormal">except OSError as e:<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 # FileNotFoundException: Could no=
t load file or assembly &#39;AlphaVSS.x64, Version=3D2.0.0.0, Culture=3Dneu=
tral, PublicKeyToken=3D959d3993561034e3&#39; or one of its dependencies. Th=
e system cannot find the file specified.<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 print(f&#39;File Not Found: {e}&#=
39;)<u></u><u></u></p>
</div>
</div>

_______________________________________________<br>
PythonNet mailing list -- <a href=3D"mailto:[email protected]" target=3D=
"_blank" rel=3D"noreferrer">[email protected]</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]=
g" target=3D"_blank" rel=3D"noreferrer">[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman3/lists/pythonnet.python.org/" re=
l=3D"noreferrer noreferrer" target=3D"_blank">https://mail.python.org/mailm=
an3/lists/pythonnet.python.org/</a><br>
Member address: <a href=3D"mailto:[email protected]" target=3D"_blank" =
rel=3D"noreferrer">[email protected]</a><br>
</blockquote></div>

--0000000000000ef30805e387ade7--

--===============4978035449860873506==
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]

--===============4978035449860873506==--