• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

Subst

Is there a function in Delphi for associating a path with a drive letter like SUBST.EXE in DOS does?

johan
0
sageryd
Asked:
sageryd
  • 6
  • 5
  • 5
  • +3
1 Solution
 
rwilson032697Commented:
Look up WNetAddConnection, WNetAddConnection2 and their ilk!

Cheers,

Raymond.
0
 
MadshiCommented:
"WNetAddConnection(*)" is for mapping drive letters to UNC network paths.

If you want to do what subst does, you need to call "DefineDosDevice". But that works only under win98 and winNT, not under win95...   :-(

Regards, Madshi.
0
 
intheCommented:
uses ShellAPI;

shellexecute(0,'open','subst','x: c:\somedir',nil,SW_SHOWNORMAL);
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
MadshiCommented:
Yes...   :-)

But you can't undo this subst, Barry...   :-(

Regards, Madshi.
0
 
Roadrunner100598Commented:
Hey Madshi,

wasn't there something like  SUBST X: /d
0
 
MadshiCommented:
Yes, but it works only in the same dos box, where it was installed. If you close the dos box and open it again, subst x: /d doesn't work anymore. Don't know why...   :-(
0
 
MadshiCommented:
Ehhm....... ooooooooops, I'm sorry, it DOES work on my win98 pc. But as far as I remember, it didn't work on win95. But since DefineDosDevice doesn't work there at all, Barry's suggestion is probably the best one...

:-)
0
 
sagerydAuthor Commented:
Barry's solution is what I'm currently using, the problem is that i have publish SUBST.EXE with my app, I need it all in one file!
0
 
intheCommented:
ok you may not want to do this but
its possible you could add subst.exe to your apps res file so its compiled into your exe and when you need it extract it ,use it ,delete it...
i had a look through tomes api book but couldnt find a similar api command apart from madshis one.
0
 
MadshiCommented:
Well, why do you have to publish subst.exe with your app?? It is installed on every win9x machine, so need to do so. If you want your program to run under winNT, too. You can use subst.exe for win9x and DefineDosDevice for winNT.
0
 
sagerydAuthor Commented:
Yes Barry...that would be nice, how can I do that?
0
 
intheCommented:
i will send the code in a minute
0
 
sagerydAuthor Commented:
Thanks!
0
 
intheCommented:
ok with this example i am using a exe file called test.exe and are calling it from the res file and renaming it to labels.exe
so create a res file in notepad with a exe in it like so:
 
TESTFILE  EXEFILE C:\TEST.EXE
save it as myres.rc and compile this with brcc32 to get myres.res    

then go to delphi and add the following code and all will be well.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,shellapi,StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
{$R MYRES.RES}
procedure ExtractRes(ResType, ResName, ResNewName : String);
var
  Res : TResourceStream;
begin
  Res := TResourceStream.Create(Hinstance, Resname, Pchar(ResType));
  Res.SavetoFile(ResNewName);
  Res.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ExtractRes('EXEFILE','TESTFILE','C:\LABELS.EXE');
// run the shellexecute command here
end;

end.

that was a an old example i did for another question so just replace test.exe and labels.exe with subst.exe and extract it to the dir of your program or windows system then run the command given in earlier posting.
Regards Barry
0
 
men xinCEOCommented:
following...
0
 
sagerydAuthor Commented:
Sorry that I kept you waiting Barry, I guess there's no other solution to this one so therefore you may post an answer!

Oh..um..just one more thing while we're still talking RES-files, how can I implement a wav/mid file in the executable and then play it in the TMediaPlayer? Do I need to extract it?
0
 
sagerydAuthor Commented:
regards,

Johan
0
 
intheCommented:
wav:

(copying from old e.e question)
1) Use the sndPlaySound() function to directly
play a wave file.

2) Read the wave file into memory, then use the
sndPlaySound() to play the wave file

3) Use sndPlaySound to directly play a wave
file thats embedded in a resource file attached
to your application.

4) Read a wave file thats embedded in a resource
file attached to your application into memory,
then use the sndPlaySound() to play the wave file.

To build the project you will need to:

1) Create a wave file called 'hello.wav'
in the project's directory.

2) Create a text file called 'snddata.rc'
in the project's directory.

