Solved

declaring variables (normally easy question)

Posted on 2004-04-14
7
130 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
[X]
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
  • 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

Suggested Solutions

Title # Comments Views Activity
How to get time_t value that is a week old 6 174
C hashtable library 3 113
Problem to scan all sheets 3 134
SQL Server 2008 R2, need a pivot/cross tab query... 4 76
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

734 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