Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   Problema con funcion (https://www.clubdelphi.com/foros/showthread.php?t=83851)

giulichajari 06-08-2013 06:45:08

Problema con funcion
 
Tengo una funcion asociada a un boton y los respectivos componentes pero me da un error en la tercera linea, dice "Declaration syntax error", es en la linea

Código Delphi [-]
unsigned int Drivetype = GetDriveType(Drive.c_str ());

Código Delphi [-]
AnsiString temp;
AnsiString Drive = AnsiString(DriveComboBox1->Drive) //EditDrive->Text;
unsigned int Drivetype = GetDriveType(Drive.c_str ());
switch (drivetype)
{
case 1 : temp = “No root directory”; return;
case DRIVE_REMOVABLE : temp = “Removable”; break;
case DRIVE_FIXED : temp = “Fixed”; break;
case DRIVE_REMOTE : temp = “Remote (network) drive”; break;
case DRIVE_CDROM : temp = “CD-ROM”; break;
case DRIVE_RAMDISK : temp = “RAM disk”; break;
default: temp = “Unknown”; return;
}
LabelDriveType->Caption = temp;
temp = “”;
DWORD VolumeSerialNumber = 0;
DWORD MaximumComponentLength = 0;
DWORD FileSystemFlags = 0;
char * volumeinfo = new char[255];
volumeinfo[0] = 0;
char* FileSystemNameBuffer = new char[255];
FileSystemNameBuffer[0] = 0;
GetVolumeInformation (Drive.c_str (), volumeinfo,
CHAPTER 14 Win32 API Functional Areas 530
255, &VolumeSerialNumber,
&MaximumComponentLength, &FileSystemFlags, 
?FileSystemNameBuffer,255);
if (strlen(volumeinfo) != 0)
EditVolumeInfo->Text = volumeinfo ;
else
EditVolumeInfo->Text = “- no label -”;
//Translate integer to chars for serial number
char string1[35];
char string2[35];
if (VolumeSerialNumber > 0)
{
unsigned int bottom = (LOWORD(VolumeSerialNumber));
unsigned int top = (HIWORD(VolumeSerialNumber));
sprintf(string1,”%04X”,top);
sprintf(string2,”%04X”,bottom);
LabelSerialNum->Caption = AnsiString(string1) + “-” + AnsiString(string2);
}
else
LabelSerialNum->Caption = “- unknown -”;
if (MaximumComponentLength > 0)
LabelMaxComponentLength->Caption = AnsiString(MaximumComponentLength) + “
?characters”;
else
LabelMaxComponentLength->Caption = “- unknown -”;
if (strlen(FileSystemNameBuffer) != 0)
LabelFileSystemNameBuffer->Caption = FileSystemNameBuffer;
else
LabelFileSystemNameBuffer->Caption = “- unknown -”;
LabelFileSystemFlags->Caption = “”; //AnsiString(FileSystemFlags);
if (FileSystemFlags & FS_CASE_IS_PRESERVED)
temp += AnsiString(“Filename case is preserved.\n”);
if (FileSystemFlags & FS_CASE_SENSITIVE)
temp += AnsiString(“Lookup is case-sensitive.\n”);
if (FileSystemFlags & FS_UNICODE_STORED_ON_DISK)
temp += AnsiString(“Supports Unicode in filenames.\n”);
if (FileSystemFlags & FS_PERSISTENT_ACLS)
temp += AnsiString(“Preserves and enforces ACLs.\n”);
if (FileSystemFlags & FS_FILE_COMPRESSION)
System Services 531
LISTING 14.6 Continued
temp += AnsiString(“Supports file-based compression.\n”);
if (FileSystemFlags & FS_VOL_IS_COMPRESSED)
temp += AnsiString(“Volume is compressed. (i.e., DoubleSpace).\n”);
LabelFileSystemFlags->Caption = temp;
DWORD spc = 0; //Sectors per cluster
DWORD bps = 0; //Bytes per cluster
DWORD cluster = 0; //clusters
DWORD freeclust = 0; //freeclusters
GetDiskFreeSpace (Drive.c_str (),&spc,&bps,&freeclust,&cluster) ;
unsigned long v1 = (unsigned long)cluster;
unsigned long v2 = (unsigned long) spc;
unsigned long v3 = (unsigned long) bps;
unsigned long volsize = (v1 * v2)/1024 * v3;
LabelVolumeSize->Caption = AnsiString(FormatSize(volsize));
unsigned long free_bytes = (freeclust * spc)/1024 * bps;
LabelFreeSpace->Caption = AnsiString(FormatSize(free_bytes));
if (volsize > 0)
LabelUsed->Caption = AnsiString(((volsize - free_bytes) * 100) / volsize) +
?“ %”;
else
LabelUsed->Caption = “n/a”;
}


Al parecer es la funcion Drive.c_str la que da el problema pero no logro darme cuenta que pasa.

Muchas gracias..
Saludos

ecfisa 06-08-2013 08:39:45

Hola giulichajari.

El error que mencionas se produce por la falta de un punto y coma:
Código:


...
  AnsiString Drive = AnsiString(DriveComboBox1->Drive);  // <- Aquí !!
  unsigned int DriveType = GetDriveType(Drive.c_str());
...

Pero no será el único... a golpe de vista vas a tener que revisar:
Código:

GetVolumeInformation (
  Drive.c_str (),
  volumeinfo,
  CHAPTER 14 Win32 API Functional Areas 530255, // Esto no es C++...
  &VolumeSerialNumber,
  &MaximumComponentLength,
  &FileSystemFlags,
  ?FileSystemNameBuffer,  // "?" error
  255
);

Código:

...
  wsprintf(string1,"%04X",top);  // En lugar de sprintf 
  wsprintf(string2,"%04X",bottom);
...

Código:

...
  if (FileSystemFlags & FS_FILE_COMPRESSION)
    System Services 531  LISTING 14.6 Continued // Esto tampoco es C++
...

Saludos. :)

Casimiro Notevi 06-08-2013 10:39:42

Me ha recordado el (fin de la cita) de Rajoy.

ecfisa 06-08-2013 11:28:34

Cita:

Empezado por Casimiro Notevi (Mensaje 465106)
Me ha recordado el (fin de la cita) de Rajoy.

:D:D:D .

Saludos. :)

giulichajari 07-08-2013 23:55:26

Gracias
 
Gracias por responder...

Bueno ya me di cuenta que faltaba el punto y coma, y a decir verdad copie el codigo de un libro, por eso tiene el numero de pagina y un titulo que no corresponde, ademas el programa no esta identado.
Pero acabo de hacerle muchas correciones.
Una cosa : la funcion sprintf si la pude utilizar incluyendo la libreria

Código Delphi [-]
#include stdio.h

lo que no me acepta ahora es:
Código Delphi [-]
LabelVolumeSize->Caption = AnsiString(FormatSize(volsize));

porque no me toma "FormatSize", pero no se que libreria o .h incluir para que lo tome.

Saludos

ecfisa 08-08-2013 01:15:18

Hola giulichajari.

Cita:

Empezado por giulichajari (Mensaje 465180)
Una cosa : la funcion sprintf si la pude utilizar incluyendo la libreria

Código Delphi [-]
#include stdio.h

Lo que decis es correcto, no fuí claro en ese punto. Sólo te aconsejaba el uso de wsprintf para evitar la inclusión del .h
Cita:

lo que no me acepta ahora es:
Código Delphi [-]
LabelVolumeSize->Caption = AnsiString(FormatSize(volsize));

