How to get an export driver to generate a .lib file using Visual Studio .vcxproj file

Hi,

I need to create a driver that would be loaded like a standard driver, but also exports some functions to other kernel-mode drivers. According to MSDN article http://msdn.microsoft.com/en-us/library/windows/hardware/ff542891(v=vs.85).aspx this is possible…

What I’m having trouble with is getting this to generate the .lib file. I’m using Visual Studio 2013 to build. I do see online articles detailing how to get this done using “sources” files (like specifying TARGETTYPE-EXPORT_DRIVER etc) , but don’t see any help with the .vcxproj files that my organization uses. Did several trial and errors by putting these “sources” file-type-macros into .vcxproj file, but nothing seems to work. The build only generates .sys file.

Any help with the .vcxproj settings to get it to generate a .lib would be great!

Regards
Madhavi

1 Like

Iirc there is an export driver toolset

d

Bent from my phone


From: xxxxx@gmail.commailto:xxxxx
Sent: ?10/?3/?2014 3:55 PM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: [ntdev] How to get an export driver to generate a .lib file using Visual Studio .vcxproj file

Hi,

I need to create a driver that would be loaded like a standard driver, but also exports some functions to other kernel-mode drivers. According to MSDN article http://msdn.microsoft.com/en-us/library/windows/hardware/ff542891(v=vs.85).aspx this is possible…

What I’m having trouble with is getting this to generate the .lib file. I’m using Visual Studio 2013 to build. I do see online articles detailing how to get this done using “sources” files (like specifying TARGETTYPE-EXPORT_DRIVER etc) , but don’t see any help with the .vcxproj files that my organization uses. Did several trial and errors by putting these “sources” file-type-macros into .vcxproj file, but nothing seems to work. The build only generates .sys file.

Any help with the .vcxproj settings to get it to generate a .lib would be great!

Regards
Madhavi


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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</mailto:xxxxx></mailto:xxxxx>

One should be able to use the Empty Dll for Drivers project except of
course that doesn’t work. It builds a user mode component. The classpnp
sample builds a kernel dll, you could clone that and then throw out
everything you don’t need.

Mark Roddy

On Fri, Oct 3, 2014 at 6:55 PM, wrote:

> Hi,
>
> I need to create a driver that would be loaded like a standard driver, but
> also exports some functions to other kernel-mode drivers. According to MSDN
> article
> http://msdn.microsoft.com/en-us/library/windows/hardware/ff542891(v=vs.85).aspx
> this is possible…
>
> What I’m having trouble with is getting this to generate the .lib file.
> I’m using Visual Studio 2013 to build. I do see online articles detailing
> how to get this done using “sources” files (like specifying
> TARGETTYPE-EXPORT_DRIVER etc) , but don’t see any help with the .vcxproj
> files that my organization uses. Did several trial and errors by putting
> these “sources” file-type-macros into .vcxproj file, but nothing seems to
> work. The build only generates .sys file.
>
> Any help with the .vcxproj settings to get it to generate a .lib would be
> great!
>
> Regards
> Madhavi
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> 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
>

Thanks a bunch, I did see the ClassPnP Storage sample http://code.msdn.microsoft.com/windowshardware/ClassPnP-Storage-Class-541a107d . So now I’m seeing out that if DriverType is ExportDriver, then it cannot be a KMDF driver…

So to frame the question, can a KMDF driver that is loaded like a “normal” driver, also export some functions?

Regards
Madhavi

I think yes as long as DriverEntry is the first entry point called, not DllMain.

d

Bent from my phone


From: xxxxx@gmail.commailto:xxxxx
Sent: ?10/?6/?2014 4:18 PM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: RE:[ntdev] How to get an export driver to generate a .lib file using Visual Studio .vcxproj file

Thanks a bunch, I did see the ClassPnP Storage sample http://code.msdn.microsoft.com/windowshardware/ClassPnP-Storage-Class-541a107d . So now I’m seeing out that if DriverType is ExportDriver, then it cannot be a KMDF driver…

So to frame the question, can a KMDF driver that is loaded like a “normal” driver, also export some functions?

Regards
Madhavi


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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</mailto:xxxxx></mailto:xxxxx>

I take that back. It won?t work if the last outstanding reference is an import resolution. Upon the last pnp reference, the driver will be unloaded by WDF and leave the import references in place and will cause a bugcheck into executing unmapped code.

d

From: Doron Holan
Sent: Monday, October 6, 2014 4:46 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] How to get an export driver to generate a .lib file using Visual Studio .vcxproj file

I think yes as long as DriverEntry is the first entry point called, not DllMain.

d

Bent from my phone


From: xxxxx@gmail.commailto:xxxxx
Sent: ?10/?6/?2014 4:18 PM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: RE:[ntdev] How to get an export driver to generate a .lib file using Visual Studio .vcxproj file
Thanks a bunch, I did see the ClassPnP Storage sample http://code.msdn.microsoft.com/windowshardware/ClassPnP-Storage-Class-541a107d . So now I’m seeing out that if DriverType is ExportDriver, then it cannot be a KMDF driver…

So to frame the question, can a KMDF driver that is loaded like a “normal” driver, also export some functions?

Regards
Madhavi


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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</mailto:xxxxx></mailto:xxxxx>

You have to build two binaries: the driver’s binary and the export driver’s binary. Your driver and others must rely on this export driver.

If you don’t want to build a second binary, your driver must handle the IRP_MN_QUERY_INTERFACE IRP.

xxxxx@hotmail.com wrote:

