Solved

a problem about API

Posted on 2001-07-06
4
161 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

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

16 Experts available now in Live!

Get 1:1 Help Now