porque no me toma "FormatSize", pero no se que libreria o .h incluir para que lo tome.
Al menos yo no conozco ninguna función de Windows o Builder C++ con ese nombre, tal vez venga en una versión posterior de Builder C++ que la que dispongo. O también podría ser una función definida por el usuario en otra parte del código.

Saludos. :)

giulichajari 10-08-2013 18:38:30

De vuelta
 
Bueno vuelvo a publicar, porque no noto ningún efecto al apretar el boton al que tengo asociado el codigo, osea lo que entiendo es que los "labels" deberian cambiar, pero no sucede nada.

el codigo me quedo asi:

Código Delphi [-]
#include 
#include 
#include <string.h>
#include 

#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"
#include "Unit1.h"
#include 
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        AnsiString temp;
        AnsiString Drive = AnsiString(DriveComboBox1->Drive); //EditDrive->Text;

        unsigned int drivetype = GetDriveType(Drive.c_str ());
switch (drivetype)
    {
        case 1 : temp = "No root directory"; return;
        case DRIVE_REMOVABLE : temp = "Removable"; break;
        case DRIVE_FIXED : temp = "Fixed"; break;
        case DRIVE_REMOTE : temp ="Remote (network) drive"; break;
        case DRIVE_CDROM : temp = "CD-ROM"; break;
        case DRIVE_RAMDISK : temp = "RAM disk"; break;
        default: temp = "Unknown"; return;
     }
        LabelDriveType->Caption = temp;
        temp = "";
        DWORD VolumeSerialNumber = 0;
        DWORD MaximumComponentLength = 0;
        DWORD FileSystemFlags = 0;
        char * volumeinfo = new char[255];
        volumeinfo[0] = 0;
        char* FileSystemNameBuffer = new char[255];
        FileSystemNameBuffer[0] = 0;
        GetVolumeInformation (Drive.c_str (), volumeinfo, 255,&VolumeSerialNumber,
        &MaximumComponentLength, &FileSystemFlags,
        FileSystemNameBuffer,255);
        if (strlen(volumeinfo) != 0)
                EditVolumeInfo->Text = volumeinfo ;
        else
                EditVolumeInfo->Text = "- no label -";
//Translate integer to chars for serial number
        char string1[35];
        char string2[35];
        if (VolumeSerialNumber > 0)
                {
                        unsigned int bottom = (LOWORD(VolumeSerialNumber));
                        unsigned int top = (HIWORD(VolumeSerialNumber));
                        sprintf(string1,"%04X",top);
                        sprintf(string2,"%04X",bottom);
                        LabelSerialNum->Caption = AnsiString(string1) + "-" + AnsiString(string2);
                }
        else
        {
                LabelSerialNum->Caption = "- unknown -";
        if (MaximumComponentLength > 0)
                LabelMaxComponentLength->Caption = AnsiString(MaximumComponentLength) + "characters";
        else
                LabelMaxComponentLength->Caption = "- unknown -";
        if (strlen(FileSystemNameBuffer) != 0)
                LabelFileSystemNameBuffer->Caption = FileSystemNameBuffer;
        else
                LabelFileSystemNameBuffer->Caption = "- unknown -";
                LabelFileSystemFlags->Caption = ""; //AnsiString(FileSystemFlags);
        if (FileSystemFlags & FS_CASE_IS_PRESERVED)
                temp += AnsiString("Filename case is preserved.\n");
        if (FileSystemFlags & FS_CASE_SENSITIVE)
                temp += AnsiString("Lookup is case-sensitive.\n");
        if (FileSystemFlags & FS_UNICODE_STORED_ON_DISK)
                temp += AnsiString("Supports Unicode in filenames.\n");
        if (FileSystemFlags & FS_PERSISTENT_ACLS)
                temp += AnsiString("Preserves and enforces ACLs.\n");
        if (FileSystemFlags & FS_FILE_COMPRESSION)
                temp += AnsiString("Supports file-based compression.\n");
        if (FileSystemFlags & FS_VOL_IS_COMPRESSED)
                temp += AnsiString("Volume is compressed. (i.e., DoubleSpace).\n");
        LabelFileSystemFlags->Caption = temp;
        DWORD spc = 0; //Sectors per cluster
        DWORD bps = 0; //Bytes per cluster
        DWORD cluster = 0; //clusters
        DWORD freeclust = 0; //freeclusters
        GetDiskFreeSpace (Drive.c_str (),&spc,&bps,&freeclust,&cluster) ;
        unsigned long v1 = (unsigned long)cluster;
        unsigned long v2 = (unsigned long) spc;
        unsigned long v3 = (unsigned long) bps;
        unsigned long volsize = (v1 * v2)/1024 * v3;
LabelVolumeSize->Caption = AnsiString(volsize);
        unsigned long free_bytes = (freeclust * spc)/1024 * bps;
LabelFreeSpace->Caption = AnsiString(free_bytes);
        if (volsize > 0)
                LabelUsed->Caption = AnsiString(((volsize - free_bytes) * 100) / volsize) +" %";
        else
                LabelUsed->Caption = "n/a";
}
};

