Archived

This topic is now archived and is closed to further replies.

mickey

hi, was wondering if i can use DirectSound in console mode?

Recommended Posts

i used the GetDisplayWindow() on the parameter of SetCooperativeLevel which works well, (i guess so coz it returned S_OK)., coz what happened is, everything is S_OK but i didn''t heard the sound! so is it possible to use DirectSound in console mode? thanks!

Share this post


Link to post
Share on other sites
yes. make sure you set the buffer to be global and sticky so you dont need to have focus for sound output. i have written a few apps that used soly the console (since they were simply testing of ideas and did not really warrent a complete gui).

Share this post


Link to post
Share on other sites
hiya a person! it''s you again! anyway...,

ack! I transfered my code into winmain, it worked! i heard the sound but in console mode i didn''t! I just changed the first parameter on the IDirectSound8::SetCooperativeLevel() and gave real hwnd that was returned by CreateWindow()

there''s something wrong with the GetDisplayWindow(), i can''t use it! so what am i going to pass onto the IDIRECTSOUND8::SETCOOPERATIVELEVEL() on it''s first parameter wherin it needs a valid HWND?

thanks,

Share this post


Link to post
Share on other sites
quote:
Original post by mickey
so what am i going to pass onto the IDIRECTSOUND8::SETCOOPERATIVELEVEL() on it''s first parameter wherin it needs a valid HWND?



You might try passing NULL or using GetActiveWindow() or GetDesktopWindow(). Here is a trick for getting the HWND of a console.

  
// console finding guid

// a unique number to identify this console - replace this with your own

#define CON_GUID TEXT("CON_GUID-{68E311EF-BF32-4b0f-8D35-E84E4A463096}")

// hwnd for console window

HWND hConWnd = NULL;

// buffer for storing a substitute title

TCHAR szTempTitle[] = CON_GUID;

// buffer for storing current console title

TCHAR szTempString[MAX_PATH];

// obtain the current console title

if( GetConsoleTitle(szTempString, sizeof(szTempString)/sizeof(TCHAR) ) )
{
// replace the current title with substitute title

SetConsoleTitle(szTempTitle);

// give it a chance to set in

Sleep(50);

// locate the console window


// console window class on W9x is "tty"

hConWnd = FindWindow(TEXT("tty"), szTempTitle);
// console window class on W2K is "ConsoleWindowClass"

// hConWnd = FindWindow(TEXT("ConsoleWindowClass"), szTempTitle);


// restore the original console title

SetConsoleTitle(szTempString);

}

// verify the console hwnd

if ( hConWnd != NULL ) {
// respond as you would

}

// use hConWnd as desired




On W2K there is also an undocumented function "GetConsoleWindow"

Share this post


Link to post
Share on other sites
hiya lessbread,

that's cool, but i don't know much about GUIDs i just know their some sort of 'should be unique ids' ? any more info about those? and should i really change that?

but anyway i got an error when i tried to play the buffer now, what does this thing means? any idea? i already used the DSSCL_PRIORITY flag...,

-----------------------
DSERR_PRIOLEVELNEEDED
The caller does not have the priority level required for the function to succeed
-----------------------

thanks!







[edited by - mickey on May 5, 2002 3:53:58 AM]

Share this post


Link to post
Share on other sites
Hiya Mickey!

quote:
Original post by mickey
that''s cool, but i don''t know much about GUIDs i just know their some sort of ''should be unique ids'' ? any more info about those? and should i really change that?


GUID stands for "Globally Unique Identifier" - no two are supposed to be alike. An easy tool to use to make them is GuidGen.exe which can be found in the MSVC tools directory (eg C:\Program Files\Microsoft Visual Studio\Common\Tools). At any rate - that value doesn''t have to be a GUID at all - just a string that you know won''t clash with any other window titles. So "mickey''s temporary console name" would work just as well. I choose to use a GUID because it''s ''guaranteed to be unique''.

quote:
Original post by mickey
but anyway i got an error when i tried to play the buffer now, what does this thing means? any idea? i already used the DSSCL_PRIORITY flag...,

-----------------------
DSERR_PRIOLEVELNEEDED
The caller does not have the priority level required for the function to succeed
-----------------------


