Solved

declaring variables (normally easy question)

Posted on 2004-04-14
7
129 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

756 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