winAPI

This is a discussion on winAPI within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, In the code below, I want to call windows API to delete a file to recycle bin. This ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    winAPI

    Hi all,

    In the code below, I want to call windows API to delete a file to recycle bin. This is my first try to use winAPI. Please, can anybody tell me where is the error?

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    
    int main()
    {
     SHFILEOPSTRUCTA fileOp;
    
     fileOp.wFunc = FO_DELETE;
     fileOp.pFrom = "D:\\Documents and Settings\\Book1\\0\\0";
     fileOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
    
     if (!SHFileOperation(&fileOp))
      printf("it is DONE!");
     else
      printf("Unwanted error happened!");
     return 0;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Could you describe the error?
    Do you have compilation erros?
    Run=time errors?
    Unwanted output?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by vart View Post
    Could you describe the error?
    Do you have compilation erros?
    Run=time errors?
    Unwanted output?
    Actaully none of the obove. It outputs the "Unwanted error happened" which is the condition of not satisfying the first "if". Any help?

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    if(!SHFileOperation(&fileOp))
      printf("Unwanted error happened!");
    
     else
      printf("it is DONE!");
    Last edited by The Brain; 06-02-2007 at 03:24 PM. Reason: cut n' paste tragedy.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    See if calling GetLastError() yeilds anything useful. That, and zero out fileOp to be sure you're not passing garbage to SHFileOperation():
    Code:
    SHFILEOPSTRUCTA fileOp = {0};
    EDIT: @The Brain: Code that doesn't compile?
    Last edited by Cactus_Hugger; 06-02-2007 at 01:12 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    I added the piece of the code that you suggested, and I got the following msg:

    Cannot delete a file: Cannot read from the source file or disk.

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Try this instead:
    Code:
     fileOp.pFrom = "D:\Documents and Settings\Book1\\0\\0";
    Last edited by abachler; 06-04-2007 at 11:51 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  8. #8
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    fileOp.pFrom = "D:\Documents and Settings\Book1\\0\\0";
    Neither \D or \B is a valid escape sequence.

    Rereading the Win32 API, I must have missed this in my first post:
    pFrom

    Pointer to a buffer that specifies one or more source file names. Multiple names must be null-separated. The list of names must be double null-terminated.
    You're not correctly giving a double null at the end of your string. This:
    Code:
    "D:\\Documents and Settings\\Book1\\0\\0"
    Translates to the string containing:
    Code:
    D:\Documents and Settings\Book1\0\0(NUL)
    First, it isn't double null (you may be lucky that the next byte is 0), and you're deleting a file called "0", probably not what you wanted. Try:
    Code:
    fileOp.pFrom = "D:\\Documents and Settings\\Book1\0";
    (The string literal provides another null, for a total of two.) This for me will delete a file called "Book1".
    Last edited by Cactus_Hugger; 06-04-2007 at 04:58 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    I liked your post, it is really helpful, Cactus_Hugger.

    You are right, it works now. I noticed that sometimes we can use \0 or \*? Compiling my code under Cygwin with gcc compiler works well with \*, but when I compile it with Microsoft visual studio compiler it works well with \0. Any thoughts?

    Another question: I want to add some codes that enable me to delete the directories. What are the flages that I should add || remove?

  10. #10
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I noticed that sometimes we can use \0 or \*? Compiling my code under Cygwin with gcc compiler works well with \*, but when I compile it with Microsoft visual studio compiler it works well with \0.
    I'm not sure if I'm entirely following your question here. Start with a string literal:
    Code:
    "Hello World!"
    Now, since the string starts and ends with quotes, you can't directly type quotes in there. So, you have what are called escape sequences, which allow you to type things easily into string literals that wouldn't otherwise be easy to do. Escape sequences start with a \. For example, to get that quote in a string:
    Code:
    "Hello World! is a famous \"quote\"."
    There are other escape sequences, such as \0, which allows you to place nulls in a string. (Note that string literals include one at the end automatically - both of the above examples have a null at the end.) \n is a newline, and \t is a tab. Since \ starts an escape sequence, if you want a \ in a string, you must type \\. \* isn't one, and a good compiler should warn you.
    Code:
    esc.c:5:9: warning: unknown escape sequence '\*'
    As for deleting directories, does passing the directory itself, as if it were a file, not work? If not, read the manual.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinAPI & threading
    By @nthony in forum Windows Programming
    Replies: 17
    Last Post: 10-15-2007, 04:41 PM
  2. do i still need winAPI
    By datainjector in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2003, 01:43 AM
  3. references for the winapi
    By stallion in forum Windows Programming
    Replies: 9
    Last Post: 01-28-2003, 01:56 AM
  4. What is the one place that has it all for the winapi
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-27-2002, 12:49 PM
  5. WINAPI: Meaning of HDC ?
    By Mecnels in forum Windows Programming
    Replies: 1
    Last Post: 01-21-2002, 09:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21