-
Notifications
You must be signed in to change notification settings - Fork 2
/
A3D_DLL.CPP
3793 lines (3345 loc) · 105 KB
/
A3D_DLL.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===========================================================================
//
// A3D_DLL.CPP
//
// Purpose: Aureal A3D 1.x interface library (Wrapper to DirectSound3D).
//
// Copyright (C) 2004 Dmitry Nesterenko. All rights reserved.
//
//===========================================================================
#include <objbase.h>
#include <initguid.h>
#include <dsound.h>
#include "ia3dapi.h"
#include "a3d_dll.h"
#include "a3d_dal.h"
#ifdef _DEBUG
# include <crtdbg.h>
# include <stdio.h>
# include <stdarg.h>
#else
# pragma intrinsic(memset, memcmp, memcpy, strcpy, strcat, strlen)
#endif
// Compilation options.
#ifdef _DEBUG
# define WRITE_LOG_FILE TRUE
#endif
// Handle of shared library module.
HMODULE g_hModule = NULL;
// Handle of application window.
HWND g_hWnd = NULL;
// Counter number of locks.
LONG g_cLock = 0;
// Counter number of all objects.
LONG g_cObj = 0;
#ifdef _DEBUG
//===========================================================================
//
// ::LogMsg
//
// Purpose: Output logging message.
//
// Parameters:
// pszFormat LPCTSTR pointer to format string for message.
// ... (?) set other parameters for message.
//
//===========================================================================
void LogMsg(LPCTSTR pcszFormat, ...)
{
_ASSERTE(pcszFormat && !IsBadReadPtr(pcszFormat, sizeof(*pcszFormat)));
va_list vlArgs;
TCHAR szBuffer[256];
#if WRITE_LOG_FILE
TCHAR szLogName[MAX_PATH];
TCHAR szCallName[MAX_PATH];
// Get library module file name and truncate extension from it.
DWORD dwLength = GetModuleFileName(g_hModule, szLogName, sizeof(szLogName));
for (UINT i = dwLength; i; i--)
{
// Found file extension pointer.
if (TEXT('.') == szLogName[i])
break;
// Found begin of file name (file extension not exist).
if (TEXT('\\') == szLogName[i] || TEXT(':') == szLogName[i])
{
i = dwLength;
break;
}
}
// Get caller process module file name and extract it name.
dwLength = GetModuleFileName(NULL, szCallName, sizeof(szCallName));
for (UINT j = dwLength; j; j--)
{
// Truncate file extension.
if (TEXT('.') == szCallName[j])
{
dwLength = j + 1;
continue;
}
// Found begin of file name.
if (TEXT('\\') == szCallName[j] || TEXT(':') == szCallName[j])
{
j++;
break;
}
}
// Make log file name from library and caller names.
szLogName[i++] = TEXT('_');
lstrcat(lstrcpyn(&szLogName[i], &szCallName[j], dwLength - j), TEXT(".log"));
// Create or open log file.
HANDLE hLogFile = CreateFile(szLogName, GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE != hLogFile)
{
SYSTEMTIME timeLocal;
DWORD dwWritten;
// Save message time prefix to output string.
GetLocalTime(&timeLocal);
wsprintf(szBuffer, TEXT("%02u/%02u/%04u %02u:%02u:%02u.%03u "),
timeLocal.wDay, timeLocal.wMonth, timeLocal.wYear,
timeLocal.wHour, timeLocal.wMinute, timeLocal.wSecond,
timeLocal.wMilliseconds);
// Format logging message.
va_start(vlArgs,pcszFormat);
vsprintf(&szBuffer[lstrlen(szBuffer)], pcszFormat, vlArgs);
va_end(vlArgs);
// Write message string to log file.
lstrcat(szBuffer, TEXT("\r\n"));
SetFilePointer(hLogFile, 0, NULL, FILE_END);
WriteFile(hLogFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwWritten, NULL);
CloseHandle(hLogFile);
}
#endif
// Save message prefix to output string.
lstrcpy(szBuffer, TEXT("A3D: "));
// Format logging message.
va_start(vlArgs, pcszFormat);
vsprintf(&szBuffer[lstrlen(szBuffer)], pcszFormat, vlArgs);
va_end(vlArgs);
// Output message to debugger.
lstrcat(szBuffer, TEXT("\n"));
OutputDebugString(szBuffer);
}
//===========================================================================
//
// ::Result
//
// Purpose: Convert standart return code to text string.
//
// Parameters:
// hResult HRESULT return code value.
//
// Return: Pointer to text string with result.
//
//===========================================================================
LPCTSTR Result(HRESULT hResult)
{
if (SUCCEEDED(hResult))
return TEXT("Ok");
TCHAR szMessage[256];
// Get system message for error code.
if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, hResult, 0, szMessage, sizeof(szMessage) / sizeof(TCHAR), NULL))
lstrcpy(szMessage, TEXT("UNKNOWN ERROR!"));
else
for (UINT i = 0; TEXT('\0') != szMessage[i]; i++)
if (TEXT('\r') == szMessage[i] || TEXT('\n') == szMessage[i])
{
szMessage[i] = TEXT('\0');
break;
}
static TCHAR szReturn[256];
// Format error code and message text.
wsprintf(szReturn, TEXT("%#x:\"%s\""), hResult, szMessage);
return szReturn;
}
#endif
//===========================================================================
//
// ::GuidToStr
//
// Purpose: Convert GUID to text string.
//
// Parameters:
// rGuid REFGUID reference for GUID.
// pszBuffer LPTSTR buffer pointer for text string.
// iLen UINT buffer size for text string.
//
// Return: Pointer to text string with GUID.
//
//===========================================================================
LPTSTR GuidToStr(REFGUID rGuid, LPTSTR pszBuffer, UINT iSize)
{
#ifdef _DEBUG
_ASSERTE(pszBuffer && !IsBadWritePtr(pszBuffer, sizeof(*pszBuffer)));
_ASSERTE(iSize);
#endif
if (!&rGuid)
lstrcpyn(pszBuffer, TEXT("{NULL}"), iSize);
else
{
WCHAR wcGuid[64];
// Convert GUID to text string.
StringFromGUID2(rGuid, wcGuid, sizeof(wcGuid) / sizeof(WCHAR));
#ifdef UNICODE
lstrcpyn(pszBuffer, wcGuid, iSize);
#else
WideCharToMultiByte(CP_ACP, 0, wcGuid, -1, pszBuffer, iSize, NULL, NULL);
#endif
}
return pszBuffer;
}
//===========================================================================
//
// ::SplashScreen
//
// Purpose: Launch A3D splash application.
//
// Parameters:
// hWnd HWND handler of parent window.
//
//===========================================================================
static void SplashScreen(HWND hWnd)
{
#ifdef _DEBUG
LogMsg(TEXT("SplashScreen(%#x)"), hWnd);
#endif
// Execute this function only first time.
static bool bFirstCall = true;
if (bFirstCall)
bFirstCall = false;
else
return;
TCHAR szSplashFile[MAX_PATH];
HKEY hKey;
DWORD dwPlayAudio = 0, dwDisplayScreen = 0;
bool bPathExist = false;
// Open registry entry under HKEY_LOCAL_MACHINE.
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Aureal\\A3D"), 0,
KEY_READ, &hKey) == ERROR_SUCCESS)
{
DWORD dwSize = sizeof(szSplashFile);
// Read registry value.
if (RegQueryValueEx(hKey, TEXT("SplashPath"), NULL, NULL,
(LPBYTE)szSplashFile, &dwSize) == ERROR_SUCCESS)
bPathExist = true;
// Read registry values and close registry entry.
dwSize = sizeof(dwPlayAudio);
RegQueryValueEx(hKey, TEXT("SplashAudio"), NULL, NULL,
(LPBYTE)dwPlayAudio, &dwSize);
dwSize = sizeof(dwDisplayScreen);
RegQueryValueEx(hKey, TEXT("SplashScreen"), NULL, NULL,
(LPBYTE)dwDisplayScreen, &dwSize);
RegCloseKey(hKey);
}
// Make file name for launch splash application.
if (bPathExist)
lstrcat(szSplashFile, TEXT("\\A3DSplsh.exe"));
else
lstrcpy(szSplashFile, TEXT("A3DSplsh.exe"));
// Launch A3D splash application.
ShellExecute(hWnd, NULL, szSplashFile, NULL, NULL, SW_SHOWNORMAL);
// Delay for play audio and/or display logo screen.
if (dwPlayAudio || dwDisplayScreen)
Sleep(3500);
// Restore parent window.
if (hWnd && IsIconic(hWnd))
ShowWindow(hWnd, SW_RESTORE);
}
//===========================================================================
//
// ::CreateComObject
//
// Purpose: Create the registry entries for COM object.
//
// Parameters:
// rObjGuid REFGUID object CLSID.
// pcszRootName LPCTSTR name of object in HKEY_CLASSES_ROOT.
// pcszObjName LPCTSTR description of object.
//
// Return: S_OK if registration successful, error otherwise.
//
//===========================================================================
static void CreateComObject(REFGUID rObjGuid, LPCTSTR pcszRootName,
LPCTSTR pcszObjName)
{
#ifdef _DEBUG
TCHAR szGuid[64];
LogMsg(TEXT("CreateComObject(%s,%s,%s)"),
GuidToStr(rObjGuid, szGuid, sizeof(szGuid)), pcszRootName, pcszObjName);
_ASSERTE(pcszRootName && !IsBadReadPtr(pcszRootName, sizeof(*pcszRootName)));
_ASSERTE(pcszObjName && !IsBadReadPtr(pcszObjName, sizeof(*pcszObjName)));
#endif
TCHAR szObjGuid[64];
TCHAR cszRegSz[] = TEXT("REG_SZ");
TCHAR szKeyName[128];
HKEY hKey, hSubKey;
// Create the registry entry name string using CLSID.
GuidToStr(rObjGuid, szObjGuid, sizeof(szObjGuid));
// Create registry entry HKEY_CLASSES_ROOT\<Root Name>.
if (RegCreateKeyEx(HKEY_CLASSES_ROOT, pcszRootName, 0, cszRegSz,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
{
// Set default value for registry key.
RegSetValueEx(hKey, NULL, 0, REG_SZ, (LPBYTE)pcszObjName,
lstrlen(pcszObjName) + 1);
// Create registry entry HKEY_CLASSES_ROOT\<Root Name>\CLSID.
if (RegCreateKeyEx(hKey, TEXT("CLSID"), 0, cszRegSz,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL) == ERROR_SUCCESS)
{
// Set default value for registry key.
RegSetValueEx(hSubKey, NULL, 0, REG_SZ, (LPBYTE)szObjGuid,
lstrlen(szObjGuid) + 1);
RegCloseKey(hSubKey);
}
RegCloseKey(hKey);
}
// Create the registry entry name for object using it CLSID.
lstrcat(lstrcpy(szKeyName, TEXT("CLSID\\")), szObjGuid);
// Create registry entry HKEY_CLASSES_ROOT\CLSID\{clsid}.
if (RegCreateKeyEx(HKEY_CLASSES_ROOT, szKeyName, 0, cszRegSz,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
{
// Set default value for registry key.
RegSetValueEx(hKey, NULL, 0, REG_SZ, (LPBYTE)pcszObjName,
lstrlen(pcszObjName) + 1);
// Set registry entry application identifier.
RegSetValueEx(hKey, TEXT("AppID"), 0, REG_SZ, (LPBYTE)szObjGuid,
lstrlen(szObjGuid) + 1);
// Create registry entry HKEY_CLASSES_ROOT\CLSID\{clsid}\InprocServer32.
if (RegCreateKeyEx(hKey, TEXT("InprocServer32"), 0, cszRegSz,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL) == ERROR_SUCCESS)
{
TCHAR szDllName[MAX_PATH];
const TCHAR cszApartment[] = TEXT("Apartment");
// Get shared library module name.
GetModuleFileName(g_hModule, szDllName, sizeof(szDllName));
// Set default value for registry key.
RegSetValueEx(hSubKey, NULL, 0, REG_SZ, (LPBYTE)szDllName,
lstrlen(szDllName) + 1);
// Set threading model value for registry entry.
RegSetValueEx(hSubKey, TEXT("ThreadingModel"), 0, REG_SZ,
(LPBYTE)cszApartment, sizeof(cszApartment));
RegCloseKey(hSubKey);
}
// Close registry entry.
RegCloseKey(hKey);
}
}
//===========================================================================
//
// ::DeleteComObject
//
// Purpose: Remove the registry entries for COM object.
//
// Parameters:
// rObjGuid REFGUID object CLSID.
// pcszRootName LPCTSTR name of object in HKEY_CLASSES_ROOT.
//
//===========================================================================
static void DeleteComObject(REFGUID rObjGuid, LPCTSTR pcszRootName)
{
#ifdef _DEBUG
TCHAR szGuid[64];
LogMsg(TEXT("DeleteComObject(%s,%s)"),
GuidToStr(rObjGuid, szGuid, sizeof(szGuid)), pcszRootName);
_ASSERTE(pcszRootName && !IsBadReadPtr(pcszRootName, sizeof(*pcszRootName)));
#endif
TCHAR szObjGuid[64];
TCHAR szKeyName[128];
HKEY hKey;
// Open registry entry HKEY_CLASSES_ROOT\<Root Name>.
if (RegOpenKeyEx(HKEY_CLASSES_ROOT, pcszRootName, 0, KEY_WRITE, &hKey) == ERROR_SUCCESS)
{
// Delete registry entry HKEY_CLASSES_ROOT\<Root Name>\CLSID.
RegDeleteKey(hKey, TEXT("CLSID"));
RegCloseKey(hKey);
// Delete registry entry HKEY_CLASSES_ROOT\<Root Name>.
RegDeleteKey(HKEY_CLASSES_ROOT, pcszRootName);
}
// Create the registry entry name for object using it CLSID.
GuidToStr(rObjGuid, szObjGuid, sizeof(szObjGuid));
lstrcat(lstrcpy(szKeyName, TEXT("CLSID\\")), szObjGuid);
// Open registry entry HKEY_CLASSES_ROOT\CLSID\{clsid}.
if (RegOpenKeyEx(HKEY_CLASSES_ROOT, szKeyName, 0, KEY_WRITE, &hKey) == ERROR_SUCCESS)
{
// Delete registry entry HKEY_CLASSES_ROOT\CLSID\{clsid}\InprocServer32.
RegDeleteKey(hKey, TEXT("InprocServer32"));
RegCloseKey(hKey);
// Delete registry entry HKEY_CLASSES_ROOT\CLSID\{clsid}.
RegDeleteKey(HKEY_CLASSES_ROOT, szKeyName);
}
}
//===========================================================================
//
// ::DllMain
//
// Purpose: Entry point for DLL.
//
// Parameters:
// hInstance HANDLE instance of DLL module.
// dwReason DWORD reason of call.
// pvReserved LPVOID reserved.
//
// Return: TRUE if OK.
//
//===========================================================================
BOOL WINAPI DllMain(HANDLE hInstance, DWORD dwReason, LPVOID pvReserved)
{
// Save library module handle and disable thread notification calls.
if (DLL_PROCESS_ATTACH == dwReason)
{
#ifdef _DEBUG
// Load DirectSound shared library DSOUND.DLL.
HINSTANCE hDsDll = LoadLibrary(TEXT("dsound.dll"));
if (!hDsDll)
return FALSE;
// Get address of the GetDeviceID function.
FARPROC pfnGetDeviceID = GetProcAddress(hDsDll, "GetDeviceID");
// Free DirectSound shared library.
FreeLibrary(hDsDll);
// Check existing of entry point.
if (!pfnGetDeviceID)
return FALSE;
#endif
// Save handle of shared library module.
g_hModule = (HMODULE)hInstance;
DisableThreadLibraryCalls(g_hModule);
}
#ifdef _DEBUG
LogMsg(TEXT("DllMain(%#x,%u)"), hInstance, dwReason);
#endif
return TRUE;
}
//===========================================================================
//
// ::DllCanUnloadNow
//
// Purpose: Called periodically by OLE in order to determine if the
// DLL can be freed.
//
// Return: S_OK if there are no objects in use and the class factory
// isn't locked.
//
//===========================================================================
STDAPI DllCanUnloadNow()
{
#ifdef _DEBUG
LogMsg(TEXT("DllCanUnloadNow()=(%u,%u)"), g_cLock, g_cObj);
#endif
// Check locks and objects counters.
if (g_cLock || g_cObj)
return S_FALSE;
return S_OK;
}
//===========================================================================
//
// ::DllGetClassObject
//
// Purpose: Called by OLE when some client wants a class factory. Return
// one only if it is the sort of class this DLL supports.
//
//===========================================================================
STDAPI DllGetClassObject(REFCLSID rClsid, REFIID rIid, LPVOID * ppvObj)
{
#ifdef _DEBUG
TCHAR szGuid[64];
LogMsg(TEXT("DllGetClassObject(%s)..."),
GuidToStr(rClsid, szGuid, sizeof(szGuid)));
_ASSERTE(ppvObj && !IsBadWritePtr(ppvObj, sizeof(*ppvObj)));
#endif
// Check arguments values.
if (!ppvObj)
return E_POINTER;
// For future invalid return.
*ppvObj = NULL;
#ifdef _DEBUG
if (CLSID_A3d != rClsid && CLSID_A3dDal != rClsid)
LogMsg(TEXT("...DllGetClassObject()=E_FAIL!"));
#endif
// Check arguments values.
if (CLSID_A3d != rClsid && CLSID_A3dDal != rClsid)
return E_FAIL;
// Create new A3dClassFactory object.
LPA3DCLASSFACTORY pA3dClassFactory = new IA3dClassFactory;
if (!pA3dClassFactory)
return E_OUTOFMEMORY;
// Kill the object if initial creation failed.
HRESULT hr = pA3dClassFactory->QueryInterface(rIid, ppvObj);
if (FAILED(hr))
delete pA3dClassFactory;
#ifdef _DEBUG
LogMsg(TEXT("...=%s"), Result(hr));
#endif
return hr;
}
//===========================================================================
//
// ::DllRegisterServer
//
// Purpose: Called during setup or by regsvr32.
//
// Return: S_OK if unregistration successful, error otherwise.
//
//===========================================================================
STDAPI DllRegisterServer()
{
#ifdef _DEBUG
LogMsg(TEXT("DllRegisterServer()"));
#endif
CreateComObject(CLSID_A3d, TEXT("A3d"), TEXT("A3d Object"));
CreateComObject(CLSID_A3dDal, TEXT("A3dDAL"), TEXT("A3dDAL Object"));
return S_OK;
}
//===========================================================================
//
// ::DllUnregisterServer
//
// Purpose: Called when it is time to remove the registry entries.
//
// Return: S_OK if unregistration successful, error otherwise.
//
//===========================================================================
STDAPI DllUnregisterServer()
{
#ifdef _DEBUG
LogMsg(TEXT("DllUnregisterServer()"));
#endif
DeleteComObject(CLSID_A3d, TEXT("A3d"));
DeleteComObject(CLSID_A3dDal, TEXT("A3dDAL"));
return S_OK;
}
//===========================================================================
//
// ::A3dCreate
//
// Purpose: Create new A3dSound object.
//
// Parameters:
// pGuidDevice LPGUID identifying the device the caller
// desires to have for the new object.
// ppDS LPLPDIRECTSOUND in which to store the desired
// interface pointer for the new object.
// pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
// being used in an aggregation.
//
// Return: A3D_OK if successful, error otherwise.
//
//===========================================================================
HRESULT WINAPI A3dCreate(LPGUID pGuidDevice, LPDIRECTSOUND * ppDS, LPUNKNOWN pUnkOuter)
{
#ifdef _DEBUG
TCHAR szGuid[64];
LogMsg(TEXT("A3dCreate(%s,%#x,%#x)"), GuidToStr((REFGUID)*pGuidDevice,
szGuid, sizeof(szGuid)), ppDS, pUnkOuter);
_ASSERTE(ppDS && !IsBadWritePtr(ppDS, sizeof(*ppDS)));
_ASSERTE(!pUnkOuter);
#endif
HKEY hKey;
// Open registry key for A3D.
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Aureal\\A3D"), 0,
KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
INT iForceDS = 0;
DWORD dwLength = sizeof(iForceDS);
// Read value from registry key.
RegQueryValueEx(hKey, TEXT("ForceDS"), NULL, NULL, (LPBYTE)&iForceDS, &dwLength);
RegCloseKey(hKey);
// Force using DirectSound.
if (iForceDS)
{
// Standard beep using the computer speaker.
MessageBeep(-1);
// Get initialized DirectSound object.
HRESULT hr = DirectSoundCreate(pGuidDevice, ppDS, NULL);
if (SUCCEEDED(hr) && (iForceDS > 1))
return A3D_OK;
return hr;
}
}
// Check arguments values.
if (!ppDS)
return E_POINTER;
// For future invalid return.
*ppDS = NULL;
// This object doesnt support aggregation.
if (pUnkOuter)
return CLASS_E_NOAGGREGATION;
LPDIRECTSOUND pDS;
// Get initialized DirectSound object.
HRESULT hr = DirectSoundCreate(pGuidDevice, &pDS, NULL);
if (FAILED(hr))
return hr;
// Create new A3dSound object.
LPA3DSOUND pA3dSound = new IA3dSound(pDS);
// Decrease reference of DirectSound object.
pDS->Release();
if (!pA3dSound)
return E_OUTOFMEMORY;
// Kill the object if initial creation failed.
hr = pA3dSound->QueryInterface(IID_IDirectSound, (LPVOID *)ppDS);
if (FAILED(hr))
{
delete pA3dSound;
return hr;
}
return A3D_OK;
}
//===========================================================================
//
// IA3dClassFactory::IA3dClassFactory
// IA3dClassFactory::~IA3dClassFactory
//
// Constructor Parameters:
// None
//
//===========================================================================
IA3dClassFactory::IA3dClassFactory() :
m_cRef(0)
{
#ifdef _DEBUG
LogMsg(TEXT("IA3dClassFactory::IA3dClassFactory()=%u"), g_cObj + 1);
#endif
// Increase object counter.
InterlockedIncrement(&g_cObj);
}
IA3dClassFactory::~IA3dClassFactory()
{
#ifdef _DEBUG
LogMsg(TEXT("IA3dClassFactory::~IA3dClassFactory()=%u"), g_cObj - 1);
_ASSERTE(g_cObj > 0);
#endif
// Decrease object counter.
InterlockedDecrement(&g_cObj);
}
//===========================================================================
//
// IA3dClassFactory::QueryInterface
// IA3dClassFactory::AddRef
// IA3dClassFactory::Release
//
// Purpose: Standard OLE routines needed for all interfaces.
//
//===========================================================================
STDMETHODIMP IA3dClassFactory::QueryInterface(REFIID rIid, LPVOID * ppvObj)
{
#ifdef _DEBUG
TCHAR szGuid[64];
LogMsg(TEXT("IA3dClassFactory::QueryInterface(%s,%#x)"),
GuidToStr(rIid, szGuid, sizeof(szGuid)), ppvObj);
_ASSERTE(ppvObj && !IsBadWritePtr(ppvObj, sizeof(*ppvObj)));
#endif
// Check arguments values.
if (!ppvObj)
return E_POINTER;
// For future invalid return.
*ppvObj = NULL;
#ifdef _DEBUG
if ((IID_IUnknown != rIid) && (IID_IClassFactory != rIid))
LogMsg(TEXT("...QueryInterface()=E_NOINTERFACE!"));
#endif
// Only Unknown or ClassFactory objects.
if ((IID_IUnknown != rIid) && (IID_IClassFactory != rIid))
return E_NOINTERFACE;
// Return this object;
*ppvObj = this;
AddRef();
return S_OK;
}
STDMETHODIMP_(ULONG) IA3dClassFactory::AddRef()
{
#ifdef _DEBUG
LogMsg(TEXT("IA3dClassFactory::AddRef()=%u"), m_cRef + 1);
_ASSERTE(m_cRef >= 0);
#endif
// Increase reference counter.
return InterlockedIncrement(&m_cRef);
}
STDMETHODIMP_(ULONG) IA3dClassFactory::Release()
{
#ifdef _DEBUG
LogMsg(TEXT("IA3dClassFactory::Release()=%u"), m_cRef - 1);
_ASSERTE(m_cRef > 0);
#endif
// Decrease reference counter.
ULONG cRef = InterlockedDecrement(&m_cRef);
if (!cRef)
{
delete this;
return 0;
}
return cRef;
}
//===========================================================================
//
// IA3dClassFactory::CreateInstance
//
// Purpose: Instantiates a Locator object returning an interface pointer.
//
// Parameters:
// pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
// being used in an aggregation.
// rIid REFIID identifying the interface the caller
// desires to have for the new object.
// ppvObj PPVOID in which to store the desired
// interface pointer for the new object.
//
// Return: S_OK if successful, error otherwise.
//
//===========================================================================
STDMETHODIMP IA3dClassFactory::CreateInstance(LPUNKNOWN pUnkOuter,
REFIID rIid, LPVOID * ppvObj)
{
#ifdef _DEBUG
TCHAR szGuid[64];
LogMsg(TEXT("IA3dClassFactory::CreateInstance(%s)"),
GuidToStr(rIid, szGuid, sizeof(szGuid)));
_ASSERTE(ppvObj && !IsBadWritePtr(ppvObj, sizeof(*ppvObj)));
_ASSERTE(!pUnkOuter);
#endif
// Check arguments values.
if (!ppvObj)
return E_POINTER;
// For future invalid return.
*ppvObj = NULL;
// This object doesnt support aggregation.
if (pUnkOuter)
return CLASS_E_NOAGGREGATION;
LPUNKNOWN pObj;
#ifdef _DEBUG
if ((IID_IUnknown != rIid) && (IID_IClassFactory != rIid) &&
(IID_IA3d != rIid) && (IID_IA3d2 != rIid) &&
(IID_IDirectSound != rIid) && (IID_IA3dDal != rIid))
LogMsg(TEXT("...QueryInterface()=E_NOINTERFACE!"));
#endif
// Create the locator object.
if ((IID_IUnknown == rIid) || (IID_IClassFactory == rIid))
pObj = new IA3dClassFactory;
else if ((IID_IA3d == rIid) || (IID_IA3d2 == rIid))
pObj = new IA3dX(NULL);
else if (IID_IDirectSound == rIid)
pObj = new IA3dSound(NULL);
else if (IID_IA3dDal == rIid)
pObj = new IA3dDal(NULL);
else
return E_NOINTERFACE;
if (!pObj)
return E_OUTOFMEMORY;
// Kill the object if initial creation failed.
HRESULT hr = pObj->QueryInterface(rIid, ppvObj);
if (FAILED(hr))
delete pObj;
return hr;
}
//===========================================================================
//
// IA3dClassFactory::LockServer
//
// Purpose:
// Increments or decrements the lock count of the DLL. If the
// lock count goes to zero and there are no objects, the DLL
// is allowed to unload. See DllCanUnloadNow.
//
// Parameters:
// fLock BOOL specifying whether to increment or
// decrement the lock count.
//
// Return: S_OK always.
//
//===========================================================================
STDMETHODIMP IA3dClassFactory::LockServer(BOOL fLock)
{
#ifdef _DEBUG
LogMsg(TEXT("IA3dClassFactory::LockServer(%u,%u)"), g_cLock, fLock);
#endif
// Increase or decrease the lock counter.
if (fLock)
InterlockedIncrement(&g_cLock);
else
InterlockedDecrement(&g_cLock);
return S_OK;
}
//===========================================================================
//
// IA3dX::CreateKsPropertySet
//
// Purpose: Get KsPropertySet object.
//
// Return: S_OK if successful, error otherwise.
//
//===========================================================================
STDMETHODIMP IA3dX::CreateKsPropertySet()
{
#ifdef _DEBUG
LogMsg(TEXT("IA3dX::CreateKsPropertySet()..."));
_ASSERTE(m_pDS);
#endif
HRESULT hr;
WAVEFORMATEX wfxFormat;
ZeroMemory(&wfxFormat, sizeof(wfxFormat));
wfxFormat.wFormatTag = WAVE_FORMAT_PCM;
DSBUFFERDESC DSBufDesc;
ZeroMemory(&DSBufDesc, sizeof(DSBufDesc));
DSBufDesc.dwSize = sizeof(DSBufDesc);
DSBufDesc.dwFlags = DSBCAPS_LOCHARDWARE | DSBCAPS_CTRL3D;
DSBufDesc.dwBufferBytes = DSBSIZE_MIN;
DSBufDesc.lpwfxFormat = &wfxFormat;
// Loop for channels: 1 or 2.
for (WORD wChannels = 1; wChannels <= 2; wChannels++)
{
wfxFormat.nChannels = wChannels;
// Loop for samples per second: see list below.
const DWORD dwSps[] = {8000, 11025, 22050, 44100, 48000};
for (UINT i = 0; i < (sizeof(dwSps) / sizeof(dwSps[0])); i++)
{
wfxFormat.nSamplesPerSec = dwSps[i];
// Loop for bits per sample: 8 or 16.
for (WORD wBps = 8; wBps <= 16; wBps += 8)
{
wfxFormat.wBitsPerSample = wBps;
wfxFormat.nBlockAlign = wfxFormat.wBitsPerSample / 8 * wfxFormat.nChannels;
wfxFormat.nAvgBytesPerSec = wfxFormat.nSamplesPerSec * wfxFormat.nBlockAlign;
LPDIRECTSOUNDBUFFER pDSB;
// Create new DirectSoundBuffer object.
hr = m_pDS->CreateSoundBuffer(&DSBufDesc, &pDSB, NULL);
if (SUCCEEDED(hr))
{
// Get KsPropertySet object.
hr = pDSB->QueryInterface(IID_IKsPropertySet, (LPVOID *)&m_pKsPS);
// Delete DirectSoundBuffer object.
pDSB->Release();
#ifdef _DEBUG
if (SUCCEEDED(hr))
{
LogMsg(TEXT("...Channels=%u"), wfxFormat.nChannels);
LogMsg(TEXT("...SamplesPerSec=%u"), wfxFormat.nSamplesPerSec);
LogMsg(TEXT("...AvgBytesPerSec=%u"), wfxFormat.nAvgBytesPerSec);
LogMsg(TEXT("...BlockAlign=%u"), wfxFormat.nBlockAlign);
LogMsg(TEXT("...BitsPerSample=%u"), wfxFormat.wBitsPerSample);
}
LogMsg(TEXT("...=%s"), Result(hr));
#endif
return hr;
}
}
}
}
#ifdef _DEBUG
LogMsg(TEXT("...=%s"), Result(hr));
#endif
return hr;
}
//===========================================================================
//
// IA3dX::SetOemResourceManagerMode
//
// Purpose: Set resource manager mode for OEM.
//
// Parameters:
// dwResManMode DWORD resource manager mode.
//
// Return: S_OK if successful, error otherwise.
//
//===========================================================================
STDMETHODIMP IA3dX::SetOemResourceManagerMode(DWORD dwResManMode)
{
#ifdef _DEBUG
LogMsg(TEXT("IA3dX::SetOemResourceManagerMode(%u)..."), dwResManMode);
_ASSERTE(m_pDS);
#endif
// Create KsPropertySet object.
HRESULT hr = (m_pKsPS) ? S_OK : CreateKsPropertySet();
if (SUCCEEDED(hr))
{
ULONG ulTypeSupport;
DWORD dwPropertyMode;
// Check arguments values.
switch (dwResManMode)
{