1

So i was experiencing this problem.

dll not loading in python

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;

class Test
{
    [DllExport("add", CallingConvention = CallingConvention.Cdecl)]
    public static int TestExport(int left, int right)
    {
        return left + right;
    }
}

Python

import ctypes
a = ctypes.cdll.LoadLibrary('ClassLibrary1.dll')
a.add(3, 5)

Error

AttributeError: function 'add' not found

I resolved it by targeting the build to be x86 instead of any CPU courtesy of the answer provided. However, i do not understand why it has to be x86 when my computer/OS is 64 bit.

Error when its a 64 bit dll

WindowsError: [Error 193] %1 is not a valid Win32 application

Simply put. When i target a x86 architecture and build the library, it works. however, when i target a x64 architecture. It throws up a windows error

My OS is 64 bit. Python 2.7.9 64 bit.

5
  • From you post it is not clear what unmanged DLL you are talking about and what is actual bitness of the DLL. (Linked post talks about exporting methods from managed DLL and says you need 2 separate version - on x86, another x64). Sep 17, 2015 at 0:51
  • 1
    Is your python process running in the corresponding architecture? (In other words, are you trying to load the 64 bit dll into a 32 bit process?)
    – theB
    Sep 17, 2015 at 1:31
  • No. Firstly i am running the dll from a python shell in my 64 bit PC. Firstly, i built the dll and copied it to desktop before running a python shell in desktop.
    – aceminer
    Sep 17, 2015 at 1:36
  • Are you expecting native 32 bit python process to load 64 bit DLL? I think adding following clarifications would be useful: OS 32/64 bit? EXE (phyton.exe I assume) 32/64 bit? your managed assembly built from C# files x86/x64/anyCPU? if you have native (unmanaged) assembly what its bitness x86/x64? Sep 17, 2015 at 1:40
  • All my code used in the program is here
    – aceminer
    Sep 17, 2015 at 1:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.