简体   繁体   中英

Writing (Function Prototype, Function Pointer, Extern Pointer) Smaller

Is there a way to write a macro or typedef with some magic that I can write the three lines below smaller?

extern "C" NTSTATUS NTAPI KeInitializeApc( PKAPC Apc, 
                                           PKTHREAD thread,
                                           UCHAR state_index,
                                           PKKERNEL_ROUTINE ker_routine,
                                           PKRUNDOWN_ROUTINE rd_routine,
                                           PKNORMAL_ROUTINE nor_routine,
                                           UCHAR mode,
                                           PVOID context );
typedef NTSTATUS (NTAPI *KeInitializeApc_t)( PKAPC Apc, 
                                             PKTHREAD thread,
                                             UCHAR state_index,
                                             PKKERNEL_ROUTINE ker_routine,
                                             PKRUNDOWN_ROUTINE rd_routine,
                                             PKNORMAL_ROUTINE nor_routine,
                                             UCHAR mode,
                                             PVOID context );
extern "C" KeInitializeApc_t PKeInitializeApc;

Also, Do I have to use extern "C" on the prototype, function and extern? If I want no-mangled names?

extern "C" NTSTATUS NTAPI KeInitializeApc( PKAPC Apc, 
                                           PKTHREAD thread,
                                           UCHAR state_index,
                                           PKKERNEL_ROUTINE ker_routine,
                                           PKRUNDOWN_ROUTINE rd_routine,
                                           PKNORMAL_ROUTINE nor_routine,
                                           UCHAR mode,
                                           PVOID context );
extern "C" typedef NTSTATUS (NTAPI *KeInitializeApc_t)( PKAPC Apc, 
                                                        PKTHREAD thread,
                                                        UCHAR state_index,
                                                        PKKERNEL_ROUTINE ker_routine,
                                                        PKRUNDOWN_ROUTINE rd_routine,
                                                        PKNORMAL_ROUTINE nor_routine,
                                                        UCHAR mode,
                                                        PVOID context );
extern "C" KeInitializeApc_t PKeInitializeApc;

Using extern "C" on every single line doesn't seem correct.

Thanks for your time.

If you are able/allowed to use C++11, you could try using decltype as follows:

extern "C" {
    NTSTATUS NTAPI KeInitializeApc( PKAPC Apc, 
                                    PKTHREAD thread,
                                    UCHAR state_index,
                                    PKKERNEL_ROUTINE ker_routine,
                                    PKRUNDOWN_ROUTINE rd_routine,
                                    PKNORMAL_ROUTINE nor_routine,
                                    UCHAR mode,
                                    PVOID context );

    using KeInitializeApc_t = decltype(&KeInitializeApc);
    KeInitializeApc_t PKeInitializeApc;
}

EDIT: I missed the c tag there. If you want to write the code so it works both for C and C++, you could try:

#ifdef __cplusplus
extern "C" {
#endif

typedef NTSTATUS NTAPI KeInitializeApc_f( PKAPC Apc, 
                                          PKTHREAD thread,
                                          UCHAR state_index,
                                          PKKERNEL_ROUTINE ker_routine,
                                          PKRUNDOWN_ROUTINE rd_routine,
                                          PKNORMAL_ROUTINE nor_routine,
                                          UCHAR mode,
                                          PVOID context );
KeInitializeApc_f KeInitializeApc;
typedef KeInitializeApc_f *KeInitializeApc_t;
KeInitializeApc_t PKeInitializeApc;

#ifdef __cplusplus
}
#endif

You can group your definitions within one extern "C" like so:

#ifdef __cplusplus
extern "C" {
#endif

NTSTATUS NTAPI KeInitializeApc( PKAPC Apc, 
                                           PKTHREAD thread,
                                           UCHAR state_index,
                                           PKKERNEL_ROUTINE ker_routine,
                                           PKRUNDOWN_ROUTINE rd_routine,
                                           PKNORMAL_ROUTINE nor_routine,
                                           UCHAR mode,
                                           PVOID context );
typedef NTSTATUS (NTAPI *KeInitializeApc_t)( PKAPC Apc, 
                                                        PKTHREAD thread,
                                                        UCHAR state_index,
                                                        PKKERNEL_ROUTINE ker_routine,
                                                        PKRUNDOWN_ROUTINE rd_routine,
                                                        PKNORMAL_ROUTINE nor_routine,
                                                        UCHAR mode,
                                                        PVOID context );
KeInitializeApc_t PKeInitializeApc;

#ifdef __cplusplus
}
#endif

Is there a way to write a macro or typedef with some magic that I can write the three lines below smaller?

Perhaps you're looking for something like this:

#define NTDECLARE(name, args) \
  extern "C" NTSTATUS NTAPI name args; \
  extern "C" typedef NTSTATUS (NTAPI * name ## _t) args; \
  extern name ## _t P ## name;

NTDECLARE(KeInitializeApc, ( PKAPC Apc, 
                             PKTHREAD thread,
                             UCHAR state_index,
                             PKKERNEL_ROUTINE ker_routine,
                             PKRUNDOWN_ROUTINE rd_routine,
                             PKNORMAL_ROUTINE nor_routine,
                             UCHAR mode,
                             PVOID context ) )

The NTDECLARE macro can, of course, be used to declare other function, type, pointer trios with different names and possibly parameters.

Also, Do I have to use extern "C" on the prototype, function and extern?

You should not need to apply extern "C" to the pointer declaration, but if the code is intended for a header file then you probably do need a plain extern , as shown. You could consider omitting the extern "C" from all the individual declarations and instead putting them all in an extern "C" { ... } block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM