DECLARE_CONST_UNICODE_STRING & WINDDK 8

Hi,

I’m porting the source code of a Windows 7 driver to WinDDK8 and i’m facing a problem related with DECLARE_CONST_UNICODE_STRING macro.

I’m doing as I was doing before:

#define DPX_DEVICE_STATE_REGISTRY L"DPX_DEVICE_STATE"
DECLARE_CONST_UNICODE_STRING(dpxState, DPX_DEVICE_STATE_REGISTRY)
status = WdfRegistryQueryValue(hKey,&dpxState,sizeof(DPX_DEVICE_STATE),&devContext->Context.State,NULL,NULL);

And now the compiler says:

1>usb.c(642): error C2143: syntax error : missing ‘;’ before ‘type’
1>usb.c(642): error C2143: syntax error : missing ‘;’ before ‘const’
1>usb.c(661): error C2065: ‘dpxState’ : undeclared identifier

I can’t understand the problem. Why does it not work in WinDDK?

Any ideas?

Thanks,

Nuno

xxxxx@imaginando.net wrote:

I’m porting the source code of a Windows 7 driver to WinDDK8 and i’m facing a problem related with DECLARE_CONST_UNICODE_STRING macro.

I’m doing as I was doing before:

#define DPX_DEVICE_STATE_REGISTRY L"DPX_DEVICE_STATE"
DECLARE_CONST_UNICODE_STRING(dpxState, DPX_DEVICE_STATE_REGISTRY)

Is that LITERALLY your code? You need a semicolon at the end. The
macro doesn’t add one. If you had two of these in a row, the second one
would get this error.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Hi Tim,

No, it’s is not a semicolon problem. I had the semicolon and I had take it out, and now that I saw your answer I put it again and nothing. It still complains about the same.

Can’t undersand this… Any ideas?

Thanks,

Nuno

On 03-May-2012 14:31, xxxxx@imaginando.net wrote:

Hi Tim,

No, it’s is not a semicolon problem. I had the semicolon and I had take it out, and now that I saw your answer I put it again and nothing. It still complains about the same.

Can’t undersand this… Any ideas?

Thanks,

Nuno

Yet another case of C vs C++ syndrome… Do you compile this as C or C++?

– pa

This is being compile as C file.

If I change the extension to .cpp a lot of more errors occur.

On 5/2/2012 6:34 PM, xxxxx@imaginando.net wrote:

1>usb.c(642): error C2143: syntax error : missing ‘;’ before ‘type’
1>usb.c(642): error C2143: syntax error : missing ‘;’ before ‘const’
1>usb.c(661): error C2065: ‘dpxState’ : undeclared identifier

I can’t understand the problem. Why does it not work in WinDDK?

Any ideas?

My psychic powers tell me you are missing an #include directive for
types like UNICODE_STRING.

Have a look at the preprocessor output to see what the compiler sees.

I searched for the header that contains the strcuture UNICODE_STRING and that lead me to

#include <ntsecapi.h>

However, after compiling a lot of more errors appeared.

How can I see pre processor output on Visual Studio? I can only see two combo box options BUILD/OUTPUT</ntsecapi.h>

On 5/3/2012 5:27 PM, xxxxx@imaginando.net wrote:

How can I see pre processor output on Visual Studio?

Project options -> C/C++ -> Preprocessor -> Preprocess to a File

Of course a driver developer does not need to be a good C/C++ programmer
to begin with. Or know their tools. We have wizards!

well i was about to post add /P or /EP to your C_DEFINES in your sources file

but then i didn;t know the wizards option :slight_smile:

On 5/3/12, Hagen Patzke wrote:
> On 5/3/2012 5:27 PM, xxxxx@imaginando.net wrote:
>> How can I see pre processor output on Visual Studio?
>
> Project options -> C/C++ -> Preprocessor -> Preprocess to a File
>
> Of course a driver developer does not need to be a good C/C++ programmer
> to begin with. Or know their tools. We have wizards!
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>

lazy evaluation! :slight_smile:

this was the pre processor output for that macro expansion

