Solved

Max length for PChar

Posted on 2002-06-06
10
1,278 Views
Last Modified: 2006-11-17
Is there a maximum number of characters that can be passwes to a PChar in the following function. I get crashes in Windows NT when it is very long.


function IsPatternWild(pattern: PChar): Boolean;
    begin
      Result := StrScan(pattern,'*') <> nil;
      if not Result then Result := StrScan(pattern,'?') <> nil;
    end;
0
Comment
Question by:moonrise
10 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7059864
What is very long?
Normally PChar C functions have no problems with very long strings.
0
 

Author Comment

by:moonrise
ID: 7059878
4096 characters
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7059893
not true
max length of any allocated memory space is 2 GB !!!
so max length PChar can be 2 GB in size :)
0
 

Author Comment

by:moonrise
ID: 7059907
4096 characters was my answer to what I considered a long string (the length that seems to cause problems in my application but only under Win NT)
0
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 100 total points
ID: 7059997
what version of winnt ?
I have win2k and It works just fine

test code:

procedure TForm1.Button1Click(Sender: TObject);
var pp: PChar;
    dwStart, bufSize: Cardinal;
begin
     bufSize:=4096 * 4096; // 16 megs !!!
     GetMem(pp, bufSize);
     try
        FillChar(pp^, bufSize-1, Ord('x'));
        PChar(pp + bufSize-2)^:='*';
        dwStart:=GetTickCount;
        Caption:=Format('%d - %d', [(Integer(IsPatternWild(pp))), GetTickCount - dwStart]);
     finally
        FreeMem(pp);
     end;
end;
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:moonrise
ID: 7060012
Actually I don't use the getmem and freemem, could that be the problem - but how come it still works most of the time?

Does it make a difference that it is declared as an argument of a function:

function IsPatternWild(pattern: PChar): Boolean;
   begin
     Result := StrScan(pattern,'*') <> nil;
     if not Result then Result := StrScan(pattern,'?') <> nil;
   end;
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7060087
how do you allocate your PChar ?
or where do you get it from
that's most probably the reason for your 'hangs'
0
 
LVL 6

Expert Comment

by:swift99
ID: 7061256
I have run into problems when mixing different families of getmemory and freememory methods.

For example, we had some code that used StrAlloc and FreeMem, and it failed intermittently under D3 but immediately raised exceptions under D6.
0
 
LVL 1

Expert Comment

by:pede
ID: 7066761
Maybe your string isnt always 0-terminated?

You can allocate with a standard Delphi string (eg. SetString or just assign, Str := ...). You can still pass it to your function since just by typecasting. Delphi strings are always 0-terminated.
0
 

Author Comment

by:moonrise
ID: 7066810
It turns out my problem was not with the PChar but rather with adding values into a ComboBox that were too long.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

21 Experts available now in Live!

Get 1:1 Help Now