cudaGraphicsD3D11RegisterResource returning cudaErrorInvalidDevice

I have some code in native c++ that works fine. I create the ID3DDevice for the apapter corresponding to my GPU and am able to create a texture and map the resource to CUDA. When I try to call this code from .NET the call to cudaGraphicsD3D11RegisterResource fails with cudaErrorInvalidDevice. I tried adding some cudaMalloc calls and those still worked up until the aforementioned call to register the resource and then they too failed after that. Calling cudaSetDevice was successful for the correct GPU as found via cudaD3D11GetDevice. Any ideas on what could cause this? Below is the test function I have in a DLL that I call from .NET.

bool resource_test() {
HRESULT hr;

int * dummy;
err = cudaMalloc((void **)&dummy, 100);
err = cudaFree(dummy);

// Find the first CUDA capable device
cudaError_t status;
int tmp;
IDXGIFactory1 * factory;
IDXGIAdapter * adapter;
CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void **)&factory);
for (unsigned int g_iAdapter = 0; factory->EnumAdapters(g_iAdapter, &adapter) == S_OK; g_iAdapter++)
{
	DXGI_ADAPTER_DESC desc;
	adapter->GetDesc(&desc);

	status = cudaD3D11GetDevice(&tmp, adapter);
	if (status == cudaSuccess)
	{
		int pbTCC;
		throwOnCUDAError( cudaDeviceGetAttribute(&pbTCC, cudaDevAttrTccDriver, tmp) );
		LOG_INFO("  -> GPU %d: driver mode is: %s\n", tmp, pbTCC ? "TCC" : "WDDM");
		if (pbTCC == 1) {
			LOG_DEBUG("Can't use GPU %d, using TCC driver mode", tmp);
			continue;
		}
		break;
	}
}

    err = cudaSetDevice(tmp);

err = cudaMalloc((void **)&dummy, 100);
if (err == cudaSuccess) {
	err = cudaFree(dummy);
}

int creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;

#if defined(DEBUG) || defined(_DEBUG)
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

ID3D11Device * device;
ID3D11DeviceContext * context;
hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, creationFlags, NULL, 0,                  D3D11_SDK_VERSION, &device, NULL, &context);
if (FAILED(hr)) {
	LOG_ERROR("Unable to create DX11 device, hr=0x%x", hr);
	LogHRESULT(hr);
	return false;
}

err = cudaMalloc((void **)&dummy, 100);
if (err == cudaSuccess) {
	err = cudaFree(dummy);
}


ID3D11Texture2D * texture;

D3D11_TEXTURE2D_DESC desc = {
    (int)1024, (int)1024, 1, 1, DXGI_FORMAT_B8G8R8A8_UNORM, { 1, 0 },
    D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET, 0, 0
};
HRESULT hResult = device->CreateTexture2D(&desc, NULL, &texture);
if (FAILED(hResult)) {
	return false;
}

err = cudaMalloc((void **)&dummy, 100);
if (err == cudaSuccess) {
	err = cudaFree(dummy);
}

cudaGraphicsResource_t cuResource;

    /* FAILS HERE returning cudaErrorInvalidDevice */
err = cudaGraphicsD3D11RegisterResource(&cuResource, texture, cudaGraphicsRegisterFlagsNone);

if (err != cudaSuccess) {
	return false;
}

return true;

}