const WCHAR dpxState_buffer = L"DPX_DEVICE_STATE"; __pragma(warning(push)) __pragma(warning(disable:4221)) __pragma(warning(disable:4204)) const UNICODE_STRING dpxState = { sizeof(L"DPX_DEVICE_STATE") - sizeof(WCHAR), sizeof(L"DPX_DEVICE_STATE"), (PWCH) dpxState_buffer } __pragma(warning(pop));

You don’t need preprocessor output, just look in the headers for the definition. From ntdef.h

#define DECLARE_CONST_UNICODE_STRING(_var, _string) \
const WCHAR _var ## _buffer = _string; \
__pragma(warning(push)) \
__pragma(warning(disable:4221)) __pragma(warning(disable:4204)) \
const UNICODE_STRING _var = { sizeof(_string) - sizeof(WCHAR), sizeof(_string), (PWCH) _var ## _buffer } \
__pragma(warning(pop))

And another one that is equally useful

#define DECLARE_GLOBAL_CONST_UNICODE_STRING(_var, _str) \
extern const __declspec(selectany) UNICODE_STRING _var = RTL_CONSTANT_STRING(_str)

like tim said, you need a ; at the end if the statement. #include’ing wdm.h or ntddk.h will get you the right types that you need

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@imaginando.net
Sent: Thursday, May 03, 2012 9:04 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] DECLARE_CONST_UNICODE_STRING & WINDDK 8

lazy evaluation! :slight_smile:

this was the pre processor output for that macro expansion

const WCHAR dpxState_buffer = L"DPX_DEVICE_STATE"; __pragma(warning(push)) __pragma(warning(disable:4221)) __pragma(warning(disable:4204)) const UNICODE_STRING dpxState = { sizeof(L"DPX_DEVICE_STATE") - sizeof(WCHAR), sizeof(L"DPX_DEVICE_STATE"), (PWCH) dpxState_buffer } __pragma(warning(pop));


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Doron,

Thanks for your reply. However, it keeps complaining about the same error. Let me expose this detailed as possible:

This is file usb.c and the function is

NTSTATUS DpxMttEvtDeviceD0Entry(IN WDFDEVICE Device, IN WDF_POWER_DEVICE_STATE PreviousState);

I have DPX_DEVICE_STATE_REGISTRY defined in an header file in the following way:

#define DPX_DEVICE_STATE_REGISTRY L"DPX_DEVICE_STATE"

That header includes already wdf.h and ntddk.h, my code had always the semicolon.

When I call the MACRO i’m doing it in the following way:

NTSTATUS DpxMttEvtDeviceD0Entry(IN WDFDEVICE Device, IN WDF_POWER_DEVICE_STATE PreviousState)
{
PDEVICE_EXTENSION devContext = NULL;
NTSTATUS status = STATUS_SUCCESS;
WDF_OBJECT_ATTRIBUTES attributes;

UNREFERENCED_PARAMETER(PreviousState);

DECLARE_CONST_UNICODE_STRING(dpxState, DPX_DEVICE_STATE_REGISTRY);

status = WdfDeviceOpenRegistryKey(Device, PLUGPLAY_REGKEY_DEVICE, STANDARD_RIGHTS_ALL, WDF_NO_OBJECT_ATTRIBUTES, &hKey);

if (NT_SUCCESS (status))
{
//
// Getting dpx state persisted registry value
//

status = WdfRegistryQueryValue(hKey,&dpxState,sizeof(DPX_DEVICE_STATE),&devContext->Context.State,NULL,NULL);

if (!NT_SUCCESS (status))
{
KdPrint((“failed to restore previous dpx state”));
}
}

WdfRegistryClose(hKey);
}

I had this code working perfectly on WINDDK 7.

I’m only having this problem since porting to WINDDK 8 inside Visual Studio 11 Beta

The compiler says the following:

1>usb.c(642): error C2143: syntax error : missing ‘;’ before ‘type’
1>usb.c(642): error C2143: syntax error : missing ‘;’ before ‘const’

None of the present solutions result in success so far…

Thanks for your time,

With my best regards,

Nuno Santos

xxxxx@imaginando.net wrote:

No, it’s is not a semicolon problem. I had the semicolon and I had take it out, and now that I saw your answer I put it again and nothing. It still complains about the same.

Can’t undersand this… Any ideas?

I would think it was obvious that you have to show us the REAL code,
including the lines before and after. What you have said is basically
“I’m getting an error using API X. What did I do wrong?”


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

