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;
  TempProc : TGetDllInfo;
  @TempProc := GetProcAddress(Handle, pChar('DllInfo'));
  if Assigned(TempProc) then
     Result := TempProc
    Raise Exception.Create('(GetDllInfo)[GetProcess Fail] GetLastError reports: ' + IntToStr(GetLastError));

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

And this is from the dll :

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


Best regards,
Thorgeir Omarsson
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.

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.

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
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).
hagurAuthor Commented:
I used pChar and everything worked. Thanks
hagurAuthor Commented:
Now I'm having the same problem with TBitmap any solution to that one other then sharemem?
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?
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

From novice to tech pro — start learning today.