Skip to content

Commit f19a232

Browse files
Merge pull request #286 from microsoft/user/xianz/updateSample
User/xianz/update sample
2 parents d0d7761 + 33517a6 commit f19a232

File tree

6 files changed

+115
-20
lines changed

6 files changed

+115
-20
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "pch.h"
2+
#include "Filehelper.h"
3+
#include <libloaderapi.h>
4+
#include <stdlib.h>
5+
6+
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
7+
8+
namespace FileHelper
9+
{
10+
std::string GetModulePath()
11+
{
12+
std::string val;
13+
char modulePath[MAX_PATH] = {};
14+
GetModuleFileNameA(NULL, modulePath, ARRAYSIZE(modulePath));
15+
char drive[_MAX_DRIVE];
16+
char dir[_MAX_DIR];
17+
char filename[_MAX_FNAME];
18+
char ext[_MAX_EXT];
19+
_splitpath_s(modulePath, drive, _MAX_DRIVE, dir, _MAX_DIR, filename, _MAX_FNAME, ext, _MAX_EXT);
20+
21+
val = drive;
22+
val += dir;
23+
return val;
24+
}
25+
} // namespace FileHelper
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
#include <string>
3+
#include <Windows.h>
4+
namespace FileHelper
5+
{
6+
std::string GetModulePath();
7+
}

Samples/SqueezeNetObjectDetection/Desktop/cpp/SqueezeNetObjectDetectionCPP.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@
123123
</ItemDefinitionGroup>
124124
<ItemGroup>
125125
<ClInclude Include="..\..\..\..\SharedContent\models\SqueezeNet.h" />
126+
<ClInclude Include="Filehelper.h" />
126127
<ClInclude Include="pch.h" />
127128
</ItemGroup>
128129
<ItemGroup>
130+
<ClCompile Include="dllload.cpp" />
131+
<ClCompile Include="Filehelper.cpp" />
129132
<ClCompile Include="main.cpp" />
130133
<ClCompile Include="pch.cpp">
131134
<PrecompiledHeader>Create</PrecompiledHeader>

Samples/SqueezeNetObjectDetection/Desktop/cpp/SqueezeNetObjectDetectionCPP.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
<ItemGroup>
44
<ClCompile Include="main.cpp" />
55
<ClCompile Include="pch.cpp" />
6+
<ClCompile Include="dllload.cpp" />
7+
<ClCompile Include="Filehelper.cpp" />
68
</ItemGroup>
79
<ItemGroup>
810
<ClInclude Include="pch.h" />
911
<ClInclude Include="..\..\..\..\SharedContent\models\SqueezeNet.h" />
12+
<ClInclude Include="Filehelper.h" />
1013
</ItemGroup>
1114
<ItemGroup>
1215
<None Include="packages.config" />
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "pch.h"
2+
#include "FileHelper.h"
3+
#include <winrt/Windows.Foundation.h>
4+
#include <winstring.h>
5+
6+
extern "C"
7+
{
8+
HRESULT __stdcall OS_RoGetActivationFactory(HSTRING classId, GUID const& iid, void** factory) noexcept;
9+
}
10+
11+
#ifdef _M_IX86
12+
#pragma comment(linker, "/alternatename:_OS_RoGetActivationFactory@12=_RoGetActivationFactory@12")
13+
#else
14+
#pragma comment(linker, "/alternatename:OS_RoGetActivationFactory=RoGetActivationFactory")
15+
#endif
16+
17+
bool starts_with(std::wstring_view value, std::wstring_view match) noexcept
18+
{
19+
return 0 == value.compare(0, match.size(), match);
20+
}
21+
22+
int32_t __stdcall WINRT_RoGetActivationFactory(void* classId, winrt::guid const& iid, void** factory) noexcept
23+
{
24+
*factory = nullptr;
25+
std::wstring_view name{ WindowsGetStringRawBuffer(static_cast<HSTRING>(classId), nullptr),
26+
WindowsGetStringLen(static_cast<HSTRING>(classId)) };
27+
HMODULE library{ nullptr };
28+
29+
std::string modulePath = FileHelper::GetModulePath();
30+
std::wstring winmlDllPath = std::wstring(modulePath.begin(), modulePath.end()) + L"Windows.AI.MachineLearning.dll";
31+
32+
if (starts_with(name, L"Windows.AI.MachineLearning."))
33+
{
34+
const wchar_t* libPath = winmlDllPath.c_str();
35+
library = LoadLibraryW(libPath);
36+
}
37+
else
38+
{
39+
return OS_RoGetActivationFactory(static_cast<HSTRING>(classId), iid, factory);
40+
}
41+
42+
// If the library is not found, get the default one
43+
if (!library)
44+
{
45+
return OS_RoGetActivationFactory(static_cast<HSTRING>(classId), iid, factory);
46+
}
47+
48+
using DllGetActivationFactory = HRESULT __stdcall(HSTRING classId, void** factory);
49+
auto call = reinterpret_cast<DllGetActivationFactory*>(GetProcAddress(library, "DllGetActivationFactory"));
50+
51+
if (!call)
52+
{
53+
HRESULT const hr = HRESULT_FROM_WIN32(GetLastError());
54+
WINRT_VERIFY(FreeLibrary(library));
55+
return hr;
56+
}
57+
58+
winrt::com_ptr<winrt::Windows::Foundation::IActivationFactory> activation_factory;
59+
HRESULT const hr = call(static_cast<HSTRING>(classId), activation_factory.put_void());
60+
61+
if (FAILED(hr))
62+
{
63+
WINRT_VERIFY(FreeLibrary(library));
64+
return hr;
65+
}
66+
67+
if (iid != winrt::guid_of<winrt::Windows::Foundation::IActivationFactory>())
68+
{
69+
return activation_factory->QueryInterface(iid, factory);
70+
}
71+
72+
*factory = activation_factory.detach();
73+
return S_OK;
74+
}

