Solved

What is the Problem in this code?

Posted on 2002-04-23
5
155 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
no probs :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now