Solved

SHFileOperation and Null terminated strings

Posted on 1998-02-18
8
294 Views
Last Modified: 2012-05-04
I am trying to use SHFileOperation to copy multiple files.  I am having difficulty constructing the pFrom string which requires the file names be seperated by a null character and if more than one file the whole string needs to be terminated with two null characters.  I originally did this program in Visual Basic 5 and it was real easy.  I need a stand-alone executable which is why I am learning Delphi.
0
Comment
Question by:rmbec
  • 3
  • 3
  • 2
8 Comments
 
LVL 2

Expert Comment

by:Waldek
ID: 1359561
Create string pFrom like filename1+#0+filename2+#0+...+#0#0
0
 

Author Comment

by:rmbec
ID: 1359562
That is what I tried first.
When I do that filename2 does not get appended.
Are pFrom, filename1, etc all type string?
0
 
LVL 2

Expert Comment

by:Waldek
ID: 1359563
pFrom must be a string I mean
0
 
LVL 5

Accepted Solution

by:
ronit051397 earned 70 total points
ID: 1359564
1. Place 2 TEdit's and a button in a form.
2. In Edit1 write the full path and file names of the file you want to copy, each is seperated by ';'.
3. In Edit2 write the destination Path/s, seperated by ';'.
4. Copy the code bellow and press the button.

uses ShellApi;

procedure TForm1.Button1Click(Sender: TObject);
var
  lpFile: TSHFileOpStruct;
  FFrom, FTo: string;
  pFrom, pTo: PChar;
  i: Integer;
begin
  with lpFile do
  begin
    Wnd:=Handle;
    wFunc := FO_COPY;
    FFrom := Edit1.Text + #0#0;
    for i := 1 to Length(FFrom) do if FFrom[i] = ';' then FFrom[i] := #0;
    pFrom := PChar(FFrom);
    FTo   := edit2.text + #0#0;
    for i := 1 to Length(FTo) do if FTo[i] = ';' then FTo[i] := #0;
    pTo   := PChar(FTo);
  end;
  SHFileOperation(lpFileOp);
end;

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:ronit051397
ID: 1359565
The last line should be SHFileOperation(lpFile);
0
 

Author Comment

by:rmbec
ID: 1359566
That won't work. As soon as I add the #0 to the null-terminated string it terminates the string.  The next strings do not get appended.  I've tried all sorts of ways to build this.  The closest I come is using an Character array but I need to dimension it after I calculate how many characters there are.
0
 
LVL 2

Expert Comment

by:Waldek
ID: 1359567
pFrom must be a string
0
 

Author Comment

by:rmbec
ID: 1359568
ronit;
My apologies, your method does work. I don;t know what I did earlier to cause it not to work.  I am not working with edit boxes, nor do I have control over file names so I can't use the ';' character since it is allowed in a file name.  I use a '?' instead. I certainly learned alot about how Delphi handles strings, arrays, pointers and memory blocks during my journey here.  Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

12 Experts available now in Live!

Get 1:1 Help Now