Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

declaring variables (normally easy question)

Posted on 2004-04-14
7
128 Views
Last Modified: 2010-04-15
Hi folks

i'm writing a WDM driver for a Pdrive and i tired of declaring each variable after the brackets.

this code : [extern "C"] should solve this normally in C language

extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,
  IN PUNICODE_STRING RegistryPath)
  {
  ...
  }

i get an error : error C2059: syntax error : 'string' on that line

that's it
0
Comment
Question by:human33
  • 3
7 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10824110
Hi human33,

The brackets {} go around the declaration.

extern "C"
  {
    NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,
       IN PUNICODE_STRING RegistryPath);
  ...
  }


Good Luck,
Kent
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 30 total points
ID: 10824377
Hi human33,
> extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,
>   IN PUNICODE_STRING RegistryPath)
>   {
>   ...
>   }

What do you want to do?

Do you want to declare a C++ function which uses C name mangling? Or do you want to call a C API function from C++?

In both cases, you wrap the functions or prototypes in an extern "C" { ... } wrapper, disabling C++ name mangling:

extern "C"{
    extern void some_c_api(void);
}

extern "C"{
    void cpp_func_called_by_c(void){
      cout << "In dummy C" << endl;
    }
}



Cheers,
Stefan
0
 

Author Comment

by:human33
ID: 10824473
owkey that works thx
but i don't want to call c++ function -> it's a driver i'm writing -> has nothing to do with application level
the manual from the windows DDK said to use extern "C" so you can declare variables everywhere you want
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10824674
human33,
Variables don't use mangling, so you don't have to use extern "C" with them. But if you have header files for your driver API, you should wrap your C prototypes by using

#ifdef  __cplusplus
extern "C" {
#endif

[...]

#ifdef  __cplusplus
}
#endif

...just to be on the safe side.


Stefan
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10824695
GCC uses a more elegant method which also fixes the namespace properly:

#ifdef __cplusplus
#if !(defined(_BEGIN_STD_C) && defined(_END_STD_C))
#ifdef _HAVE_STD_CXX
#define _BEGIN_STD_C namespace std { extern "C" {
#define _END_STD_C  } }
#else
#define _BEGIN_STD_C extern "C" {
#define _END_STD_C  }
#endif
#endif
#else
#define _BEGIN_STD_C
#define _END_STD_C
#endif

...then you only have to wrap your headers in
_BEGIN_STD_C
[...]
_END_STD_C

pairs.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question