CTypes "[WinError 126] The specified module could not be found"

时间:2018-03-25 19:27:11

标签: c gcc dll cygwin ctypes

I have a (64-bit) DLL written in C to call from CTypes in Windows. It's compiled and linked with GCC in Cygwin, but when I call it from CTypes I get "[WinError 126] The specified module could not be found."

The path to the DLL is correct (I even checked it with "if os.path.exists(path_to_dll)"). Research says that this error is most commonly found when the dll depends on other dlls, so I compiled it with the -M flag in GCC and it reports no dependencies.

I have called DLLs written in assembler using the same ctypes strings, but this C dll does not load even though there are no dependencies.

Here is the C code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "WL_02.h"

double* main(double* X, int64_t len_X)
{
    double *collect;
    int64_t while_counter = 0;
    int64_t collect_counter = 0;
    int64_t new_len = 0;
    int64_t this = 0;
    int64_t start = 0;
    int64_t stop = 0;
    int64_t x = 0;

   /* Initial memory allocation */
   collect = (double *) malloc(len_X);

   while (while_counter < len_X)
   {
      this = X[while_counter];

      if (this < 10)
     {
      start = 0;
      stop = this;
      }
      else
      {
      start = this - 10;
      stop = this;
      }

      for (x = start; x < stop; x++)
      {
         if ((x % 2) != 0)
         {
            x *= x;
            collect[collect_counter] = x;
            collect_counter ++;

               if (collect_counter >= len_X)
               {
               /* Reallocating memory */
               new_len =  len_X * 2;
               collect = (double *) realloc(collect, new_len);
               len_X = new_len;
               }
         }
      }

   while_counter += 1;

   }

   return (0);
}

Here is the .h file (WL_02.h):

#ifndef WL_02_H
#define WL_02_H

#define EXPORT_DLL __declspec(dllexport)

EXPORT_DLL double* main (double* X, int64_t len_X);

#endif

Here are the GCC compile and link strings:

gcc -c WL_02.c -o WL_02.obj
gcc -shared -o WL_02.dll WL_02.obj

Here is the CTypes code:

    hDLL = ctypes.WinDLL("C:/cygwin64/our_files/WL_02/WL_02.dll")
    CallName = hDLL.main
    CallName.argtypes = [ctypes.POINTER(ctypes.c_double),ctypes.c_int64]
    CallName.restype = ctypes.POINTER(ctypes.c_int64)
    ret_ptr = CallName(CA_X,length_array_out)

The error occurs on the first line (hDLL = ...) where I load the DLL.

The only external dependency I can think of is msvcrt.dll which contains malloc and realloc, but I believe all I need to do is include stdlib.h, which I did.

Thanks in advance for any help.

0 个答案:

没有答案