Trouble loading Port Drivers (on Windows)

Hi,

I was trying to run the port driver example but am not having much luck loading the driver.

It fails to load the driver. I tried running load_driver from the shell and get {error,{open_error,-203}}. Translating that with erl_ddll:format_error({open_error, -203}), gives 'Unspecified error'. It is finding the .dll, because if I put the wrong name in it says 'The specified module could not be found.'

I tried setting ERL_LIBS to include this directory.
I tried setting the below as per this post:

ERL_INTERFACE_INCLUDE_DIR="c:/Program Files/erl-24.1.2/lib/erl_interface-5.1/include"
ERL_INTERFACE_LIB_DIR="c:/Program Files/erl-24.1.2/lib/erl_interface-5.1/lib"
ERTS_INCLUDE_DIR="c:/Program Files/erl-24.1.2/erts-12.1.2/include"```

I tried adding #define __WIN32__ to the c file.

I tried using the echo_drv from erlang tests instead, with the same result.

I tried adding the path as per this SO with no luck.

start(SharedLib) ->
    {ok,Cwd} = file:get_cwd(),
    code:add_path(Cwd),
    case erl_ddll:load_driver(Cwd, SharedLib) of
			ok -> ok;
			{error, already_loaded} -> ok;
			{error, Error} -> 
				erlang:display(erl_ddll:format_error(Error)),
				exit({error, could_not_load_driver})
    end,
    spawn(?MODULE, init, [SharedLib]).

Running .\dumpbin.exe /dependents example_drv.dll gives:

Microsoft (R) COFF/PE Dumper Version 14.27.29112.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file C:\Users\simon\code\ctapinif\example_drv.dll

File Type: DLL

  Image has the following dependencies:

    KERNEL32.dll
    VCRUNTIME140.dll
    api-ms-win-crt-runtime-l1-1-0.dll

  Summary

        1000 .data
        1000 .rdata
        1000 .reloc
        1000 .text

I tried copying them to the cwd. Is there anything I need to do with regards to the dependant dlls?

As an aside,
In port_driver.c, the includes are:

#include <stdio.h>
#include "erl_driver.h"

which means you need to include the path to erl_driver.h and cl in VS 2019 docs use / instead of -, so I ended running the following in VS2019 dev console:
cl /I"C:\Program Files\erl-24.1.2\usr\include" /LD /MD /Feexample_drv.dll complex.c port_driver.c

2 Likes

I’m not very good at windows (maybe @dgud can help you), but I think that -203 translates to error code 193 which is ERROR_BAD_EXE_FORMAT. Maybe searching for that error will guide you to what is going on?

3 Likes

Works for me… I only have OTP-24.0 installed. and using
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29335 for x64

No changes where made to the source files, I just copied complex.c and port_driver.c

This is using cl.exe from 64b setup i.e.

X64:/mnt/e/temp/test:which cl.exe
/mnt/c/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64//cl.exe

cl.exe -LD -MD -Feexample_drv.dll complex.c port_driver.c -I"c:/Program Files/erl-24.0/usr/include"
werl.exe
> erl_ddll:load_driver("e:/temp/test", example_drv).
ok

3 Likes

I was using the Developer Command Prompt for Visual Studio 2019.
Works in the x64 Native Tools... one. :man_facepalming:

Thanks :slight_smile:

4 Likes

Well that’s… interesting… Was it trying to link in a debug CRT or something from the developer tools or something weird?

1 Like

Not sure on the specifics, I’m a novice in the C space. Judging from the ms docs. I was compiling from the 32bit space and running Erlang in 64.

2 Likes

Oh yeah that would absolutely not work. Windows (like mac and linux) don’t support linking 32-bit and 64-bit binaries in the same process space. So that makes sense then.

1 Like