1: #include "stdafx.h"
2: #include <metahost.h>
3: #include <stdio.h>
4: #include <Windows.h>
5: #include <stdlib.h>
6: #include <conio.h>
7: #include <iostream>
8: #include <ObjBase.h>
9: #include <strsafe.h>
10:
11: using namespace std;
12:
13: // Structs
14: typedef struct _ExecuteArgs {
15: LPCWSTR pwzAssemblyPath;
16: LPCWSTR pwzTypeName;
17: LPCWSTR pwzMethodName;
18: LPCWSTR pwzArgument;
19: DWORD pReturnValue;
20: } ExecuteArgs, *PExecuteArgs;
21:
22: // Function prototypes
23: void LoadAssembly(wchar_t* searchString);
24: void ReadData(LPVOID lpViewAddress, int cbBytesWritten);
25: void ExecuteMethod(ICLRRuntimeHost* clrHost, PExecuteArgs args);
26: BOOL CreateFileMapped(HANDLE& hFileMap, LPVOID& lpViewAddress, wchar_t* fileName);
27:
28: int _tmain(int argc, _TCHAR* argv[])
29: {
30: wchar_t buffer[30];
31: wcout << "Enter search string: ";
32: wcin.getline(buffer, 30);
33: LoadAssembly(buffer);
34: wcout << "\n\nPress any key to continue...";
35: _getch();
36:
37: return 0;
38: }
39:
40: // Method responsible for loading the .NET assembly
41: void LoadAssembly(wchar_t* searchString) {
42: GUID fileNameGuid;
43: ExecuteArgs args;
44: HANDLE hFileMap = 0;
45: LPVOID lpViewAddress = NULL;
46: const int fileSize = 0x00100000;
47: LPOLESTR szGUID = new WCHAR [39];
48: ICLRMetaHost *pMetaHost = NULL;
49: ICLRRuntimeInfo *pRuntimeInfo = NULL;
50: ICLRRuntimeHost *pRuntimeHost = NULL;
51: wchar_t concatStr[50], parsedStr[100], mappedFileName[60];
52: size_t concatStrCb = 0, parsedStrCb = 0, mappedFileStrCb = 0;
53:
54: CoCreateGuid(&fileNameGuid);
55: StringFromGUID2(fileNameGuid, szGUID, 39);
56: concatStrCb = sizeof(concatStr) / sizeof(wchar_t);
57: parsedStrCb = sizeof(parsedStr) / sizeof(wchar_t);
58: mappedFileStrCb = sizeof(mappedFileName) / sizeof(wchar_t);
59: StringCchPrintf(mappedFileName, mappedFileStrCb, L"Global\\LinqData%s", &szGUID[1]);
60: StringCchCopyN(parsedStr, parsedStrCb, mappedFileName, mappedFileStrCb - 37);
61: StringCchPrintf(concatStr, concatStrCb, L"%s;%s;%u", searchString, parsedStr, fileSize);
62:
63: args.pwzAssemblyPath=L"Managed.dll";
64: args.pwzTypeName=L"Managed.DataOperations";
65: args.pwzMethodName=L"SearchCustomer";
66: args.pwzArgument = concatStr;
67: args.pReturnValue = 0;
68:
69: if (CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost) == S_OK) {
70: if (pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*)&pRuntimeInfo) == S_OK) {
71: if (pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&pRuntimeHost) == S_OK) {
72: if (CreateFileMapped(hFileMap, lpViewAddress, parsedStr)) {
73: pRuntimeHost->Start();
74: ExecuteMethod(pRuntimeHost, &args);
75: // Found any data?
76: if (args.pReturnValue > 0) ReadData(lpViewAddress, args.pReturnValue);
77: pRuntimeHost->Stop();
78: UnmapViewOfFile(lpViewAddress);
79: }
80: CloseHandle(hFileMap);
81: }
82: }
83: }
84: delete[] szGUID;
85: }
86:
87: // Method responsible for creating a file mapped into memory
88: BOOL CreateFileMapped(HANDLE& hFileMap, LPVOID& lpViewAddress, wchar_t* fileName) {
89: BOOL retval = FALSE;
90:
91: hFileMap = CreateFileMapping(NULL, NULL, PAGE_EXECUTE_READWRITE | SEC_COMMIT, 0x00000000, 0x00100000, fileName);
92:
93: if (GetLastError() == NULL) {
94: lpViewAddress = MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, NULL, NULL, 0x00100000);
95: retval = GetLastError() == NULL;
96: }
97: return retval;
98: }
99:
100: // Method responsible for reading from the mapped file
101: void ReadData(LPVOID lpViewAddress, int cbBytesWritten) {
102: wchar_t* buffer = new wchar_t[cbBytesWritten + 1];
103: char* pData = (char*) lpViewAddress;
104: buffer[cbBytesWritten] = '\0';
105:
106: for(int nByte = 0; nByte < cbBytesWritten; nByte++)
107: buffer[nByte] = pData[nByte];
108:
109: wcout << "\n\nThe value returned from .NET is \n\n" << buffer << endl;
110:
111: delete[] buffer;
112: }
113:
114: // Method responsible for executing method in hosted assembly
115: void ExecuteMethod(ICLRRuntimeHost* clrHost, PExecuteArgs args) {
116: HRESULT result= clrHost->ExecuteInDefaultAppDomain(args->pwzAssemblyPath, args->pwzTypeName,
117: args->pwzMethodName, args->pwzArgument, &args->pReturnValue);
118: }