Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What is the Problem in this code?

Posted on 2002-04-23
5
Medium Priority
?
162 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 150 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

618 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