[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

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
0
hagur
Asked:
hagur
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now