Honestly, I don''t know. I''ve never implemented DirectSound before. I just knew how to get a HWND to a console and so I thought I would share that tip with you. A google search turns up a few pages, but nothing that seems to the point - but a google group search might help.

Looking through a few of them, that error message appears to indicate that SetCooperativeLevel probably failed. Maybe a person or someone else can be of more help.

Share this post


Link to post
Share on other sites
ack lessbread! your code is correct it was my mistake, it works now!!!

I saw this thing in your comments (am using XP) so i tried this one instead
// console window class on W2K is "ConsoleWindowClass"
hConWnd = FindWindow(TEXT("ConsoleWindowClass"), szTempTitle);

and commented this thing out..
// hConWnd = FindWindow(TEXT("tty"), szTempTitle);

so what does this two thing means? i mean, what''s with the difference between the two? and what does tty stands for?

thanks a lot lessbread!

ps,
any other cool functions to get a valid HWND?

Share this post


Link to post
Share on other sites
quote:
Original post by mickey
I saw this thing in your comments (am using XP) so i tried this one instead
// console window class on W2K is "ConsoleWindowClass"
hConWnd = FindWindow(TEXT("ConsoleWindowClass"), szTempTitle);

and commented this thing out..
// hConWnd = FindWindow(TEXT("tty"), szTempTitle);

so what does this two thing means? i mean, what''s with the difference between the two? and what does tty stands for?


Those are the window class names for the console window. Windows 95 and 98 (W9x) implement consoles differently than Windows NT (2000/XP) because they have a lot of 16 bit leftovers from DOS. There is an entire console API written for NT, W9x implements most of it, but not all of it.

TTY - an abbreviation of "teletypewriter". The name harkens back to Unix consoles, VT100 terminals, 2800 baud modems and the days before GUI''s - but the acronym is not obsolete. A google search on tty turns up 100,000 pages - mostly about telecommunications devices for the deaf - modify the search with the word ''console'' for better results.

HWND tricks? Yes - here are two that use undocumented functions, so your mileage may vary.


  
HWND WINAPI GetConsoleWindowNT(void)
{
// declare function pointer type

typedef HWND WINAPI (*GetConsoleWindowT)(void);

// declare one such function pointer

GetConsoleWindowT GetConsoleWindow;

// get a handle on kernel32.dll

HMODULE hK32Lib = GetModuleHandle(TEXT("KERNEL32.DLL"));

// assign procedure address to function pointer

GetConsoleWindow = (GetConsoleWindowT)GetProcAddress(hK32Lib,TEXT("GetConsoleWindow"));

// check if the function pointer is valid

// since the function is undocumented

if ( GetConsoleWindow == NULL ) {
return NULL;
}

// call the undocumented function

return GetConsoleWindow();
}

// ---------------------------------------------------------------------------


HWND WINAPI GetHandleToShellWindow(void)
{
// declare function pointer type

typedef HWND (WINAPI *GetShellWindowT)(void);

// declare one such function pointer

GetShellWindowT GetShellWindow;

// declare a local hwnd

HWND hShellWnd = NULL;

// load User32.dll

HMODULE hU32Lib = LoadLibrary(TEXT("USER32.DLL"));
if ( hU32Lib == NULL ) {
return NULL;
}

// assign procedure address to function pointer

GetShellWindow = (GetShellWindowT)GetProcAddress(hU32Lib,TEXT("GetShellWindow"));

// check if the function pointer is valid

// since the function is undocumented

if ( GetShellWindow != NULL ) {
// call the undocumented function

hShellWnd = GetShellWindow();
}

// play nice with windows

FreeLibrary(hU32Lib);

return hShellWnd;
}

A couple items to note. "TEXT" is a macro that ensures the string is Unicode or Ansi as needed, so those calls will remain valid whether or not you have UNICODE defined.

In GetConsoleWindowNT, I called GetModuleHandle rather than LoadLibrary because GetModuleHandle doesn''t increase the reference count for dll''s that are already loaded into the process address space. That means there''s no need to call an unloading function. I have yet to see a Win32 prog that doesn''t map Kernel32.dll so there''s really no need to use LoadLibrary there.

In GetHandleToShellWindow I used LoadLibrary because it''s possible that User32.dll hasn''t already been mapped into the process address space. In this case, the call to FreeLibrary to unmap the dll was warranted.

Share this post


Link to post
Share on other sites