C++ function pointers versus C function pointer problem 
Author Message
 C++ function pointers versus C function pointer problem

 typedef BOOL (*STDCALL TSortStatusProc)(int *pos, void *userData);

I think the problem is this prototype. The STDCALL attribute is applied to
the pointer and not to the function which does not make sense and is
ignored. Indeed you should have gotten a warning for this. The standard
calling convention for C defaults to _cdecl and for C++ to __stdcall, so
you get different conventions if the modules are compiled in different
languages. Could you try this:

 typedef BOOL (__stdcall * TSortStatusProc)(int *pos, void *userData);



Mon, 31 Jan 2000 03:00:00 GMT  
 C++ function pointers versus C function pointer problem

Quote:

> Hi all,

> I am having a bit of a problem with function pointer declarations in C
> and
> C++.  My library has to work in both C and C++.  Now, the following .h
> and
> .cpp files compile and work under a CPP file, but not under a C file.
> The
> function call back pointer in the header seems to be one of the main
> culprits.

The problem is not the functions but the special namespace around them.

BTW, it is a little known fact of C(++) that callbacks should be
'static'. This proves the only way another "module" gets its paws on
them is by passing a pointer to a function. Your strategy is okay if
someone outside this translation unit really needs the functions by
name.

Quote:
> <<<nitrodll.h>>>

> #ifndef __AFXWIN_H__
>         #error include 'stdafx.h' before including this file for PCH
> #endif

> #define STDCALL __stdcall

> typedef BOOL (*STDCALL TSortStatusProc)(int *pos, void *userData);

I'm the C compiler... Okay, I can handle BOOL and STDCALL and stuff...

Quote:
> extern "C" {

Eeek! what's that? That's not C!

Quote:
> int STDCALL Sort(LPCSTR cmdStmt, LPSTR errorStr, int errorLen,
>                         TSortStatusProc statusProc, void *userData);
> int STDCALL SortMem(void *memBlock, int memSize, LPSTR cmdStmt,
>                         LPSTR errorStr, int errorLen);
> int STDCALL SortMemByPtrs(void *memPtrs, int numRecs, LPCSTR cmdStmt,
>                         LPSTR errorStr, int errorLen);

> }

> <<<end nitrodll.h>>>

> <<<begin nitrodll.cpp>>>

> #include "stdafx.h"
> #include "nitrodll.h"
> #include <winbase.h>

> #ifdef _DEBUG
> #define new DEBUG_NEW
> #undef THIS_FILE
> static char THIS_FILE[] = __FILE__;
> #endif

> // Procedure pointers.
> typedef int  ( far Pascal *TDEFSort)
>       (LPCSTR, LPSTR, int, TSortStatusProc, LPVOID );
> typedef int  ( far PASCAL *TDEFSortMem)
>       (LPVOID, int, LPCSTR , LPSTR , int );
> typedef int  ( far PASCAL *TDEFSortMemPtrs)
>       (LPVOID, int, LPCSTR , LPSTR , int );

> static TDEFSort dynSort = 0 ;
> static TDEFSortMem dynSortMem = 0 ;
> static TDEFSortMemPtrs dynSortMemPtrs = 0 ;

> int STDCALL Sort(LPCSTR cmdStmt, LPSTR errorStr, int errorLen,
>                              TSortStatusProc statusProc, void
> *userData)
> {
>    HMODULE hSort = 0;
>    int tmpInt = 0;

>    hSort = LoadLibrary("nitro.dll");
>    dynSort = (TDEFSort) GetProcAddress(hSort,"Sort");
>    tmpInt = dynSort(cmdStmt, errorStr, errorLen, statusProc,
> userData);
>    FreeLibrary(hSort);

>    return tmpInt;
> }

> int STDCALL SortMem(void *memBlock, int memSize, LPSTR cmdStmt,
>                                 LPSTR errorStr, int errorLen)
> {
>    HMODULE hSort = 0;
>    int tmpInt = 0;

>    hSort = LoadLibrary("nitro.dll");
>    dynSortMem = (TDEFSortMem) GetProcAddress(hSort,"SortMem");
>    tmpInt = dynSortMem(memBlock, memSize, cmdStmt, errorStr,
> errorLen);
>    FreeLibrary(hSort);

>    return tmpInt;
> }

> int STDCALL SortMemByPtrs(void *memPtrs, int numRecs, LPCSTR cmdStmt,
>                                       LPSTR errorStr, int errorLen)
> {

In C++, this needs the same linkage as its declaration in the header.
Therefore you need 'extern "C"' here too.

    extern "C" int STDCALL SortMemByPtrs(void *memPtrs, int numRecs,
LPCSTR ...

Finally, to hid this thing from the C compiler, wrap it like this:

    #ifdef __cplusplus
        extern "C"
    #endif

To hide it in the header,

    #ifdef __cplusplus
        extern "C" {
    #endif

wrap the end-of-block the same way.

<snip>

  --  Phlip
====== http://users.deltanet.com/~tegan/home.html ======
  --  A card-carrying Microsoft LVP
         - Least Valuable Professional  --



Mon, 31 Jan 2000 03:00:00 GMT  
 C++ function pointers versus C function pointer problem

Quote:

> I am having a bit of a problem with function pointer declarations in C
> and
> C++.  My library has to work in both C and C++.  Now, the following .h
> and
> .cpp files compile and work under a CPP file, but not under a C file.
> The
> function call back pointer in the header seems to be one of the main
> culprits.  Any help on this would be greatly appreciated:

> #define STDCALL __stdcall

> typedef BOOL (*STDCALL TSortStatusProc)(int *pos, void *userData);

> extern "C" {
> int STDCALL Sort(LPCSTR cmdStmt, LPSTR errorStr, int errorLen,
>                         TSortStatusProc statusProc, void *userData);
> int STDCALL SortMem(void *memBlock, int memSize, LPSTR cmdStmt,
>                         LPSTR errorStr, int errorLen);
> int STDCALL SortMemByPtrs(void *memPtrs, int numRecs, LPCSTR cmdStmt,
>                         LPSTR errorStr, int errorLen);

> }

What errors do you get when it compiles?

Does it act differently if you put the typedef inside the extern "C"?
Also try changing "*STDCALL" in the typedef to "STDCALL*" and see what
happens.



Mon, 31 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to use (pointer to function), and function and pointer to (pointer to function)

2. Pointer Functions and Pointers to Pointer Functions

3. Question about signal()/pointers to functions that return pointers to functions

4. function pointers and function pointers array

5. Pointer to function returning pointer to function returning...

6. Problem assigning function to function pointer

7. Need help in function pointer problem in C++

8. Problem using C function pointers in C++ program

9. Problems Using Function Pointers in C++ Classes

10. How to pass a function pointer from Managed C++ to unmanaged c++

11. Pointers to Structure that contains pointer to Function

12. Casting function pointer to void pointer

 

 
Powered by phpBB® Forum Software