Recent

Author Topic: GetProcAdress returns valid pointer but execution returns: "External exception:"  (Read 4559 times)

Shpend

  • Full Member
  • ***
  • Posts: 167
Title.

this is the function and i just cant see why :(

Code: Pascal  [Select][+][-]
  1. function LoadCOMLib: string;
  2.   var
  3.     funcPtr: InputToStrFunc;  //the definition of this is: function():integer; //sometimes even added with stdcall or cdecl still no work..
  4.     path: string;
  5.     handleToLib: HWND;
  6.     funcReturn: integer;
  7.   begin
  8.     GetDir(0, path);
  9.     path := Concat(path, '\', 'UnmanagedExport__TEST.dll');
  10.     handleToLib := LoadLibrary(PChar(path));
  11.     if handleToLib <> NilHandle then
  12.     begin
  13.       try
  14.         funcPtr := InputToStrFunc(GetProcAddress(handleToLib, 'AllocRecord'));
  15.         //funcPtr('TEST', funcReturn);
  16.         funcReturn := funcPtr();
  17.         Result := IntToStr(funcReturn);
  18.         RaiseLastWin32Error;
  19.         //result = Concat(result, 'A');
  20.         //result := 'YEEAAA it works' + IntToStr(funcReturn);
  21.       except
  22.         on e: Exception do
  23.         begin
  24.           FreeLibrary(handleToLib);
  25.           Result := e.Message;
  26.         end;
  27.       end;
  28.     end
  29.     else
  30.       Result := 'Er hat die Library nicht gefunden irgendwie';
  31.   end;            
  32.  

Much appreciation if any1 has a glimpse why.

BTW: i try to import a .NET 6 assembly (with .net 6 features) but there is a project on github allowing do use [DllExport] and i followed everys tep carefully like he does in this video: https://www.youtube.com/watch?v=QXMj9-8XJnY
https://github.com/3F/Conari

Idk ..

Shpend

  • Full Member
  • ***
  • Posts: 167
Small Addition:

OS: windows 10 x64 latest version (21h2)
FPC: 3.3.1 (trunk) ; x64 setting, O0 optimization
Lazarus 2.3.0 (trunk)

440bx

  • Hero Member
  • *****
  • Posts: 3944
You're not testing the value returned by GetProcAddress.  That would be the first thing to do, otherwise FuncPtr may be nil.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

engkin

  • Hero Member
  • *****
  • Posts: 3112
AllocRecord signature?

InputToStrFunc definition?

Shpend

  • Full Member
  • ***
  • Posts: 167
it is not nil @440bx

it has an arbitrary adress. i see it via the debugger.

Shpend

  • Full Member
  • ***
  • Posts: 167
here the image

Shpend

  • Full Member
  • ***
  • Posts: 167
And yea, there is written the "cdecl" but i have written it with sdtcall, then without any conventions, all the same..

here the value:

« Last Edit: January 14, 2022, 04:46:36 pm by Shpend »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Still, AllocRecord signature?

engkin

  • Hero Member
  • *****
  • Posts: 3112
You need to be sure they match.

Same signature, same size Result, same calling convention, and no prerequisites prior to calling.

Shpend

  • Full Member
  • ***
  • Posts: 167
here

engkin

  • Hero Member
  • *****
  • Posts: 3112
According to this example you should use cdecl

Shpend

  • Full Member
  • ***
  • Posts: 167
ok. I have changed it back again to use "cdecl" in function-variable, and i have talked to the guy who provided the "DllExport" attribute, and he suggested me to change some configuration stuff, and now atleast i get this:

(see image below)

why is the address 000000000?

now, indeed, the functionPointer gives back nill... I changed some stuff with his "Configurator-Tool" (u dont have controll over that sadly so u cant give me hints anyway^^) but curious...
« Last Edit: January 14, 2022, 07:19:55 pm by Shpend »

Akira1364

  • Hero Member
  • *****
  • Posts: 561
I think in C#, `int` is always 4 bytes (so equivalent to LongInt, not Int64).

Edit: Also, why are you unconditionally calling `RaiseLastWin32Error`? That will just always raise an exception, and on top of that you're already inside of a try-finally block there...
« Last Edit: January 14, 2022, 08:40:25 pm by Akira1364 »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Dump the tables using objdump or a GUI program like PE explorer.

Check the export tables there.,

Shpend

  • Full Member
  • ***
  • Posts: 167
Dump the tables using objdump or a GUI program like PE explorer.

Check the export tables there.,

see image.

sadly, i have little knowledge of such measurements, would be really nice if you guys could help me understand what it says  :-[

 

TinyPortal © 2005-2018