Pero no se porque no pasa nada.

Muchas gracias
Saludos..

ecfisa 10-08-2013 19:04:25

Hola.

Uno de los motivos probables que no veas ningún resultado se debe a estas líneas:
Código:

...
  unsigned int drivetype = GetDriveType(Drive.c_str ());
  switch (drivetype) {
    case 1 : temp = "No root directory"; return;  // <- aqui
    case DRIVE_REMOVABLE : temp = "Removable"; break;
    case DRIVE_FIXED : temp = "Fixed"; break;
    case DRIVE_REMOTE : temp ="Remote (network) drive"; break;
    case DRIVE_CDROM : temp = "CD-ROM"; break;
    case DRIVE_RAMDISK : temp = "RAM disk"; break;
    default: temp = "Unknown"; return;  // <- o aqui
  }
...

Si drivetype toma el valor 1 u otro no contemplado en los case del switch, saldrá de la función en ese punto y no ejecutará el codigo siguiente.

Saludos. :)

giulichajari 11-08-2013 12:52:56

Ok gracias
 
Cambie el primer return por un break y ahora funciona solo que me tira "0" y "unknown" y no me muestra la informacion, tendria que revisar el codigo, pero gracias por tu consejo ecfisa.

giulichajari 18-08-2013 18:39:00

Nuevo codigo
 
Bueno he conseguido que me muestre una cosa por lo menos..

Los "DRIVE" son Defines...


Código Delphi [-]
#include 
#include 
#include <string.h>
#include 
#define DRIVE_UNKNOWN 0
#define DRIVE_NO_ROOT_DIR 1
#define DRIVE_REMOVABLE 2
#define DRIVE_FIXED 3
#define DRIVE_REMOTE 4
#define DRIVE_CDROM 5
#define DRIVE_RAMDISK 6
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"
#include "Unit1.h"
#include 
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        AnsiString temp;
        char Drive[256];
        AnsiString drive = DriveComboBox1->Drive; //EditDrive->Text;
        StrCopy(Drive,drive.c_str());
        StrCat(Drive,":\\");



        unsigned int DriveType = GetDriveType(Drive);
switch (DriveType)
    {
        case 1 : temp = "No root directory"; break;
        case 2 : temp = "Removable"; break;
        case 3 : temp = "Fixed"; break;
        case 4 : temp ="Remote (network) drive"; break;
        case 5 : temp = "CD-ROM"; break;
        case 6 : temp = "RAM disk"; break;
        default: temp = "Unknown"; return;
     }
        LabelDriveType->Caption = temp;
        temp = "";

El caso ahora es que el resto del codigo no funciona correctamente, porque me mustra 0 o desconocido.
Solo logro ver el tipo de volumen.


La franja horaria es GMT +2. Ahora son las 09:16:08.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi