Solved

declaring variables (normally easy question)

Posted on 2004-04-14
7
125 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
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 and use conditional statements in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now