Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi ,
I am working on c++ application.
we are using Beep() function to produce sound.But we are able to hear the sound from speaker but not from buzzer.


can u give me the code or file by which we can call it so that we can hear buzzer sound.

What I have tried:

We have tried c++ Beep() to produce the sound But its coming from speaker but not from buzzer. My requirement is sound should come from buzzer.
Posted
Updated 5-Sep-22 2:41am
Comments
Member 15757860 6-Sep-22 2:18am    
code is like:
--------------------------------------------------.cpp -----------------------------------------
#include <iostream>
#include <stdlib.h>
#include "inpout32.h"


//[DllImport("inpout32.dll")]
//extern void Out32(short PortAddress, short Data);
//[DllImport("inpout32.dll")]
//extern char Inp32(short PortAddress);

void StopBeep()
{
Out32(0x61, (System::Byte)(System::Convert::ToByte(Inp32(0x61)) & 0xFC));
}
void Beep(unsigned int freq, int ms)
{
Out32(0x43, 0xB6);
int div = 0x1234dc / freq;
Out32(0x42, (System::Byte)(div & 0xFF));
Out32(0x42, (System::Byte)(div >> 8));
System::Threading::Thread::Sleep(10);
Out32(0x61, (System::Byte)(System::Convert::ToByte(Inp32(0x61)) | 0x03));
System::Threading::Thread::Sleep(ms);
StopBeep();
}



-----------------------------------------------------------------------------------------------
inpout32.h



/**********************************************************************************\
* Defines InpOut32.dll and InpOutX64.dll for direct and GetProcAddress() linking *
\**********************************************************************************/
/* Change log:
1304xx created
+130819 Additional DlPortIo.DLL entries (stubs) for compatibility to PCS500
+130819 IO.DLL entries (stubs)
-190515 _declspec -> __declspec
*/

#pragma once

#include <windows.h>
#ifdef INPOUT_EXPORTS
# define FUNC(ret,name,args) EXTERN_C ret WINAPI name args
typedef struct QUEUE *HQUEUE; // virtual class pointer
#else
/* FUNC() will generate import function prototypes AND function pointer types
* for the application programmer.
* Function prototypes are useful for importing this library using inpout32.lib.
* Drawback: The inpout32.dll is required, otherwise, the application will not start.
*
* Function pointer types are suffixed with "_t". These are useful for defining
* pointers to funtions and to cast the result of GetProcAddress().
* Drawback: This requires more coding effort for the application programmer.
*
* I have never used the "delayed import" feature (available since Visual C++ 2005).
*/
# define FUNC(ret,name,args) EXTERN_C __declspec(dllimport) ret WINAPI name args;\
typedef ret(WINAPI*name##_t)args
DECLARE_HANDLE(HQUEUE); // opaque to user of inpout.h
#endif

/***************\
** Basic I/O **
\***************/

/* Basic BYTE input/output
* Access to old-style LPT addresses 0x378 (LPT1), 0x278 (LPT2), and 0x3BC (LPT3)
* are automatically redirected to PCI and PCIexpress cards.
* If no true parallel port available for those addresses, redirection applies
* to USB2LPT (native or HID API) and USB->ParallelPrinter adapter
* You can use space-saving numerical imports like GetProcAddress(h,MAKEINTRESOURCE(1))
* safely, as these numbers will never change
*/
FUNC(BYTE, Inp32, (WORD addr)); // @1
FUNC(void, Out32, (WORD addr, BYTE value)); // @2
FUNC(BOOL, IsInpOutDriverOpen, (void)); // @3

/********************************\
** DlPortIo.DLL compatibility **
\********************************/

/* BYTE access is redirected too (as above), but WORD/DWORD not.
* Not recommended for new software.
* Import by number is not recommended from here and below.
*/
FUNC(BYTE, DlPortReadPortUchar, (WORD addr)); // @4
FUNC(DWORD,DlPortReadPortUlong, (WORD addr)); // @5
FUNC(WORD, DlPortReadPortUshort, (WORD addr)); // @6
FUNC(void, DlPortWritePortBufferUchar, (WORD, const UCHAR*, ULONG)); // @13
FUNC(void, DlPortWritePortBufferUlong, (WORD, const ULONG*, ULONG)); // @14
FUNC(void, DlPortWritePortBufferUshort, (WORD, const USHORT*, ULONG)); // @15
FUNC(void, DlPortWritePortUchar, (WORD addr, BYTE value)); // @10
FUNC(void, DlPortWritePortUshort, (WORD addr, WORD value)); // @11
FUNC(void, DlPortWritePortUlong, (WORD addr, DWORD value)); // @12
FUNC(void, DlPortReadPortBufferUchar, (WORD, PUCHAR, ULO

1 solution

I fear the only option (if feasible) you have is going lower level, bypassing the Beep function, see the remarks section here:Beep function (utilapiset.h) - Win32 apps | Microsoft Docs[^].
 
Share this answer
 
Comments
Member 15757860 6-Sep-22 1:46am    
can we bypass Beep() function so that Beep() can produce sound from buzzer instead of speaker?
CPallini 6-Sep-22 2:00am    
I found this:

https://www.codeproject.com/Tips/1022207/Make-Buzzer-Internal-Speaker-Sound-in-Windows-bit

however I don't know if it would actually work in your scenario:
Member 15757860 6-Sep-22 2:15am    
after implementig there, I have below build issue.

Build started...
1>------ Build started: Project: ConsoleApplication4, Configuration: Release Win32 ------
1>ConsoleApplication4.obj : error LNK2001: unresolved external symbol "extern "C" unsigned char __stdcall Inp32(unsigned short)" (?Inp32@@$$J14YGEG@Z)
1>ConsoleApplication4.obj : error LNK2001: unresolved external symbol "extern "C" void __stdcall Out32(unsigned short,unsigned char)" (?Out32@@$$J18YGXGE@Z)
1>C:\Users\3593963\source\repos\ConsoleApplication4\Release\ConsoleApplication4.exe : fatal error LNK1120: 2 unresolved externals
1>Done building project "ConsoleApplication4.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900