Solved

a problem about API

Posted on 2001-07-06
4
160 Views
Last Modified: 2010-04-06
I  used a api function GetVolumeInformation to get the volume serial number of a cd. and I found that I can not get the same value in the win98 os and win2000 os . why!!!
my code is below
procedure TForm1.Button1Click(Sender: TObject);
var
 dw1,dw2:dword;
 p:pDWORD;
begin
  try
    getmem(p,255);
    getvolumeinformation('g:\',nil,0,p,dw1,dw2,nil,0);
    edit1.Text:=inttostr(p^);
  finally
    freemem(p);
  end;
end;

I get two different edit1.text in win98 and win2000. but in fact they should be the same value.

who can help me to solve the problem

sincerely yours
lovedelphi

0
Comment
Question by:lovedelphi
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Motaz earned 100 total points
ID: 6261282
Try this solution:

Drop a Button, Label and  a MediaPlayer component. Make sure that MMSYstem is included in the Uses clause:


function GetCDInfo(ID: Integer; vWord: Word):String;
      var msp : TMCI_INFO_PARMS;
          MediaString : array[0..255] of char;
          ret : longint;
      begin
       FillChar(MediaString, SizeOf(MediaString), #0);
       FillChar(msp, sizeof(msp), #0);
       msp.lpstrReturn := @MediaString;
       msp.dwRetSize := 255;
       ret := mciSendCommand(ID,
                             MCI_INFO,
                             vWord,
                             longint(@msp));
       if Ret <> 0 then begin
         MciGetErrorString(ret, @MediaString, SizeOf(MediaString));
         Result := StrPas(MediaString);
       end else Result := StrPas(MediaString);
      end;

      function GetSerialNumber(ID: Integer): string;
      begin
       Result := GetCDInfo(ID, MCI_INFO_MEDIA_IDENTITY);
      end;



procedure TForm1.Button1Click(Sender: TObject);
begin
 MediaPlayer1.DeviceType:= dtCDAudio;
 MediaPlayer1.Open;
 Label1.Caption:= GetSerialNumber(MediaPlayer1.DeviceID);
end;

Motaz
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6261436
Hi lovedelphi,

wow, I just HAVE to help a guy with this name...   :-)

Please don't ask me why GetVolumeInformation returns different serial numbers under 9x and NT, I really don't know the reason. Motaz' suggestion (using MCI) is the correct solution. It gives you the same serial number in 9x and NT - well, in most cases at least. Recently I had a DVD ROM, which produced different MCI serial numbers, too. Sigh... But in most cases (at least for normal CDs) it works alright.

If you don't want to use the mediaPlayer, you can use my function, it needs less imports/uses:

function GetCDSerialNo(drive: char) : cardinal;
var mo    : TMCI_OPEN_PARMS;
    mp    : TMCI_INFO_PARMS;
    mg    : TMCI_GENERIC_PARMS;
    s1    : string;
    arrCh : array [0..255] of char;
begin
  result := 0;
  s1 := drive + ':';
  ZeroMemory(@mo, sizeOf(mo));
  ZeroMemory(@mp, sizeOf(mp));
  ZeroMemory(@mg, sizeOf(mg));
  mo.lpstrDeviceType  := pchar(MCI_DEVTYPE_CD_AUDIO);
  mo.lpstrElementName := pchar(s1);
  mp.lpstrReturn := arrCh;
  mp.dwRetSize   := sizeOf(arrCh) - 1;
  if mciSendCommand(0, MCI_OPEN,
                    MCI_OPEN_ELEMENT or MCI_OPEN_SHAREABLE or
                                        MCI_OPEN_TYPE or MCI_OPEN_TYPE_ID or MCI_WAIT,
                    cardinal(@mo)) = 0 then
    try
      if mciSendCommand(mo.wDeviceID, MCI_INFO, MCI_INFO_MEDIA_IDENTITY, cardinal(@mp)) = 0 then
        result := cardinal(StrToIntDef(arrCh, 0));
    finally mciSendCommand(mo.wDeviceID, MCI_CLOSE, 0, cardinal(@mg)) end;
end;

Regards, Madshi.
0
 

Author Comment

by:lovedelphi
ID: 6267727
thank you I got it!
0
 

Author Comment

by:lovedelphi
ID: 6267938
I can not give your score!!! how to...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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