Dll question..

What is wrong with this code?
I get right data from the GetDllInfo function
but an access violation when RecordDllInfo finish.

this is from the application :

tGetDllInfo = Function : String;

Function GetDllInfo(Handle : THandle):String;
Var
  TempProc : TGetDllInfo;
Begin
  @TempProc := GetProcAddress(Handle, pChar('DllInfo'));
  if Assigned(TempProc) then
     Result := TempProc
  else
    Raise Exception.Create('(GetDllInfo)[GetProcess Fail] GetLastError reports: ' + IntToStr(GetLastError));
end;

Procedure RecordDllInfo(FileName : String);
var
  DllHandle : tHandle;
  TempStr : String;
Begin
  DllHandle := LoadLibrary(pChar(FileName));
  TempStr := GetDllInfo(DllHandle);
  if DLLHandle <> 0 then FreeLibrary(DLLHandle);
end;

And this is from the dll :

Function DllInfo : String;
Begin
  Result := 'iNFO';
end;

exports
    DllInfo;

Best regards,
Thorgeir Omarsson
LVL 3
hagurAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MadshiCommented:
You need to use Borland's ShareMem unit if you want to pass strings from your dll to your application or vice versa. Look, in your example the Dll allocates memory for the info string. The application gets this string and tries to deallocate it when leaving the RecordDllInfo function. But the application does not know how to deallocate the string, because the string was allocated by the dll. Both the dll and the application have their own memory manager.
If you use Borland's ShareMem unit, you need to distribute the dll "borlndmm.dll" with your application. Then both your application and your dll use the memory manager in this borlndmm.dll. As a result the application knows how to free the string that the dll has allocated.
Everything clear?   :-)

Regards, Madshi.

P.S: Another way around this problem is to use pchar instead of string.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MadshiCommented:
Hmmm... If you decide to use ShareMem instead of changing the types to pchar, you need to add ShareMem as the VERY FIRST unit to the uses clause of your application's and dll's project files (*.dpr).
0
hagurAuthor Commented:
I used pChar and everything worked. Thanks
0
hagurAuthor Commented:
Now I'm having the same problem with TBitmap any solution to that one other then sharemem?
0
MadshiCommented:
Hmm... You can *USE* a bitmap that was created in the app, in the dll, too. But what you can't do is *FREE* a bitmap in the dll, that was created in the application (or vice versa). If you need to do that, you need to use ShareMem. But what's the big problem about using ShareMem?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.