Samples/SqueezeNetObjectDetection/Desktop/cpp/main.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33

44
#include "pch.h"
5+
#include "FileHelper.h"
56

67
using namespace winrt;
78
using namespace Windows::Foundation;
@@ -19,8 +20,6 @@ LearningModelDeviceKind deviceKind = LearningModelDeviceKind::Default;
1920
string deviceName = "default";
2021
hstring imagePath;
2122

22-
// helper functions
23-
string GetModulePath();
2423
void LoadLabels();
2524
VideoFrame LoadImageFile(hstring filePath, ColorManagementMode colorManagementMode);
2625
void PrintResults(IVectorView<float> results);
@@ -30,7 +29,7 @@ ColorManagementMode GetColorManagementMode(const LearningModel& model);
3029
wstring GetModelPath()
3130
{
3231
wostringstream woss;
33-
woss << GetModulePath().c_str();
32+
woss << FileHelper::GetModulePath().c_str();
3433
woss << "SqueezeNet.onnx";
3534
return woss.str();
3635
}
@@ -118,26 +117,10 @@ bool ParseArgs(int argc, char* argv[])
118117
return true;
119118
}
120119

121-
string GetModulePath()
122-
{
123-
string val;
124-
char modulePath[MAX_PATH] = {};
125-
GetModuleFileNameA(NULL, modulePath, ARRAYSIZE(modulePath));
126-
char drive[_MAX_DRIVE];
127-
char dir[_MAX_DIR];
128-
char filename[_MAX_FNAME];
129-
char ext[_MAX_EXT];
130-
_splitpath_s(modulePath, drive, _MAX_DRIVE, dir, _MAX_DIR, filename, _MAX_FNAME, ext, _MAX_EXT);
131-
132-
val = drive;
133-
val += dir;
134-
return val;
135-
}
136-
137120
void LoadLabels()
138121
{
139122
// Parse labels from labels file. We know the file's entries are already sorted in order.
140-
std::string labelsFilePath = GetModulePath() + labelsFileName;
123+
std::string labelsFilePath = FileHelper::GetModulePath() + labelsFileName;
141124
ifstream labelFile(labelsFilePath, ifstream::in);
142125
if (labelFile.fail())
143126
{

0 commit comments

Comments
 (0)