Lazarus

Programming => General => Topic started by: Shpend on January 14, 2022, 03:42:55 pm

Title: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 14, 2022, 03:42:55 pm
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 ..
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 14, 2022, 04:20:06 pm
Small Addition:

OS: windows 10 x64 latest version (21h2)
FPC: 3.3.1 (trunk) ; x64 setting, O0 optimization
Lazarus 2.3.0 (trunk)
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: 440bx on January 14, 2022, 04:29:37 pm
You're not testing the value returned by GetProcAddress.  That would be the first thing to do, otherwise FuncPtr may be nil.

HTH.
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: engkin on January 14, 2022, 04:41:19 pm
AllocRecord signature?

InputToStrFunc definition?
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 14, 2022, 04:42:33 pm
it is not nil @440bx

it has an arbitrary adress. i see it via the debugger.
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 14, 2022, 04:43:51 pm
here the image
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 14, 2022, 04:44:38 pm
And yea, there is written the "cdecl" but i have written it with sdtcall, then without any conventions, all the same..

here the value:

Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: engkin on January 14, 2022, 05:00:06 pm
Still, AllocRecord signature?
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: engkin on January 14, 2022, 05:03:53 pm
You need to be sure they match.

Same signature, same size Result, same calling convention, and no prerequisites prior to calling.
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 14, 2022, 05:04:48 pm
here
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: engkin on January 14, 2022, 05:37:02 pm
According to this example (https://github.com/3F/Conari/wiki/Examples-Part-1) you should use cdecl
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 14, 2022, 07:14:32 pm
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...
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Akira1364 on January 14, 2022, 08:26:33 pm
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...
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: marcov on January 15, 2022, 12:37:28 am
Dump the tables using objdump or a GUI program like PE explorer.

Check the export tables there.,
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 15, 2022, 01:54:14 am
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  :-[
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 15, 2022, 02:24:28 am
take this image now pls, i have created a new project and did some adjusments, atleast now i get back a valid and found lib and a proc-adress! its no longer nil!

But same issue as my first post: External Exception 0434352  %) %)

Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: engkin on January 15, 2022, 02:28:37 am
32bit or 64bit dll?
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: engkin on January 15, 2022, 02:32:12 am
Would be nice if you explain the adjusments you did?
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 15, 2022, 02:41:13 am
@engkin:

I have to appologize for my stupidness sometimes, I fixed it and made it work :)

I pepega took a x86 .dll (i had it set some ages ago in my setting -.-- and forgot to change it back to x64...) into pascal folder where the default target of the compiler only understands x86...

So yea...

Sry for that, i need to be more careful on the surrounding-settings before i start coding a programm/Idea, I always tend to take that to lightweight and forget the smallest thing when coding are often settings/configurations.
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 15, 2022, 02:42:57 am
Can actually some1 tell me: Why cant FPC understand a x86 library? or execute my function of such a lib/.dll?

since 32-bit is less than 64 ofc, and since im not asking more than the environment allows to, he should be fine right?

i dont understand that quite.
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: engkin on January 15, 2022, 02:51:38 am
Glad it works.

As for your question, it is not an FPC problem. Typically 32bit applications deal 32bit dlls. Same for 64bit apps/dlls.
Title: Re: GetProcAdress returns valid pointer but execution returns: "External exception:"
Post by: Shpend on January 15, 2022, 03:02:25 am
Ok, thx @engkin, apparently this is a deep-OS-thing for conventional benefits or the like I guess..
TinyPortal © 2005-2018