PByte to PAnsiChar Conversion

Hi,
I'm migrating a system from Delphi 7 to Delphi XE4 and running into all sorts of conversion problems.
Is there a conversion function for converting PByte to PAnsiChar?
Many thanks
Vector7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pierre CorneliusCommented:
you could just typecast it e.g. PAnsiChar(some_byte_var)

or you could do something like:


var b: byte;
  pb: pbyte;
  pc: PAnsiChar;
begin
  b:= 65;
  pb:= @b; //pb^ will be 65
  pc:= @b;  //pc^ will be 'A'
...
0
Vector7Author Commented:
Hi,

Thanks for your reply.  The problem is that only the first character of 'Value' (code below) is being saved in the statement e.g. a 'Value' of 2048 is saved as 2.

          Check(DbiCfgModifyRecord(hCfg, P, ParamCount, pFld, pRec));


Herewith the code:-



var hCfg: hDBICfg;
    Config: SYSConfig;
    Path, Option: string;
    ParamCount, i: word;
    pFields, pFld: pFLDDesc;
    pRecBuf, pRec: pBYTE;
    Found, SelfInitialized: Boolean;
    rslt: DBIResult;
    P: PAnsiChar;
 


   pFields := AllocMem(ParamCount * sizeof(FLDDesc));
     pFld := pFields;
     pRecBuf := AllocMem(10000);
     pRec := pRecBuf;

     {Get the node values... }
     Check(DbiCfgGetRecord(hCfg, StrToPAnsiChar(Path), ParamCount, pFields, pRecBuf));

     for i := 0 to ParamCount - 1 do
        begin
        if pFields^.szName = Option then
           begin
           StrPCopy(PAnsiChar(pRecBuf), Value);
           P := StrToPAnsiChar(Path);
           Check(DbiCfgModifyRecord(hCfg, P, ParamCount, pFld, pRec));
           Found := True;
           end;

        Inc(pFields);
        Inc(pRecBuf, 128);
        end;
0
Pierre CorneliusCommented:
It seems to me your original question is not exactly what you are after. Your question was if and how to convert a pbyte to pAnsiChar - which I believe I answered.

Just remember that a PByte or a PAnsichar are merely pointers. PByte points to an address and if you dereference it, it will give you the byte value at that address. The same goes for PAnsichar i.e. if you dereference it it will give you the AnsiChar at that address. Regarding your value of 2048, this can't be stored in a single byte (max is 255).

From your code, it looks like DbiCfgGetRecord returns a pointer to the fields (pFields) and its values as a buffer (pRecBuf).

Looking at your loop through the paramcount it seems that the buffer allocates 128 bytes for each field/parameter value (see: Inc(pRecBuf, 128); ).

It looks like what you are trying to do is set the pRecBuf (the parameter value buffer) to contain the value you pass, is this right? What is "Value" defined as? this may be your problem. try: StrPCopy(PAnsiChar(pRecBuf), AnsiString(Value));
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Pierre CorneliusCommented:
Can you post your function declaration? In Delphi7 the string type equates to a AnsiString whereas in XE4 it equates to a Unicode string. I think therein lies your problem.

So you may have to change your function from something like:
procedure YourFunc(... value: string);

to:
procedure YourFunc(... value: AnsiString);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pierre CorneliusCommented:
Why a b grade?
0
Pierre CorneliusCommented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Network Management

From novice to tech pro — start learning today.