3) Add the following line to the file 'snddata.rc':
HELLO WAVE hello.wav

4) At a dos prompt, go to your project directory
and compile the .rc file using the Borland Resource
compiler (brcc32.exe) by typing the path to brcc32.exe
and giving 'snddata.rc' as a parameter.

Example:

bin\brcc32 snddata.rc

This will create the file 'snddata.res' that
Delphi will link with your application's .exe
file.



*)

unit PlaySnd1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
PlaySndFromFile: TButton;
PlaySndFromMemory: TButton;
PlaySndbyLoadRes: TButton;
PlaySndFromRes: TButton;
procedure PlaySndFromFileClick(Sender: TObject);
procedure PlaySndFromMemoryClick(Sender: TObject);
procedure PlaySndFromResClick(Sender: TObject);
procedure PlaySndbyLoadResClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

{$R snddata.res}

uses MMSystem;

procedure TForm1.PlaySndFromFileClick(Sender: TObject);
begin
sndPlaySound('hello.wav',
SND_FILENAME or SND_SYNC);
end;

procedure TForm1.PlaySndFromMemoryClick(Sender: TObject);
var
f: file;
p: pointer;
fs: integer;
begin
AssignFile(f, 'hello.wav');
Reset(f,1);
fs := FileSize(f);
GetMem(p, fs);
BlockRead(f, p^, fs);
CloseFile(f);
sndPlaySound(p,
SND_MEMORY or SND_SYNC);
FreeMem(p, fs);
end;

procedure TForm1.PlaySndFromResClick(Sender: TObject);
begin
PlaySound('HELLO',
hInstance,
SND_RESOURCE or SND_SYNC);
end;

procedure TForm1.PlaySndbyLoadResClick(Sender: TObject);
var
h: THandle;
p: pointer;
begin
h := FindResource(hInstance,
'HELLO',
'WAVE');
h := LoadResource(hInstance, h);
p := LockResource(h);
sndPlaySound(p,
SND_MEMORY or SND_SYNC);
UnLockResource(h);
FreeResource(h);
end;


end.









midi:

ive not put a midi in a res so either
follow the same for putting exe in res(does MIDI exist for res files ?)  but extract it to disk same as exe file (i dunno if its possible to stream it to tmediaplayer so i use the following but once you have it on disk i suppose you can use tmediaplayer if you wish)
or then use this to play it

You need to add the MMSYSTEM unit in your USES clause.



Function PlayMidiFile(FileName:string):word;
var
wdeviceid:integer;
dwreturn:word;
mciOpen:tmci_open_parms;
mciPlay:tmci_play_parms;
mciStat:tmci_status_parms;
mciseq:tmci_seq_set_parms;
begin
    // Open the device by specifying the device and filename.
    // MCI will attempt to choose the MIDI mapper as the output port.

    mciopen.lpstrDeviceType:='sequencer';
    mciopen.lpstrElementName:=pchar(filename);

    dwReturn:=mciSendCommand($0,mci_open,mci_open_type OR
mci_open_element,longint(@mciopen));
    if dwreturn<>0 then result:=dwreturn else
    begin
      // The device opened successfully; get the device ID.
      wDeviceID:=mciOpen.wDeviceID;
      // Check if the output port is the MIDI mapper.
      mciStat.dwItem:= MCI_SEQ_STATUS_PORT;
      dwReturn:=mciSendCommand(wDeviceID, MCI_STATUS,
MCI_STATUS_ITEM,longint(@mciStat));
      if dwreturn<>0 then
      begin
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
        result:=dwreturn;
      end else
      begin
        // Begin playback. The window procedure function for the parent
        // window will be notified with an MM_MCINOTIFY message when
        // playback is complete. At this time, the window procedure closes
        // the device.
        mciPlay.dwCallback:=application.Handle;
        dwReturn:=mciSendCommand(wDeviceID, MCI_PLAY,
MCI_NOTIFY,longint(@mciPlay));
        if dwreturn<>0 then
        begin
          mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
          result:=dwreturn;
        end;
      end;
    end;
end;

hope all that makes sense ;-)
Regards Barry
0
 
sagerydAuthor Commented:
Nice! Thanx! Hope it works though......


regards,

Johan
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 5
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now