Solved

What is the Problem in this code?

Posted on 2002-04-23
5
159 Views
Last Modified: 2010-04-04
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

0
Comment
Question by:sudhakar_koundinya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 50 total points
ID: 6964815
add stdcall to the end of the declaration

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


HTH
DragonSlayer
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 6964817
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



0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 6964833
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
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 6964849
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
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6964863
no probs :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question