tags:

views:

112

answers:

2

Hi,

Im having problems using data from the host application.
I want to send a string through the dll into the host, then combine it with some data from the host app.

By just including the form in the uses clauses i can use methods to send data into the host, When the data is recived i try to add a lokal variable, this is when i get a access violation:

Host:

procedure TMainForm.DllLink(sMessage: String);
begin
    try
      //This is ok:
      Showmessage(sMessage);
      //This is causes Access error:
      Showmessage(sMessage +sPid);
    except
      Showmessage('Access violation');
    end;
end;

Dll:

procedure Transfer(sMessage: PChar); stdcall;
var
sMyPid : String;
begin
    try
       //Get error if i try to use this method to get sPid which is a string:
       sMyPid := MainForm.GetPid; 
       //Or this:
       MainForm.NextsysDllLink(sMessage);
    except
        showmessage('Error');
    end;    
end;

I dont think the dll is using the running applications forms that is what's causing the access violations (maybe im wrong ?) How do I make the dll aware of a running application(that is its host app.) and use that instance to ether get or manipulate data from itself ?

Im using Delphi 5.

A: 

ShareMem should do the trick.

bassfriend
Im already using ShareMem, thats not the problem. I will try to specify some more. . .
Roise
+1  A: 

It is recommend to not pass native Delphi Objects between Application and DLL boundaries.
If you want to do that you should be using Packages instead of DLLs.

Global variables are not shared between application and Dll.

n your case, your referencing the global mainform variable in the DLL, if you debug that code you will find that mainform = nil or another address that is not the same as the mainform in your host application.

Robert Love
Im building this dll so that a C program can interact with mine, do any one know of any god tutorials for how to make packages that can be used by C or other languages ? ive tried once but ended up with a delphi package.
Roise
You reached the wrong conclusion from Robert's answer, Roise. C programs can't use Delphi packages, and they obviously can't use Delphi object types, so you must go the other way. Don't use any Delphi types in your DLL interface. Restrict yourself to the types you see used in the Windows API: mostly DWord, PChar, and record. If you want to ask a new question (such as one about package tutorials), then please post a new question; comments aren't the place to introduce new topics on Stack Overflow.
Rob Kennedy