Imported midi playing methords in DLL fails to run

HEY GUYS!!!
Is there anyone who wants to import midi playing methords just like me ?

I am college student who intends to run .mid file in my project, and I followed the instructions for linking dll in the epic wiki. There wasn’t any problems in this part.

Compile and ue4 runs, all goes smoothly.

BUT WHEN I TRIGGER MY MIDI PLAYING METHORD IN UE4, vs2013 shows me an exception like this image shows:

71650-捕获.png

AND BY DEBUGGING I found that the problem may be caused by the failiure to create Component Object Model.
BTW, I use directx to play the .mid file.

Here is the some code I think relevant:

This is the code in my created class in ue4:

The methord below is the imported one and it is called by the methord above.

int MidiPlayerRun()
	{
		CoInitialize(NULL);
		IDirectMusicLoader8*		g_pLoader = NULL;
		IDirectMusicPerformance8*	g_pPerformance = NULL;
		IDirectMusicSegment8*		g_pSegment = NULL;
		int cvt;
		HRESULT error;
		WCHAR searchPath[MAX_PATH];
		char spath[MAX_PATH];
		char fname[MAX_PATH];
		WCHAR filename[MAX_PATH];

		CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
			IID_IDirectMusicLoader8, (void **)&g_pLoader);
		CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
			IID_IDirectMusicPerformance8, (void **)&g_pPerformance);

		//cin.getline(spath, MAX_PATH);
		spath[0] = 'd'; spath[1] = ':'; spath[2] = '\\';
		cvt = mbstowcs(searchPath, spath, MAX_PATH);
		g_pLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, searchPath, FALSE);

		//cin.getline(fname, MAX_PATH);
		fname[0] = '0'; fname[1] = '3'; fname[2] = '6'; fname[3] = '.'; fname[4] = 'm'; fname[5] = 'i'; fname[6] = 'd';

		cvt = mbstowcs(filename, fname, MAX_PATH);
		error = g_pLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8,
			filename, (void**)&g_pSegment);
		printf("%d\n", error);

		// init audio
		g_pPerformance->InitAudio(
			NULL,                  // IDirectMusic interface not needed.
			NULL,                  // IDirectSound interface not needed.
			NULL,                  // Window handle.
			DMUS_APATH_SHARED_STEREOPLUSREVERB,  // Default audiopath type.
			64,                    // Number of performance channels.
			DMUS_AUDIOF_ALL,       // Features on synthesizer.
			NULL                   // Audio parameters; use defaults.
			);
		// end init audio

		
		if (error)    // Pointer that receives interface.
		{
			switch (error)
			{
			case E_FAIL:
				printf("MIDI file could not be loaded: E_FAIL");
				return 000;
				break;
			case E_INVALIDARG:
				printf("MIDI file could not be loaded: E_INVALIDARG");
				return 999;
				break;
			case E_OUTOFMEMORY:
				printf("MIDI file could not be loaded: E_OUTOFMEMORY");
				return 888;
				break;
			case E_POINTER:
				printf("MIDI file could not be loaded: E_POINTER");
				return 777;
				break;
			case REGDB_E_CLASSNOTREG:
				printf("MIDI file could not be loaded: REGDB_E_CLASSNOTREG");
				return 666;
				break;
			case DMUS_E_LOADER_NOCLASSID:
				printf("MIDI file could not be loaded: DMUS_E_LOADER_NOCLASSID");
				return 789;
				break;
			case DMUS_E_LOADER_FAILEDOPEN:
				printf("MIDI file could not be loaded: DMUS_E_LOADER_FAILEDOPEN ");
				return 444;
				break;
			case DMUS_E_LOADER_FORMATNOTSUPPORTED:
				printf("MIDI file could not be loaded: DMUS_E_LOADER_FORMATNOTSUPPORTED");
				return 333;
				break;
			case DMUS_E_LOADER_FAILEDCREATE:
				printf("MIDI file could not be loaded: DMUS_E_LOADER_FAILEDCREATE");
				return 222;
				break;
			default:
				printf("MIDI file could not be loaded: Unknown Error");
				return 111;
				break;
			}

			//printf("error");
			//system("pause");
			//return -1;
			//return 555;
		}

		g_pSegment->Download(g_pPerformance);

		g_pPerformance->PlaySegmentEx(
			g_pSegment,  // Segment to play.
			NULL,        // Used for songs; not implemented.
			NULL,        // For transitions. 
			0,           // Flags.
			0,           // Start time; 0 is immediate.
			NULL,        // Pointer that receives segment state.
			NULL,        // Object to stop.
			NULL         // Audiopath, if not default.
			);

		if (g_pPerformance->IsPlaying(g_pSegment, NULL) == S_OK)
		{
			printf("is playing\n");
		}
		else
		{
			printf("is not playing\n");
		}
		//printf("is playing\n");
		system("pause");
		//return 0;
		return 321;
	}

The exception is throwed at the calling to ‘g_pPerformance->InitAudio’ :

71654-call.png

So I think the point may be the failiure of thisCoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,IID_IDirectMusicPerformance8, (void **)&g_pPerformance);

BTW , I have tried calling 'CoInitialize(NULL); ’ in ue4 separately, and I the code for playing midi file functions well when it runs itself in win32 console.

SEEKING HELP EAGERLY!!!

Hi.

If you know where error in DirectX App,

please use in MidiPlayer.dll

Error Handling Macros:

#include <DXErr.h> // DxErr.lib				old #include <dxerr9.h>

DXTRACE_ERR

DXTRACE_ERR_MSGBOX

Error Handling Functions:

DXGetErrorString

DXGetErrorDescription

DXTrace

When you got code-error from handle.

Get more information from: $DXSDK_DIR\Utilities\bin\DXErr.exe

Example for you:

if ( FAILED(error = g_pPerformance->InitAudio(...)) )

{

	// === use one, your choice.

	// DXTRACE_ERR( L"g_pPerformance FAIL!", error );

	// DXTRACE_ERR_MSGBOX( L"g_pPerformance FAIL!", error );

	// DXTRACE_ERR_MSGBOX( DXGetErrorString(error), error );

	DXTRACE_ERR( DXGetErrorDescription(error), error );

           return -1;

}

Hope this helps!