Board index » delphi » procedure x(var s:string) or procedure x(s:pchar)

procedure x(var s:string) or procedure x(s:pchar)

Hi all,

My first problem is I can't write in english very well...

I created a dll. In this dll I defined a procedure

procedure x( s:string); stdcall; external;
begin
    s:='qualquer coisa'; // any text
end;

but when I use this function the variable 's' doesn't change its value;
To correct this I redefined the procedure

procedure x(var s:string); stdcall; external;
// or
procedure x(s:pchar); stdcall; external;
begin
    s:='qualquer coisa'; // any text
end;

but in both cases a have an Access Violation error.
Does Anyone can help me?

Tia
Clair Rios
cl...@ucb.br

 

Re:procedure x(var s:string) or procedure x(s:pchar)


{dll's source}
library mydll;
...
procedure x(var s1, s2:string); stdcall; export;
begin
    s1:='text1';
    s2:='text2';
end;
exports
    x index 1;
begin
end.

unit test;
...
procedure x(var s1, s2:string); stdcall; external 'mydll.dll' index 1;
{my first error was in line above, I didn't put the word "var"}

procedure tform1.buttonclick(sender:tobject)
var
    l1,l2:string;
begin
    x(l1, l2);
    showmessage(l1);
    showmessage(l2);
end;
...

Now the application raise an exception ('Invalid pointer operation')
followed by an access violation when I close the application.
I still need help.

tia

Clair
cl...@ucb.br

Quote

Re:procedure x(var s:string) or procedure x(s:pchar)


library dll_file;

procedure x(var s:string);
begin
s:='any text';
end;

export x

end.

Re:procedure x(var s:string) or procedure x(s:pchar)


Quote
Clair wrote:
> I created a dll. In this dll I defined a procedure

> procedure x( s:string); stdcall; external;
> begin
>     s:='qualquer coisa'; // any text
> end;

> but when I use this function the variable 's' doesn't change its value;
> To correct this I redefined the procedure

> procedure x(var s:string); stdcall; external;
> // or
> procedure x(s:pchar); stdcall; external;
> begin
>     s:='qualquer coisa'; // any text
> end;

> but in both cases a have an Access Violation error.
> Does Anyone can help me?

Did you include the ShareMem unit? If not, read the corresponding online
help topic.

Greetings, Robert
--
Robert Ro?mair
http://home.t-online.de/home/Robert.Rossmair/
Programming environment: Delphi 3.0, WinNT 4.0 SP5

Re:procedure x(var s:string) or procedure x(s:pchar)


I include the ShareMem unit today. Now I get the error
"Run time error at 000044A2"

Robert Rossmair escreveu na mensagem <37A622C3.17967...@t-online.de>...

Quote

>Did you include the ShareMem unit? If not, read the corresponding online
>help topic.

>Greetings, Robert
>--
>Robert Ro?mair
>http://home.t-online.de/home/Robert.Rossmair/
>Programming environment: Delphi 3.0, WinNT 4.0 SP5

Re:procedure x(var s:string) or procedure x(s:pchar)


Quote
Clair wrote:
> I include the ShareMem unit today. Now I get the error
> "Run time error at 000044A2"

Maybe you didn't include ShareMem the right way? See below:

  Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  View-Project Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the DELPHIMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using DELPHIMM.DLL, pass string information
  using PChar or ShortString parameters.

- Robert

Re:procedure x(var s:string) or procedure x(s:pchar)


Did you make it the first unit in the uses clause of both DLL and app?

MH.

Quote
Clair wrote:

> I include the ShareMem unit today. Now I get the error
> "Run time error at 000044A2"

> Robert Rossmair escreveu na mensagem <37A622C3.17967...@t-online.de>...

> >Did you include the ShareMem unit? If not, read the corresponding online
> >help topic.

> >Greetings, Robert
> >--
> >Robert Ro?mair
> >http://home.t-online.de/home/Robert.Rossmair/
> >Programming environment: Delphi 3.0, WinNT 4.0 SP5

--
Martin Harvey.
mar...@aziraphale.demon.co.uk
mc...@harvey27.demon.co.uk
http://www.harvey27.demon.co.uk/mch24/

Re:procedure x(var s:string) or procedure x(s:pchar)


Martin Harvey escreveu na mensagem
<37A77FCC.7DCD2...@aziraphale.demon.co.uk>...

Quote

>Did you make it the first unit in the uses clause of both DLL and app?

>MH.

Yes, I did.

Re:procedure x(var s:string) or procedure x(s:pchar)


Just to be sure I tested the code that you have posted. With the
ShareMem unit included it worked.

- Robert

Re:procedure x(var s:string) or procedure x(s:pchar)


I did the same test. I created a new dll and a new application, copied the
code that I have posted and included sharemem unit (first unit declared in
uses clause (dll and app)). Then I compiled and ran the program. It work,
but when I try to close the application I get the messages:
'Runtime error 217 at 00002AEA' and
'Runtime error 216 at 000044CA'
To close the application I have to press ctrl+F2 in Delphi IDE.

I'm using D3 and NT 4.0 workstation.

Clair

Robert Rossmair escreveu na mensagem <37A832E3.71DF4...@t-online.de>...

Quote
>Just to be sure I tested the code that you have posted. With the
>ShareMem unit included it worked.

>- Robert

Re:procedure x(var s:string) or procedure x(s:pchar)


Hi Clair,

Quote
you wrote:
> I did the same test. I created a new dll and a new application, copied the
> code that I have posted and included sharemem unit (first unit declared in
> uses clause (dll and app)). Then I compiled and ran the program. It work,
> but when I try to close the application I get the messages:
> 'Runtime error 217 at 00002AEA' and
> 'Runtime error 216 at 000044CA'
> To close the application I have to press ctrl+F2 in Delphi IDE.

> I'm using D3 and NT 4.0 workstation.

I'm using D3 under WinNT 4.0 (SP5), too, and, as said before, no such
problems.
I suggest you send me your complete test project source code (.DOF,
.DPR, .PAS, DLL-Source) per e-mail and I try to find the error.

- Robert

Other Threads