Solved

What is the Problem in this code?

Posted on 2002-04-23
5
156 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 87
Downloading email attachments 2 73
Tembedded WB animatid gifs not animated on some pcs 2 79
TEMBEDDEDWB how can i change its user agent ? 8 58
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

785 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