You have to build two binaries: the driver’s binary and the export driver’s binary. Your driver and others must rely on this export driver.

If you don’t want to build a second binary, your driver must handle the IRP_MN_QUERY_INTERFACE IRP.

I don’t understand either of the assertions in your reply. If I have a
driver that has services it can offer to other drivers, what’s the point
of creating a separate binary for the export driver? The services are
all in the original driver.

And the point of using an export driver is that I can call functions
directly, exactly like a user-mode DLL, without using the Query
Interface mechanism. Query Interface is an ALTERNATIVE to export
drivers (usually a better alternative), not a PART of them.


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

My apologies. Because the OP stated that the .lib file was not produced, I concluded that building a driver that is both a regular driver and an export driver isn’t supported or is a system reserved feature (acpi.sys does export functions) but it is supported.

You can use a regular driver as an export driver, but the driver must be built as an export driver. So you must create an export driver project as a starting point. I missed the following point in the MSDN documentation:


Standard drivers can also function as export drivers. For a driver to function in both ways, it must be built as an export driver and loaded as a regular driver.

http://msdn.microsoft.com/en-us/library/windows/hardware/ff542891(v=vs.85).aspx

How to create a .lib from a .DLL: See for example
http://adrianhenke.wordpress.com/2008/12/05/create-lib-file-from-dll/

  • pa

>What I’m having trouble with is getting this to generate the .lib file. I’m using Visual Studio 2013 to build.

A lib file should be created if the code exports functions and/or data. The use of __declspec(dllexport) is the recommended way for defining exports. I have made a test under VS2013 with a driver that exports a function with the __declspec(dllexport) statement and a .lib file is created.


When LINK creates a program that contains exports (usually a DLL), it also builds a .lib file, unless an .exp file was used in the build. You can control the import library file name with the /IMPLIB option.

http://msdn.microsoft.com/en-us/library/37b80k4a.aspx

> A lib file should be created if the code exports functions and/or data. The use of __declspec

(dllexport) is the recommended way for defining exports.

Unrecommended.

It cannot produce un-mangled export names, at least for __stdcall functions which are default for kmode.

So, it is used either by Visual Studio novices, or for projects portable to Linux (__declspec(dllexport) is supported on Linux unlike the .DEF file).


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

hi,

Thanks all.

I have a follow-up question on Doron’s comment about the workability of this scheme:

“I take that back. It won?t work if the last outstanding reference is an import
resolution. Upon the last pnp reference, the driver will be unloaded by WDF and
leave the import references in place and will cause a bugcheck into executing
unmapped code.”

Question:
How can I fix this problem? I mean, how can I prevent the driver being un-loaded if the import reference is in place?
How about dictating the users of the import library to call an Init() and increment a reference count, and use the reference count in the driver to prevent the driver from getting unloaded? Is there a way this can be done in KMDF?

Also, posting what worked for general benefit:
After adding the parameter into the .vcxproj file, the build is generating an import library. (I have a .def and a dummy exported function for now).

So the parameters I have are:


x.lib
x.def


And
Driver
KMDF

Please do add your thoughts about the un-loading problem with dangling references.

Regards
Madhavi

There is no way to tell the wdf loader about an external reference it doesn’t take itself

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Wednesday, October 8, 2014 12:26 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to get an export driver to generate a .lib file using Visual Studio .vcxproj file

hi,

Thanks all.

I have a follow-up question on Doron’s comment about the workability of this scheme:

“I take that back. It won?t work if the last outstanding reference is an import resolution. Upon the last pnp reference, the driver will be unloaded by WDF and leave the import references in place and will cause a bugcheck into executing unmapped code.”

Question:
How can I fix this problem? I mean, how can I prevent the driver being un-loaded if the import reference is in place?
How about dictating the users of the import library to call an Init() and increment a reference count, and use the reference count in the driver to prevent the driver from getting unloaded? Is there a way this can be done in KMDF?

Also, posting what worked for general benefit:
After adding the parameter into the .vcxproj file, the build is generating an import library. (I have a .def and a dummy exported function for now).

So the parameters I have are:


x.lib
x.def


And
Driver
KMDF

Please do add your thoughts about the un-loading problem with dangling references.

Regards
Madhavi


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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

thanks, Doron. Will it help if the users of the export KMDF driver register for PnP notification and stop using the exported functions when they get notified?

Regards
Madhavi

You are in the land of unsupported and untested. No matter what i say it is an educated guess where even if it appears to work, i would never make it a part of a shipping product

d

Bent from my phone


From: xxxxx@gmail.commailto:xxxxx
Sent: ?10/?8/?2014 4:38 PM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: RE:[ntdev] How to get an export driver to generate a .lib file using Visual Studio .vcxproj file

thanks, Doron. Will it help if the users of the export KMDF driver register for PnP notification and stop using the exported functions when they get notified?

Regards
Madhavi


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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</mailto:xxxxx></mailto:xxxxx>

Looks like for my use-case, having a separate export driver is the way to go, then.
Thank you very much for all the inputs, Doron and everyone!

Regards
Madhavi

I think the ImportLibrary property is of interest if you want the library name to be different than that of the generated binary. The lib file is produced if your binary exports at least one symbol and no .exp file is used.

Your driver must export DllInitialize and DllUnload entry points (defined as PRIVATE in the .DEF file so that they want be present in the lib file and therefore won’t be usable by the code but only by the loader). Look at the MSDN pages for the details.

Your driver can be both an export and a standard driver, this is a fully supported feature.