DLL written in Delphi cannot be loaded in C++

I have a dll written in Delphi that I need to call from a c++ program. My dll works when called from Delphi, both when using static and dynamic loading. However, in c++, LoadLibrary returns a null pointer. In both cases, test.dll is located in the same dir as the test program. I am a c++ beginner - any help is appreciated.

The dll:
library test;

uses
  System.SysUtils,
  System.Classes;

{$R *.res}
 function AddIntegers(_a, _b: integer): integer; stdcall;
begin
  Result := _a + _b;
end;

exports
  AddIntegers;

begin
end.

Open in new window


The Delphi test program - this return the line "3 + 4 is 7", as expected:
program SimpleDLLTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, Winapi.Windows;

type
  TAddIntegersFunc = function (_a, _b: integer): integer; stdcall;
var
  DllHandle : HMODULE;
  AddIntegersFunc : TAddIntegersFunc;
  TestInt         : integer;
begin
  try
    DllHandle := LoadLibrary(pWideChar('test.dll'));
    if DllHandle = 0 then begin
      Writeln('Error loading dll');
    end else begin
      @AddIntegersFunc := GetProcAddress(DllHandle, 'AddIntegers');
      if assigned(AddIntegersFunc) then begin
        TestInt := AddIntegersFunc(3,4);
        Writeln('3 + 4 is ' + IntToStr(TestInt));
      end else begin
        Writeln('Function not found');
      end;
    end;
    Write('Press Enter'); ReadLn;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Open in new window


The c++ test program:
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
	HINSTANCE hGetProcIDDLL = LoadLibrary("test.dll");

	if (!hGetProcIDDLL)
	{
		cout<<"Could not load library!\n";
		cin.get();
	}
	else
	{
		cout<<"Library loaded!\n";
		cin.get();
	}

	return 0;
}

Open in new window


I did not write any function call in c++, since I have not yet even managed to load the library - I just get "Could not load library".
Stinne HøstDeveloperAsked:
Who is Participating?
 
ZoppoConnect With a Mentor Commented:
Hi Stinne Høst,

my first guess is the DLL isn't found - are you sure the DLL is in a path where you test progam can find it? It should be either in the same directory where the test-program's EXE is or in any path defined in PATH evironment variable.

Anyway: You should call GetLatestError() to see the error code set in LoadLibrary, i.e.:
		cout<<"Could not load library "(ec: " << GetLastError() << ")!\n";

Open in new window


You should see a none-Null number - if you have Visual Studio there's a small tool to show information about error codes (Menue Tools->Error Lookup), otherwise you can search for error information at https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

Hope that helps,

ZOPPO
0
 
Stinne HøstDeveloperAuthor Commented:
Hi Zoppo
The dll is located correctly, but the GetLastError helped - it was a 32 bit dll and a 64 bit program. Thanks a lot!!
Stinne
0
 
sarabandeCommented:
 it was a 32 bit dll and a 64 bit program

Open in new window


then, you have to convert either the one or the other.

i would recommend to get visual studio community 2017 (which is free) and then create a 64-bit dll project.

Sara
0
 
Stinne HøstDeveloperAuthor Commented:
@Sara: I know I have to convert one or the other. I just hadn't realised that was the problem. Thanks for answering!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.