I will try to repro locally…BUT, you should really not be touching the registry in the power up/down path. Registry is slow, you want the power up path to be as fast as possible so that your device is up and running and the user gets their machine back quickly

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@imaginando.net
Sent: Thursday, May 03, 2012 9:44 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] DECLARE_CONST_UNICODE_STRING & WINDDK 8

Doron,

Thanks for your reply. However, it keeps complaining about the same error. Let me expose this detailed as possible:

This is file usb.c and the function is

NTSTATUS DpxMttEvtDeviceD0Entry(IN WDFDEVICE Device, IN WDF_POWER_DEVICE_STATE PreviousState);

I have DPX_DEVICE_STATE_REGISTRY defined in an header file in the following way:

#define DPX_DEVICE_STATE_REGISTRY L"DPX_DEVICE_STATE"

That header includes already wdf.h and ntddk.h, my code had always the semicolon.

When I call the MACRO i’m doing it in the following way:

NTSTATUS DpxMttEvtDeviceD0Entry(IN WDFDEVICE Device, IN WDF_POWER_DEVICE_STATE PreviousState) {
PDEVICE_EXTENSION devContext = NULL;
NTSTATUS status = STATUS_SUCCESS;
WDF_OBJECT_ATTRIBUTES attributes;

UNREFERENCED_PARAMETER(PreviousState);

DECLARE_CONST_UNICODE_STRING(dpxState, DPX_DEVICE_STATE_REGISTRY);

status = WdfDeviceOpenRegistryKey(Device, PLUGPLAY_REGKEY_DEVICE, STANDARD_RIGHTS_ALL, WDF_NO_OBJECT_ATTRIBUTES, &hKey);

if (NT_SUCCESS (status))
{
//
// Getting dpx state persisted registry value
//

status = WdfRegistryQueryValue(hKey,&dpxState,sizeof(DPX_DEVICE_STATE),&devContext->Context.State,NULL,NULL);

if (!NT_SUCCESS (status))
{
KdPrint((“failed to restore previous dpx state”));
}
}

WdfRegistryClose(hKey);
}

I had this code working perfectly on WINDDK 7.

I’m only having this problem since porting to WINDDK 8 inside Visual Studio 11 Beta

The compiler says the following:

1>usb.c(642): error C2143: syntax error : missing ‘;’ before ‘type’
1>usb.c(642): error C2143: syntax error : missing ‘;’ before ‘const’

None of the present solutions result in success so far…

Thanks for your time,

With my best regards,

Nuno Santos


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

xxxxx@imaginando.net wrote:

Thanks for your reply. However, it keeps complaining about the same error. Let me expose this detailed as possible:

This is file usb.c and the function is

NTSTATUS DpxMttEvtDeviceD0Entry(IN WDFDEVICE Device, IN WDF_POWER_DEVICE_STATE PreviousState)
{
PDEVICE_EXTENSION devContext = NULL;
NTSTATUS status = STATUS_SUCCESS;
WDF_OBJECT_ATTRIBUTES attributes;

UNREFERENCED_PARAMETER(PreviousState);

DECLARE_CONST_UNICODE_STRING(dpxState, DPX_DEVICE_STATE_REGISTRY);

status = WdfDeviceOpenRegistryKey(Device, PLUGPLAY_REGKEY_DEVICE, STANDARD_RIGHTS_ALL, WDF_NO_OBJECT_ATTRIBUTES, &hKey);

See? Now that you have shown us the real code, we can point out the
problem.

DECLARE_CONST_UNICODE_STRING is a declaration – it declares a variable
(two, actually). In C (but not in C++), all declarations must occur
before all executable statements. The tricky thing here is that the
UNREFERENCED_PARAMETER will result in this:
PreviousState;
Because PreviousState is a parameter, that’s not a default declaration,
but is actually considered an executable statement.

Move the UNREFERENCED_PARAMETER to after the DECLARE, and all will be well.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Oh damn… Sorry for not being so explicit since the beggining… :confused:

Now I also understand why was this working before and not anymore since using Visual Studio. It happens that with visual studio, the warnings treated as errors was even more strict so I had to put unreferenced parameters in almost every function.

Thanks everyone who gave an help here!

With my best regards,

Nuno Santos