Solved

declaring variables (normally easy question)

Posted on 2004-04-14
7
127 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:Kdo
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

803 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