strsafe.h unter dem BCB?



  • Hallo,

    Kurze und knappe Frage.
    Ich will folgenden Beispiel Code aus der MSDN kompilieren:

    #define _WIN32_WINNT 0x0501
    
    #include <windows.h>
    #include <stdio.h>
    #include <strsafe.h>
    #include <malloc.h>
    
    #define BUFSIZE MAX_PATH
    
    int main(int argc, char *argv[])
    {
       WIN32_FIND_DATA FindFileData;
       HANDLE hFind = INVALID_HANDLE_VALUE;
       DWORD dwError;
       LPSTR DirSpec;
       size_t length_of_arg;
    
       DirSpec = (LPSTR) malloc (BUFSIZE);
    
       // Check for command-line parameter; otherwise, print usage.
       if(argc != 2)
       {
          printf("Usage: Test <dir>\n");
          return 2;
       }
    
       // Check that the input is not larger than allowed.
       StringCbLength(argv[1], BUFSIZE, &length_of_arg);
       if (length_of_arg > (BUFSIZE - 2))
       {
          printf("Input directory is too large.\n");
          return 3;
       }
    
       printf ("Target directory is %s.\n", argv[1]);
    
       // Prepare string for use with FindFile functions.  First, 
       // copy the string to a buffer, then append '\*' to the 
       // directory name.
       StringCbCopyN (DirSpec, BUFSIZE, argv[1], length_of_arg+1);
       StringCbCatN (DirSpec, BUFSIZE, "\\*", 3);
    
       // Find the first file in the directory.
       hFind = FindFirstFile(DirSpec, &FindFileData);
    
       if (hFind == INVALID_HANDLE_VALUE) 
       {
          printf ("Invalid file handle. Error is %u.\n", GetLastError());
          return (-1);
       } 
       else 
       {
          printf ("First file name is %s.\n", FindFileData.cFileName);
    
    						// List all the other files in the directory.
          while (FindNextFile(hFind, &FindFileData) != 0) 
          {
             printf ("Next file name is %s.\n", FindFileData.cFileName);
          }
    
          dwError = GetLastError();
          FindClose(hFind);
          if (dwError != ERROR_NO_MORE_FILES) 
          {
             printf ("FindNextFile error. Error is %u.\n", dwError);
             return (-1);
          }
       }
    
       free(DirSpec);
       return (0);
    }
    

    Dabei erhalte ich folgende Fehlermeldungen:

    [C++ Fehler] Unit1.cpp(5): E2209 include-Datei 'strsafe.h' kann nicht geöffnet werden
    [C++ Fehler] Unit1.cpp(29): E2268 Aufruf der undefinierten Funktion 'StringCbLength'
    [C++ Fehler] Unit1.cpp(41): E2268 Aufruf der undefinierten Funktion 'StringCbCopyN'
    [C++ Fehler] Unit1.cpp(42): E2268 Aufruf der undefinierten Funktion 'StringCbCatN'
    

    Den Header strsafe gibt's wohl nicht.
    Wie mache ich es denn richtig, dass es unter dem Borland C++ Builder 6 läuft?



  • Verwende C++ statt C. Dann gibt es keine Probleme.
    Was ich damit meine, ist, ersetze diese StringCB.. Funktionen und char* durch string oder AnsiString , FindFirstFile und FindNextFile durch FindFirst und FindNext (wenn du VCL verwenden willst).
    Etwa so (ohne VCL)

    //#define _WIN32_WINNT 0x0501
    
    #include <windows.h>
    //#include <cstdio>
    #include <iostream>
    using namspace std;
    
    //#define BUFSIZE MAX_PATH
    
    int main(int argc, char *argv[])
    {
       // Check for command-line parameter; otherwise, print usage.
       if(argc != 2)
       {
          cout << "Usage: Test <dir>\n";
          return 2;
       }
    
       WIN32_FIND_DATA FindFileData;
    
       cout << "Target directory is " << argv[1] << "\n";
    
       // Prepare string for use with FindFile functions.  First,
       // copy the string to a buffer, then append '\*' to the
       // directory name.
       string DirSpec(argv[1]);
       DirSpec += "\\*";
    
       // Find the first file in the directory.
       HANDLE hFind = FindFirstFile(DirSpec.c_str(), &FindFileData);
    
       if (hFind == INVALID_HANDLE_VALUE)
       {
          cout << "Invalid file handle. Error is" <<  GetLastError() << "\n";
          return -1;
       }
       else
       {
          cout << "First file name is " <<  FindFileData.cFileName << ".\n";
    
                            // List all the other files in the directory.
          while (FindNextFile(hFind, &FindFileData) != 0)
          {
             cout << "Next file name is " << FindFileData.cFileName << ".\n";
          }
    
          DWORD dwError = GetLastError();
          FindClose(hFind);
          if (dwError != ERROR_NO_MORE_FILES)
          {
             cout << "FindNextFile error. Error is " << dwError << ".\n";
             return (-1);
          }
       }
       return (0);
    }
    


  • @Braunstein: Danke schonmal!

    Kannst du mir jetzt noch sagen, wie ich es anwende?
    Bei mir geht es irgendwie nicht...

    Ich rufe die Konsole auf,
    geh in das Verzeichnis wo meine Programm.exe liegt und gebe

    programm.exe Test C:\Programme
    

    ein.

    Steht ja hier:

    cout << "Usage: Test <dir>\n";
    

    Aber dann kommt wieder die Usage Meldung...
    Was mache ich falsch?



  • Das Test, was da steht soll der Programmname sein. Bei dir dann eben programm.exe.
    Du mußt also folgendes eintippen

    programm.exe C:\Programme


Anmelden zum Antworten