See the question and my original answer on StackOverflow

If you debug your program, you should see this in the output:

DXGI ERROR: CreateDXGIFactory cannot be called from DllMain. [ MISCELLANEOUS ERROR #76: ]

This is expected as explained here: DXGI responses from DLLMain (D3D11CreateDeviceAndSwapChain will implicitly create a DXGI factory)

If your app's DllMain function creates a DXGI factory, DXGI returns an error code.

So, you must not call this from DllMain (this is anynay a very special place where some black magic voodoo happens.)