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.

Community
  • 1
  • 1
aceminer
  • 4,089
  • 9
  • 56
  • 104
  • 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). – Alexei Levenkov Sep 17 '15 at 00: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 '15 at 01: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 '15 at 01: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? – Alexei Levenkov Sep 17 '15 at 01:40
  • All my code used in the program is here – aceminer Sep 17 '15 at 01:45

0 Answers0