skip to Main Content

I’m trying to replace some functions made in LazarusLib with functions written in C++ for Inno Setup installer.

So I created a Dynamic-Link Library (DLL) project in Visual studio and added the following code:

InstallerFunctions.h:

extern "C" {
    __declspec(dllexport) bool __stdcall CheckPortAvailable(int Port);
}

InstallerFunctions.cpp:

#include "pch.h"
#include "framework.h"
#include "InstallerFunctions.h"
#include <windows.h>
#include <winsock.h>
#include <string>
#include <fstream>

bool __stdcall CheckPortAvailable(int Port) {
    WSADATA WSAData;
    SOCKET Sock;
    sockaddr_in SockAddr;
    int ErrorCode;

    if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)
        return false;

    Sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (Sock == INVALID_SOCKET)
        return false;

    ZeroMemory(&SockAddr, sizeof(SockAddr));
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = INADDR_ANY;
    SockAddr.sin_port = htons(Port);

    ErrorCode = bind(Sock, reinterpret_cast<sockaddr*>(&SockAddr), sizeof(SockAddr));
    closesocket(Sock);

    WSACleanup();

    return (ErrorCode != SOCKET_ERROR);
}

This project builds successfully. The project is configured to output the DLL file to Output
Then I added this to Inno Setup script:

[Files]
Source: "..InstallerFunctionsOutputInstallerFunctions.dll"; DestDir: "{app}"; Flags: ignoreversion

The path is correct. Then I declare the function as following:

function CheckPortAvailable(Port: Integer): Boolean;
external 'CheckPortAvailable@files:InstallerFunctions.dll stdcall delayload';

When I try to call CheckPortAvailable I get the following error:
Could not call proc.

I tried to use DLL explorer to find the functions I require and DLL explorers are able to find the functions. Any idea what else can I try?

Edit:
It appears that the function is recognized, so the issue defenitly lies in C++ project:

2

Answers


  1. Chosen as BEST ANSWER

    It seems that the main issue is that the function name is different than expected: enter image description here

    Left side is from LazarusLib, right side is from Visual Studio C++ project. I'm still not sure why it's named differently and how to fix it, but changing the exported function name in Inno Setup, fixes the issue.


  2. My guess is that your .dll file is not present on machine at the time you call the exported function from it.

    [Files] section is processed when Install page is shown (that is when installer is processing the section and copying the files and so one).

    I suppose you are calling your .dll function earlier, so solution for you might be to extract the .dll into the Temp folder at installer startup so the exported fucntion is available.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search