[Python.NET] Re: BadImageFormatException or FileNotFoundException- pythonnet and AlphaVSS
Mark Visser <[email protected]> Mon, 11 Jul 2022 11:02:16 -0400
| Newsgroups | gmane.comp.python.dotnet |
|---|---|
| Message-ID | <[email protected]> |
--===============6294416213166923681== Content-Type: multipart/alternative; boundary="Apple-Mail=_BB669CB7-1381-4EF0-A84E-12D217B54CEE" --Apple-Mail=_BB669CB7-1381-4EF0-A84E-12D217B54CEE Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 +1 - in the past I've used https://www.dependencywalker.com/ = <https://www.dependencywalker.com/> to figure out the problem. On mac = you can use otool -L, and on linux you can use ldd. > On Jul 11, 2022, at 9:38 AM, Alex Earl <[email protected]> wrote: >=20 > Sometimes the file not found error occurs because a dependency of the = dll can not be found.=20 >=20 > On Sun, Jul 10, 2022, 20:06 Steven Manross <[email protected] = <mailto:[email protected]>> wrote: > Howdy all, >=20 > =20 >=20 > 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 neither seem to be the case (that I can tell). = Depending on exactly how I call the code, I get a File Not Found or Bad = Image Exception. >=20 > =20 >=20 > 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). >=20 > =20 >=20 > Specifics: >=20 > * Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC = v.1929 64 bit (AMD64)] on win32 >=20 > * uninstalled Python 3.10 temporarily so I cant get you that detail = (sorry) >=20 > * And the Windows version clearly states it=E2=80=99s Windows Server = 2016 x64 >=20 > =20 >=20 > I=E2=80=99m doing my testing from ipython. >=20 > =20 >=20 > 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. >=20 > =20 >=20 > 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 Framework 4.8 and this Cumulative update: >=20 > =20 >=20 > 2022-06 Cumulative Update for .NET Framework 4.8 for Windows Server = 2016 for x64 (KB5014630) > =20 >=20 > 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 = 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. >=20 > =20 >=20 > Here is the documentation for the DLLs (version 2.0.0) if anyone wants = that: >=20 > https://alphavss.alphaleonis.com/api/Alphaleonis.Win32.Vss.html = <https://alphavss.alphaleonis.com/api/Alphaleonis.Win32.Vss.html> > =20 >=20 > git source: >=20 > https://github.com/alphaleonis/AlphaVSS = <https://github.com/alphaleonis/AlphaVSS> > =20 >=20 > 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). >=20 > =20 >=20 > 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 doubt that matters). >=20 > =20 >=20 > Thank you for your time! >=20 > =20 >=20 > Steven >=20 > # ipython dir output > In [16]: ! dir "c:\users\administrator\desktop\alphavss" >=20 > Volume in drive C has no label. >=20 > Volume Serial Number is 8C70-F8A3 >=20 > =20 >=20 > Directory of c:\users\administrator\desktop\alphavss >=20 > =20 >=20 > 07/09/2022 03:10 AM <DIR> . >=20 > 07/09/2022 03:10 AM <DIR> .. >=20 > 12/03/2019 08:37 PM 69,632 AlphaVSS.Common.dll >=20 > 12/03/2019 08:37 PM 21,408 AlphaVSS.Common.pdb >=20 > 12/03/2019 08:37 PM 618,522 AlphaVSS.Common.xml >=20 > 12/03/2019 08:37 PM 325,120 AlphaVSS.x64.dll >=20 > 12/03/2019 08:37 PM 271,872 AlphaVSS.x86.dll >=20 > 12/03/2019 08:29 PM 342,392 Ijwhost.dll >=20 > 07/09/2022 03:09 AM <DIR> runtimes >=20 > 07/09/2022 03:10 AM <DIR> win-x64 >=20 > 07/09/2022 03:10 AM <DIR> win-x86 >=20 > =20 >=20 > # code >=20 > import sys >=20 > import clr >=20 > import System >=20 > # from System import Reflection >=20 > =20 >=20 > clr_dir =3D "C:\\Users\\Administrator\\Desktop\\AlphaVSS" >=20 > if clr_dir not in sys.path: >=20 > sys.path.append(clr_dir) >=20 > =20 >=20 > # common_vss =3D = Reflection.Assembly.LoadFile(f'{clr_dir}\\AlphaVSS.Common.dll') >=20 > clr.AddReference("AlphaVSS.Common") #pylint:disable=3DI1101 >=20 > =20 >=20 > import Alphaleonis.Win32.Vss as alphavss >=20 > =20 >=20 > =20 >=20 > # clr.AddReference("AlphaVSS.x64") #pylint:disable=3DI1101 >=20 > vss =3D alphavss.VssFactoryProvider.Default >=20 > # x64_vss =3D = Reflection.Assembly.LoadFile(f'{clr_dir}\\AlphaVSS.x64.dll') >=20 > # BadImageFormatException: An attempt was made to load a program with = an incorrect format. (Exception from HRESULT: 0x8007000B) >=20 > =20 >=20 > try: >=20 > factory =3D vss.GetVssFactory() >=20 > except System.BadImageFormatException as e: >=20 > print(f'Bad Image Format: {e}') >=20 > except OSError as e: >=20 > # 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. >=20 > print(f'File Not Found: {e}') >=20 > _______________________________________________ > PythonNet mailing list -- [email protected] = <mailto:[email protected]> > To unsubscribe send an email to [email protected] = <mailto:[email protected]> > https://mail.python.org/mailman3/lists/pythonnet.python.org/ = <https://mail.python.org/mailman3/lists/pythonnet.python.org/> > Member address: [email protected] <mailto:[email protected]> > _______________________________________________ > 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] --Apple-Mail=_BB669CB7-1381-4EF0-A84E-12D217B54CEE Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 <html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; = charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; = -webkit-nbsp-mode: space; line-break: after-white-space;" class=3D"">+1 = - in the past I've used <a href=3D"https://www.dependencywalker.com/"= class=3D"">https://www.dependencywalker.com/</a> to figure out the = problem. On mac you can use otool -L, and on linux you can use ldd.<br = class=3D""> <div><br class=3D""><blockquote type=3D"cite" class=3D""><div = class=3D"">On Jul 11, 2022, at 9:38 AM, Alex Earl <<a = href=3D"mailto:[email protected]" = class=3D"">[email protected]</a>> wrote:</div><br = class=3D"Apple-interchange-newline"><div class=3D""><div dir=3D"auto" = class=3D"">Sometimes the file not found error occurs because a = dependency of the dll can not be found. </div><br class=3D""><div = class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Sun, Jul = 10, 2022, 20:06 Steven Manross <<a href=3D"mailto:[email protected]" = class=3D"">[email protected]</a>> wrote:<br = class=3D""></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:break-word" class=3D""> <div class=3D"m_-2275646643585513278WordSection1"><p = class=3D"MsoNormal">Howdy all,<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"><u class=3D""></u> <u class=3D""></u></p><p = class=3D"MsoNormal">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 neither seem to be the = case (that I can tell). Depending on exactly how I call the code, = I get a File Not Found or Bad Image Exception.<u class=3D""></u><u = class=3D""></u></p><p class=3D"MsoNormal"><u class=3D""></u> <u = class=3D""></u></p><p class=3D"MsoNormal">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).<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"><u class=3D""></u> <u class=3D""></u></p><p = class=3D"MsoNormal">Specifics:<u class=3D""></u><u class=3D""></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 class=3D""></u><u = class=3D""></u></p><p class=3D"MsoNormal">* uninstalled Python 3.10 = temporarily so I cant get you that detail (sorry)<u class=3D""></u><u = class=3D""></u></p><p class=3D"MsoNormal">* And the Windows version = clearly states it=E2=80=99s Windows Server 2016 x64<u class=3D""></u><u = class=3D""></u></p><p class=3D"MsoNormal"><u class=3D""></u> <u = class=3D""></u></p><p class=3D"MsoNormal">I=E2=80=99m doing my testing = from ipython.<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"><u class=3D""></u> <u class=3D""></u></p><p = class=3D"MsoNormal">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. <u = class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal"><u = class=3D""></u> <u class=3D""></u></p><p class=3D"MsoNormal">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 Framework 4.8 and = this Cumulative update:<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"><u class=3D""></u> <u class=3D""></u></p> <ul style=3D"margin-top:0in" type=3D"disc" class=3D""> <li class=3D"m_-2275646643585513278MsoListParagraph" = style=3D"margin-left:0in">2022-06 Cumulative Update for .NET Framework = 4.8 for Windows Server 2016 for x64 (KB5014630)<u class=3D""></u><u = class=3D""></u></li></ul><p class=3D"MsoNormal"><u class=3D""></u> <u= class=3D""></u></p><p class=3D"MsoNormal">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 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 class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal"><u = class=3D""></u> <u class=3D""></u></p><p class=3D"MsoNormal">Here = is the documentation for the DLLs (version 2.0.0) if anyone wants = that:<u class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal"><a = href=3D"https://alphavss.alphaleonis.com/api/Alphaleonis.Win32.Vss.html" = target=3D"_blank" rel=3D"noreferrer" = class=3D"">https://alphavss.alphaleonis.com/api/Alphaleonis.Win32.Vss.html= </a><u class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal"><u = class=3D""></u> <u class=3D""></u></p><p class=3D"MsoNormal">git = source:<u class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal"><a = href=3D"https://github.com/alphaleonis/AlphaVSS" target=3D"_blank" = rel=3D"noreferrer" = class=3D"">https://github.com/alphaleonis/AlphaVSS</a><u class=3D""></u><u= class=3D""></u></p><p class=3D"MsoNormal"><u class=3D""></u> <u = class=3D""></u></p><p class=3D"MsoNormal">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).<u class=3D""></u><u= class=3D""></u></p><p class=3D"MsoNormal"><u class=3D""></u> <u = class=3D""></u></p><p class=3D"MsoNormal">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 = doubt that matters).<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"><u class=3D""></u> <u class=3D""></u></p><p = class=3D"MsoNormal">Thank you for your time!<u class=3D""></u><u = class=3D""></u></p><p class=3D"MsoNormal"><u class=3D""></u> <u = class=3D""></u></p><p class=3D"MsoNormal">Steven<u class=3D""></u><u = class=3D""></u></p><p class=3D"MsoNormal"># ipython dir = output<br class=3D""> In [16]: ! dir "c:\users\administrator\desktop\alphavss"<u = class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal">Volume in = drive C has no label.<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">Volume Serial Number is 8C70-F8A3<u class=3D""></u><u = class=3D""></u></p><p class=3D"MsoNormal"><u class=3D""></u> <u = class=3D""></u></p><p class=3D"MsoNormal">Directory of = c:\users\administrator\desktop\alphavss<u class=3D""></u><u = class=3D""></u></p><p class=3D"MsoNormal"><u class=3D""></u> <u = class=3D""></u></p><p class=3D"MsoNormal">07/09/2022 03:10 = AM = <DIR> .<u = class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">07/09/2022 03:10 AM = <DIR> ..<u = class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">12/03/2019 08:37 = PM = 69,632 AlphaVSS.Common.dll<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">12/03/2019 08:37 PM = 21,408 = AlphaVSS.Common.pdb<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">12/03/2019 08:37 = PM 618,522 = AlphaVSS.Common.xml<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">12/03/2019 08:37 = PM 325,120 = AlphaVSS.x64.dll<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">12/03/2019 08:37 = PM 271,872 = AlphaVSS.x86.dll<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">12/03/2019 08:29 = PM 342,392 = Ijwhost.dll<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">07/09/2022 03:09 AM = <DIR> = runtimes<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">07/09/2022 03:10 AM = <DIR> = win-x64<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">07/09/2022 03:10 AM = <DIR> = win-x86<u class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal"><u = class=3D""></u> <u class=3D""></u></p><p class=3D"MsoNormal"># = code<u class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal">import = sys<u class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal">import = clr<u class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal">import = System<u class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal"># = from System import Reflection<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"><u class=3D""></u> <u class=3D""></u></p><p = class=3D"MsoNormal">clr_dir =3D = "C:\\Users\\Administrator\\Desktop\\AlphaVSS" <u class=3D""></u> <u class=3D""></u></p><p class=3D"MsoNormal">if clr_dir not in = sys.path:<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"> sys.path.append(clr_dir)<u = class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal"><u = class=3D""></u> <u class=3D""></u></p><p class=3D"MsoNormal"># = common_vss =3D = Reflection.Assembly.LoadFile(f'{clr_dir}\\AlphaVSS.Common.dll')<u = class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">clr.AddReference("AlphaVSS.Common") = #pylint:disable=3DI1101<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"><u class=3D""></u> <u class=3D""></u></p><p = class=3D"MsoNormal">import Alphaleonis.Win32.Vss as alphavss<u = class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal"><u = class=3D""></u> <u class=3D""></u></p><p class=3D"MsoNormal"><u = class=3D""></u> <u class=3D""></u></p><p class=3D"MsoNormal"># = clr.AddReference("AlphaVSS.x64") #pylint:disable=3DI1101<u = class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal">vss =3D = alphavss.VssFactoryProvider.Default<u class=3D""></u><u = class=3D""></u></p><p class=3D"MsoNormal"># x64_vss =3D = Reflection.Assembly.LoadFile(f'{clr_dir}\\AlphaVSS.x64.dll')<u = class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal"># = BadImageFormatException: An attempt was made to load a program with an = incorrect format. (Exception from HRESULT: 0x8007000B)<u class=3D""></u><u= class=3D""></u></p><p class=3D"MsoNormal"><u class=3D""></u> <u = class=3D""></u></p><p class=3D"MsoNormal">try:<u class=3D""></u><u = class=3D""></u></p><p class=3D"MsoNormal"> factory =3D = vss.GetVssFactory()<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal">except System.BadImageFormatException as e:<u = class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"> print(f'Bad Image Format: {e}')<u = class=3D""></u><u class=3D""></u></p><p class=3D"MsoNormal">except = OSError as e:<u class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"> # 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.<u = class=3D""></u><u class=3D""></u></p><p = class=3D"MsoNormal"> print(f'File Not Found: {e}')<u = class=3D""></u><u class=3D""></u></p> </div> </div> _______________________________________________<br class=3D""> PythonNet mailing list -- <a href=3D"mailto:[email protected]" = target=3D"_blank" rel=3D"noreferrer" = class=3D"">[email protected]</a><br class=3D""> To unsubscribe send an email to <a = href=3D"mailto:[email protected]" target=3D"_blank" = rel=3D"noreferrer" class=3D"">[email protected]</a><br = class=3D""> <a href=3D"https://mail.python.org/mailman3/lists/pythonnet.python.org/" = rel=3D"noreferrer noreferrer" target=3D"_blank" = class=3D"">https://mail.python.org/mailman3/lists/pythonnet.python.org/</a= ><br class=3D""> Member address: <a href=3D"mailto:[email protected]" target=3D"_blank"= rel=3D"noreferrer" class=3D"">[email protected]</a><br class=3D""> </blockquote></div> _______________________________________________<br class=3D"">PythonNet = mailing list -- <a href=3D"mailto:[email protected]" = class=3D"">[email protected]</a><br class=3D"">To unsubscribe send an = email to <a href=3D"mailto:[email protected]" = class=3D"">[email protected]</a><br class=3D""><a = href=3D"https://mail.python.org/mailman3/lists/pythonnet.python.org/" = class=3D"">https://mail.python.org/mailman3/lists/pythonnet.python.org/</a= ><br class=3D"">Member address: [email protected]<br = class=3D""></div></blockquote></div><br class=3D""></body></html>= --Apple-Mail=_BB669CB7-1381-4EF0-A84E-12D217B54CEE-- --===============6294416213166923681== 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] --===============6294416213166923681==--