Board index » delphi » Code problems using pointers

Code problems using pointers

I'm having problems with the following code

procedure RunMacro(MacroName:PChar);

function CdiRunMacro(MacroFile: PChar): integer;
far; external 'CAD1.DLL';

The SpeedButton1Click routine doesn't send Macroname corectly to
RunMacro.  SpeedButton1.hint had cv_help stored in it by doing this

var
MacroName:string;
begin
MacroName:='cv_help'
SpeedButton1.hint:=MacroName;
end;

RunMacro will execute correctly using RunMacro('cv_help')

procedure TPalette1.SpeedButton1Click(Sender: TObject);
var
MacroName:string;
begin
MacroName:=SpeedButton1.hint;
RunMacro(@MacroName);
end;

procedure TPalette1.RunMacro(MacroName:PChar);
begin
CdiPutMsg(MacroName);
case CdiRunMacro(MacroName) of
CDI_ERROR : CdiPutMsg('Error in macro execution');
CDI_OK : CdiPutMsg(MacroName);
end;
end;

Any advice would be appreciated.
Thanks

--
Alexander Medwedew
Computer Ventures, Inc.
compv...@tribeca.ios.com
http://tribeca.ios.com/~compvent/
CADVANCE LITE - Affordable CAD Software
http://tribeca.ios.com/~compvent/cadvlite.html

 

Re:Code problems using pointers


Your problem probably stems from your mixing PChars and strings.
You might want to try doing:

procedure TPalette1.SpeedButton1Click(Sender: TObject);
var
   MacroName: array [0..255] of char;
begin
   StrPLCopy(MacroName,SpeedButton1.hint,SizeOf(MacroName) - 1);
   RunMacro(MacroName);
end;

Quote
Alexander Medwedew (compv...@tribeca.ios.com) wrote:

: I'm having problems with the following code

: procedure RunMacro(MacroName:PChar);

: function CdiRunMacro(MacroFile: PChar): integer;
: far; external 'CAD1.DLL';

: The SpeedButton1Click routine doesn't send Macroname corectly to
: RunMacro.  SpeedButton1.hint had cv_help stored in it by doing this

: var
: MacroName:string;
: begin
: MacroName:='cv_help'
: SpeedButton1.hint:=MacroName;
: end;

: RunMacro will execute correctly using RunMacro('cv_help')

: procedure TPalette1.SpeedButton1Click(Sender: TObject);
: var
: MacroName:string;
: begin
: MacroName:=SpeedButton1.hint;
: RunMacro(@MacroName);
: end;

: procedure TPalette1.RunMacro(MacroName:PChar);
: begin
: CdiPutMsg(MacroName);
: case CdiRunMacro(MacroName) of
: CDI_ERROR : CdiPutMsg('Error in macro execution');
: CDI_OK : CdiPutMsg(MacroName);
: end;
: end;

: Any advice would be appreciated.
: Thanks

: --
: Alexander Medwedew
: Computer Ventures, Inc.
: compv...@tribeca.ios.com
: http://tribeca.ios.com/~compvent/
: CADVANCE LITE - Affordable CAD Software
: http://tribeca.ios.com/~compvent/cadvlite.html

Re:Code problems using pointers


My Guess is that CdiRunMacro() is expecting a NULL Terminated String.  You
pass that as a PChar.  But that is NOT the same as @StringVar.

When you pass it as ('Hello') it works because the compiler fixes it up for
you.

Try Using the stringconversion function
  RunMacro( StrPCopy(szBuf, MacroFile) );

Assume:
Var szBuf: Array [0..128] of Character;

HTH,

Kirk Out

In article <4a9urd$...@news2.ios.com>, compv...@tribeca.ios.com says...

Quote

>I'm having problems with the following code

>procedure RunMacro(MacroName:PChar);

>function CdiRunMacro(MacroFile: PChar): integer;
>far; external 'CAD1.DLL';

>The SpeedButton1Click routine doesn't send Macroname corectly to
>RunMacro.  SpeedButton1.hint had cv_help stored in it by doing this

>var
>MacroName:string;
>begin
>MacroName:='cv_help'
>SpeedButton1.hint:=MacroName;
>end;

>RunMacro will execute correctly using RunMacro('cv_help')

>procedure TPalette1.SpeedButton1Click(Sender: TObject);
>var
>MacroName:string;
>begin
>MacroName:=SpeedButton1.hint;
>RunMacro(@MacroName);
>end;

>procedure TPalette1.RunMacro(MacroName:PChar);
>begin
>CdiPutMsg(MacroName);
>case CdiRunMacro(MacroName) of
>CDI_ERROR : CdiPutMsg('Error in macro execution');
>CDI_OK : CdiPutMsg(MacroName);
>end;
>end;

>Any advice would be appreciated.
>Thanks

>--
>Alexander Medwedew
>Computer Ventures, Inc.
>compv...@tribeca.ios.com
>http://tribeca.ios.com/~compvent/
>CADVANCE LITE - Affordable CAD Software
>http://tribeca.ios.com/~compvent/cadvlite.html

--
___________________________________________________________________________
Kirk Wolak
KWo...@mail.cbf.com
//
// Const ViewsExpressed = "My own and not those of the company I work for!";
//

Other Threads