Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Before Posting...
Please check out the Community Guidelines in the Announcements and Administration Category.

__stdio_common_vswprintf is required for RtlUnicodeStringVPrintf.

Michael_RolleMichael_Rolle Member - All Emails Posts: 135

I just put in a call to RtlUnicodeStringVPrintf in my driver, and the linker is looking for __stdio_common_vswprintf.
This function is part of the Universal C Runtime library (ucrt.lib).
Do I have to link to the ucrt.lib, or is there some library in the WDK that contains this function? I'm afraid I'll break something if I link in the ucrt.lib.

I did link to ucrt.lib and the link of my driver was successful. The linker appears to have only loaded that one function from the library. But it still makes me nervous.

I added ntstrsafe.lib to my link, according to the MS documentation, but that library wasn't used!

And why oh why has Microsoft created this problem in the first place? Or is there some #define I need to make so that ntstrsafe.h will generate something that will link?

Comments

  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 7,138

    I did link to ucrt.lib and the link of my driver was successful.

    I didn't think that was allowed in a driver.

    I added ntstrsafe.lib to my link, according to the MS documentation, but that library wasn't used!

    Hmmmm...

    And why oh why has Microsoft created this problem in the first place?

    What kind of a question is that, really?

    Or is there some #define I need to make so that ntstrsafe.h will generate something that will link?

    You've looked through ntstrsafe.h and checked for this, I assume? Found the definition for the function in question? Please do some independent investigation and let us know what you find.

    Peter

    Peter Viscarola
    OSR
    @OSRDrivers

  • Michael_RolleMichael_Rolle Member - All Emails Posts: 135

    Linking to ucrt.lib doesn't actually work. The driver links OK, but it won't start. The sc start mydriver command gives me

    [SC] StartService FAILED 2:
    The system cannot find the file specified.

  • Michael_RolleMichael_Rolle Member - All Emails Posts: 135

    So that leaves me with the original question: How do you use RtlUnicodeStringVPrintf in a driver? Or is there some other way to create a string like \Device\devMyDriver\_num_ from an integer value for num?
    Surely you guys do this sort of thing all the time, so please someone tell me how you do it.

    Here is the relevant portion of ntstrsafe.h (in 10.0.17763.0):

    #ifdef _M_CEE_PURE
    #define NTSTRSAFEDDI      __inline NTSTATUS __clrcall
    #else
    **#define NTSTRSAFEDDI      __inline NTSTATUS __stdcall**
    #endif
    
    #if defined(NTSTRSAFE_LIB_IMPL) || defined(NTSTRSAFE_LIB)
    #define NTSTRSAFEWORKERDDI    EXTERN_C NTSTATUS __stdcall
    #else
    **#define NTSTRSAFEWORKERDDI    static NTSTRSAFEDDI**
    #endif
    
    // The following steps are *REQUIRED* if ntstrsafe.h is used for drivers on:
    //     Windows 2000
    //     Windows Millennium Edition
    //     Windows 98 Second Edition
    //     Windows 98
    //
    // 1. #define NTSTRSAFE_LIB before including the ntstrsafe.h header file.
    // 2. Add ntstrsafe.lib to the TARGET_LIBS line in SOURCES
    //
    // Drivers running on XP and later can skip these steps to create a smaller
    // driver by running the functions inline.
    

    The above comment is unclear to me. Do 1. and 2. apply to XP and later?

    #if defined(NTSTRSAFE_LIB)
    #pragma comment(lib, "ntstrsafe.lib")
    #endif
    
    NTSTRSAFEDDI
       RtlUnicodeStringVPrintf(
            _Inout_ PUNICODE_STRING DestinationString,
           _In_ _Printf_format_string_ NTSTRSAFE_PCWSTR pszFormat,
            _In_ va_list argList)
     {
        ...
            size_t cchNewDestLength = 0;
            status = RtlWideCharArrayVPrintfWorker(pszDest,
                    cchDest,
                    &cchNewDestLength,
                    pszFormat,
                    argList);
        return status;
    }
    ...
    NTSTRSAFEWORKERDDI
        RtlWideCharArrayVPrintfWorker(
                _Out_writes_to_(cchDest, *pcchNewDestLength) wchar_t* pszDest,
                _In_ size_t cchDest,
                _Out_ size_t* pcchNewDestLength,
                _In_ _Printf_format_string_ NTSTRSAFE_PCWSTR pszFormat,
                _In_ va_list argList)
    {
        NTSTATUS status = STATUS_SUCCESS;
        int iRet;
    
     #pragma warning(push)
     #pragma warning(disable: __WARNING_BANNED_API_USAGE)// "STRSAFE not included"
        iRet = _vsnwprintf(pszDest, cchDest, pszFormat, argList);
     #pragma warning(pop)
        ...
        return status;
    }
    

    I linked to ucrt.lib because that's the only place I could find _vsnwprintf.

    The Microsoft documentation at https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/ntstrsafe/nf-ntstrsafe-rtlunicodestringvprintf says you need ntstrsafe.lib. It also links to https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/using-safe-string-functions, which includes a broken link for "Safe string functions for UNICODE_STRING structures". There's no mention there about defining NTSTRSAFE_LIB.
    So at least the documentation is inadequate, which is why I asked why Microsoft created this problem.

  • Michael_RolleMichael_Rolle Member - All Emails Posts: 135

    Problem solved
    Well, I followed the steps in ntstrsafe.h (which are not mentioned in Microsoft docs as I have noted), and the driver links and starts.
    To summarize, I added ntstrsafe.lib to the Linker Inputs tab for the project, and I have

    #define NTSTRSAFE_LIB
    #include <ntstrsafe.h>
    

    in the driver code.

    I searched online for NTSTRSAFE_LIB and found several places where the above steps were cited, including 3 posts on NTDEV. But nothing from Microsoft. So my complaint about Microsoft stands.

  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 7,138

    So my complaint about Microsoft stands.

    I hope you file a bug on th doc page... or fix the doc page if it’s one that’s editable.

    Peter

    Peter Viscarola
    OSR
    @OSRDrivers

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Upcoming OSR Seminars
Writing WDF Drivers 25 Feb 2019 OSR Seminar Space
Developing Minifilters 8 April 2019 OSR Seminar Space