NO

Author Topic: Header file and function pointer  (Read 3248 times)

czerny

  • Guest
Header file and function pointer
« on: September 08, 2014, 03:09:33 PM »
Say I would call the kernel function 'Beep' under the name 'MyBeep' and I want to provide a header file to this:

Code: [Select]
// main.c
#include <stdio.h>
#include "mybeep.h"

int __cdecl main(int argc, char *argv[])
{
        MyBeep(1000, 1000);
return 0;
}

Code: [Select]
// mybeep.h
#ifndef _MYBEEP_H
#define _MYBEEP_H

#include <windows.h>

BOOL WINAPI MyBeep(DWORD dwFreq, DWORD dwDuration);

#endif // _MYBEEP_H

Code: [Select]
// 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

Code: [Select]
// 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?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Header file and function pointer
« Reply #1 on: September 08, 2014, 04:45:39 PM »
mybeep.c without mybeep.h and
linker flag:  /ALTERNATENAME:_MyBeep@8=_MyBeep ?
« Last Edit: September 10, 2014, 04:46:58 PM by TimoVJL »
May the source be with you

czerny

  • Guest
Re: Header file and function pointer
« Reply #2 on: September 08, 2014, 05:56:56 PM »
Sorry! I have not made it clear enough.

I do not want to change main.c nor mybeep.h!