Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with passing a record to dll

Status
Not open for further replies.

Juhas0

Programmer
May 6, 2008
10
PL
Hi, I've got a problem with passing a record to dll.

I do it like this:

[tt][blue]
type
TMyRec = record
field1: PChar;
field2: integer;
end;

type
PMojRec = ^TMojRec;

//of course in my application there is more fields
[/tt][/blue]


Then in main application I create array of those records and fill them in:

[tt][blue]
var
devArr: array of TMyRec;
begin
setLength(devArr, 1);
devArr[0].field1:=PChar('blabla');

function_from_dll(@devArr[0]);
end;
[/tt][/blue]


And in dll:

[tt][blue]
function function_from_dll(MyRec: PMyRec): HResult; safecall;
var
s: string;
begin
s:=MyRec^.field1; //and here I get AccessViolation
end;

[/tt][/blue]

I think that I am doing everything the way it should be, but this AV still appears. WHY?
 
the problem is that the DLL cannot access the memory of your process. you need a memory manager to share memory between your DLL and application. there are several ways to achieve this, search "delphi ipc communication" and "delphi shared memory" to find some samples.

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Yes, adding "sharemem" to both the DLL and the main program or using some other solution of the like will solve this. DLLs can only receive rudimentary data types and not extended ones when not using "sharemem".

Measurement is not management.
 
But if I do it using shareMem, I won't be able to write dlls in other languages, will I?
 
nope, that's why you need to seek other methods.
A memory mapped file for example...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Haha! That's just not true :)

The only thing that I had to do was to allocate memory for the PChar field:

Code:
GetMem(myrec.field1, length(str_which_I_assign));
myrec.field1:=PChar(str_which_I_assign);

And then I can pass the pointer to myRec to the dll :)
 
correct, as long that the DLL is used in the same process...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top