Link to home
Start Free TrialLog in
Avatar of cebasso
cebassoFlag for United States of America

asked on

Stupid Question (LoadLibrary)

Hello,

This a very stupid quesiton (maybe)... but, i prefer to ask! hehe

Example

TSomethingW = record
  FuncW: function (Something: PWideChar): Boolean; StdCall;
  hLib: THandle;
end;
PSomethingW = ^TSomethingW;

TSomethingA = record
  FuncA: function (Something: PAnsiChar): Boolean; StdCall;
  hLib: THandle;
end;
PSomethingA = ^TSomethingA;

var
  SomethingW: PSomethingW;
  SomethingA: PSomethingA;
begin
  New(SomethingW);
  FillChar(SomethingW^, SizeOf(TSomethingW), 0);
  //looking for unicode function, if supported
  SomethingW^.hLib := LoadLibrary(somedll.dll);
  if LongBool(SomethingW^.hLib) then
  begin
    SomethingW^.FuncW := GetProcAddress(SomethingW^.hLib, 'SomethingW');

    //now we check if there is unicode support for this DLL, if not, its ANSI only
    if SomethingW^.FuncW = nil then
    begin
      //its ANSI and supposed to have a "SomethingA" exported func to work with ansi
      //but, i need to FreeLibrary for SomethingW^ and LoadLibrary for SomethingA or i can simple
      New(SomethingA);
      FillChar(SomethingA^, SizeOf(TSomethingA), 0);
      SomethingA^.hInstance := SomethingW^.hInstance; //?!?!?!?!?
      Dispose(SomethingW); //freeing
  end;

i need to freelibrary for somethingW and loadlibrary for somethingA or i can just pass the Handle of loaded library to SomethingA?

Regards,
Carlos
Avatar of systan
systan
Flag of Philippines image

So thus the code has a problem for you?   I see your code is Ok.
but Im not sure  if you can pass the load from w to a with this code, and why are you doing this? for what reason?
SomethingA^.hInstance := SomethingW^.hInstance;
Avatar of cebasso

ASKER

Hello!
My program has a plugin framework... i have a struct/record for ANSI and WIDE(Unicode)

For old plugins, its ANSI and new plugins WIDE
For backward compatibility i need to support ANSI yet
So, with a new version of the engine, first i check if the plugin was upgraded to WIDE, if not, i use the old Struct (ANSI)
But, i load it from a StructW = WIDE, if there is no unicode support, then i load the old structA = ANSI... my doubt is, if i can just pass the loaded handle to the StructA^.hLib and use to call the plugin, or if i need to FreeLibrary at the StructW^.hLib and load for StructA^.hLib like my example above!
I guess you can't, Delphi doesn't support automatic type conversions betwwen Ansi and Wide string types.  Someone correct me if I'm wrong.
Avatar of cebasso

ASKER

i think that you dont understood hehe
but no problem, i found a way!
i can't gave you the points, i'm sorry...
thank you anyway!
Cheers!
ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Answer is you don't need to unload the library, look at my last comment.
Sorry typo

function SupportsUnicode(AHandle: THandle; const AFunctionName: string): Boolean;
var
  FunctPointer: Pointer;
begin
  FunctPointer := GetProcAddress(AHandle, PChar(AFunctionName));
  Result := FunctPointer <> nil;
end;
Avatar of cebasso

ASKER

Hey thanks!

But i still need a hLib var at the Struct since it will be added in a TList, so latelly i can know the instance of the plugin :D
But you answered my question, if its needed to unload in StructW first and re-load for StructA.

Cheers!
Avatar of cebasso

ASKER

@systan thank you too for starting to help me!

Best Regards!