Say I would call the kernel function 'Beep' under the name 'MyBeep' and I want to provide a header file to this:
// main.c
#include <stdio.h>
#include "mybeep.h"
int __cdecl main(int argc, char *argv[])
{
MyBeep(1000, 1000);
return 0;
}
// mybeep.h
#ifndef _MYBEEP_H
#define _MYBEEP_H
#include <windows.h>
BOOL WINAPI MyBeep(DWORD dwFreq, DWORD dwDuration);
#endif // _MYBEEP_H
// mybeep.c
#include "mybeep.h"
typedef BOOL (WINAPI *BEEP_PROC)(DWORD dwFreq, DWORD dwDuration);
void __cdecl mybeep__open(void);
#pragma startup mybeep__open
BEEP_PROC MyBeep;
void __cdecl mybeep__open(void)
{
HMODULE hKernel32 = LoadLibrary("kernel32.dll");
if (hKernel32) {
MyBeep = (BEEP_PROC) GetProcAddress(hKernel32, "Beep");
}
}
This doesn't work!
I get "error #2120: Redeclaration of 'MyBeep', previously declared at E:\wwl\c\src\mru\mru.h(21); expected 'int __stdcall function(unsigned long int, unsigned long int)' but found 'int __stdcall (*)(unsigned long int, unsigned long int)'."
With one step of indirection it works
// mybeep.c
#include "mybeep.h"
typedef BOOL (WINAPI *BEEP_PROC)(DWORD dwFreq, DWORD dwDuration);
void __cdecl mybeep__open(void);
#pragma startup mybeep__open
BEEP_PROC _MyBeep;
void __cdecl mybeep__open(void)
{
HMODULE hKernel32 = LoadLibrary("kernel32.dll");
if (hKernel32) {
_MyBeep = (BEEP_PROC) GetProcAddress(hKernel32, "Beep");
}
}
BOOL WINAPI MyBeep(DWORD dwFreq, DWORD dwDuration)
{
return _MyBeep(dwFreq, dwDuration);
}
Is there a solution to avoid this additional step?