7

I am trying to compile a DLL library on Windows that can be used with NativeCall in Raku. Here is a minimal C code (my_c_dll.c):

#include <stdio.h>
#define EXPORTED __declspec(dllexport)
extern __declspec(dllexport) void foo();

void foo()
{
  printf("Hello from C\n");
}

I am on Windows 10 and have installed Build Tools for Visual Studio 2019. To compile the DLL I open a "Developer Command Prompt for VS 2019" and run:

> cl.exe /c my_c_dll.c
> link /DLL /OUT:my_c_dll.dll my_c_dll.obj

This creates a DLL my_c_dll.dll, then I try to use this from Raku (test-dll.raku):

use v6.d;
use NativeCall;
sub foo() is native("./my_c_dll.dll"){ * }
foo();

but when I run this (I have installed Rakudo version 2020.05.1) I get:

> raku test-dll.raku
Cannot locate native library '(null)': error 0xc1
  in method setup at C:\rakudo\share\perl6\core\sources\947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B (NativeCall) line 298
  in block foo at C:\rakudo\share\perl6\core\sources\947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B (NativeCall) line 594
  in block <unit> at test-dll.raku line 9

What can be the problem here?

1 Answer 1

9

Cannot locate native library '(null)': error 0xc1

The error code 0xc1 is a Windows system error code :

ERROR_BAD_EXE_FORMAT
193 (0xC1)
%1 is not a valid Win32 application.

By inspecting the header of the DLL using dumpbin

> dumpbin /headers my_c_dll.dll
Microsoft (R) COFF/PE Dumper Version 14.26.28806.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file my_c_dll.dll

PE signature found

File Type: DLL

FILE HEADER VALUES
             14C machine (x86)
               4 number of sections
        6043A8CB time date stamp Sat Mar  6 17:07:39 2021
               0 file pointer to symbol table
               0 number of symbols
              E0 size of optional header
            2102 characteristics
                   Executable
                   32 bit word machine
                   DLL

I observe that it says machine (x86) and 32 bit word machine, so I suspect the DLL is a 32-bit DLL. However, I am on a 64 bit machine:

> PowerShell -Command "systeminfo | perl -nE 'say if /System Type/'"
System Type:               x64-based PC

It turns out that the Build Tools for Visual Studio 2019 installed some more developer prompts in addition to the "Developer Command Prompt for VS 2019", namely:

  • x64 Native Tools Command Prompt for VS 2019
  • x64_x86 Cross Tools Command Prompt for VS 2019
  • x86 Native Tools Command Prompt for VS 2019
  • x86_x64 Cross Tools Command Prompt for VS 2019

By opening a "x64 Native Tools Command Prompt for VS 2019" and recompiling the DLL here, I now get:

> dumpbin /headers my_c_dll.dll
Microsoft (R) COFF/PE Dumper Version 14.26.28806.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file my_c_dll.dll

PE signature found

File Type: DLL

FILE HEADER VALUES
            8664 machine (x64)
               6 number of sections
        6043AAA2 time date stamp Sat Mar  6 17:15:30 2021
               0 file pointer to symbol table
               0 number of symbols
              F0 size of optional header
            2022 characteristics
                   Executable
                   Application can handle large (>2GB) addresses
                   DLL

Notice that the output now says machine (x64) and Application can handle large (>2GB) addresses, and this also seems to fix the problem:

> raku test-dll.raku
Hello from C

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.