Link to home
Start Free TrialLog in
Avatar of sudhakar_koundinya
sudhakar_koundinya

asked on

What is the Problem in this code?

Hi All

I am new to delphi coing.

Please check this code. And please explain me where i am doing mistakes

Dll Code:

library sudhakar;


uses
  ShareMem,
  Windows,
  SysUtils,
  Classes,
  Axctrls,
  ActiveX,
  Dialogs,
  ShellApi
;
{$R *.RES}

procedure tester(x:Char;var y:PChar;alloc: longInt);
var
  xx:longint;
  zzz:PChar;
begin
  try
    //GetMem(y,alloc);
    y:='hello';
    showmessage(y);
  except
  end;
end;
exports
 tester;
begin

end.













Application Code:

unit Unit111;

interface

uses
  ShareMem,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;
procedure tester(x:Char;var y:PChar;alloc: longInt);stdcall;external 'c:\windows\desktop\test\sudhakar.Dll' ;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
xx: PChar;
yy: Char;
begin
try
        yy:='a';
        GetMem(xx,101);
        tester(yy,xx,110);
        showmessage(xx);
except
end;
end;

end.




what is the problem in the above code?

Actually my requirement is i want to manage external PChar Data in dll and i am facing problems and is giving memory access voilation exception.

i tried to allocate the memory for pchar data external to dll and with in dll, but both are giving memory access voilation problems with different results.I am not understanding whare i am doing mistake.




Thanks in advance
Sudhakar Koundinya

ASKER CERTIFIED SOLUTION
Avatar of DragonSlayer
DragonSlayer
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sudhakar_koundinya
sudhakar_koundinya

ASKER

Hi all

I solved this problem
by defining the procedure in dll as

procedure tester(x:Char;var y:PChar;alloc: longInt);cdecl;

but after accessing the data in external application

i am getting following system message


SUDHAKARTEST caused an invalid page fault in
module KERNEL32.DLL at 017f:bff99b2d.
Registers:
EAX=00000001 CS=017f EIP=bff99b2d EFLGS=00010246
EBX=819c705c SS=0187 ESP=005f0000 EBP=005f0058
ECX=c16da6b0 DS=0187 ESI=00000000 FS=53c7
EDX=0000000e ES=0187 EDI=81997180 GS=0000
Bytes at CS:EIP:
e8 a2 78 fd ff 3d c0 00 00 00 8b f0 75 05 e8 ec
Stack dump:
002a005c c166be20 ffffffff 00000000 819970f0 bff7c8b3 c166be20 ffffffff 00000000 bff957b1 c166be20 ffffffff 00000000 005f0150 bff887ee 819970f0


If i try to free the memory also, it is giving same problem


Please post the comments on this

Thanks
Koundinya



Hi DragonSlayer

I Solved the problem before you posted this answer.

any way Thanks for your help, and as you gave me the answer before i posted my comment, i am ready to give the points

thanks
Sudhakar
Hi I solved the above roblem also

may me it is internal system problem because, it is working in other systems with no problem

thanks and cheers
Sudhakar
no probs :)