<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>BoxedApp Blog</title>
	<atom:link href="http://boxedapp.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://boxedapp.com/blog</link>
	<description>BoxedApp SDK: Tips'n'Tricks, Examples, Use Cases etc.</description>
	<pubDate>Sat, 26 Jul 2008 17:33:40 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>C++ / CLI - How To Use Managed C++ DLL when Microsoft Visual C++ Redistributable is not installed?</title>
		<link>http://boxedapp.com/blog/2008/07/16/c-cli-how-to-use-managed-c-dll-when-microsoft-visual-c-redistributable-is-not-installed/</link>
		<comments>http://boxedapp.com/blog/2008/07/16/c-cli-how-to-use-managed-c-dll-when-microsoft-visual-c-redistributable-is-not-installed/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 14:34:49 +0000</pubDate>
		<dc:creator>Artem A. Razin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C++]]></category>

		<category><![CDATA[examples]]></category>

		<category><![CDATA[use cases]]></category>

		<guid isPermaLink="false">http://boxedapp.com/blog/?p=7</guid>
		<description><![CDATA[If your .NET application uses components written in Managed C++, you face the necessity to distribute Microsoft Visual C++ Redistributable with it. If one attempts to launch such application in a system that doesn’t have the corresponding Microsoft Visual C++ Redistributable installed, the user will get a warning &#8220;This application has failed to start because [...]]]></description>
			<content:encoded><![CDATA[<p>If your .NET application uses components written in Managed C++, you face the necessity to distribute Microsoft Visual C++ Redistributable with it. If one attempts to launch such application in a system that doesn’t have the corresponding Microsoft Visual C++ Redistributable installed, the user will get a warning &#8220;This application has failed to start because the application configuration is incorrect&#8221;. <span id="more-7"></span> Why this happens, and can that be done without installing Microsoft Visual C++ Redistributable?</p>
<p>With the common approach, a DLL written in Managed C++ has a dependency from Microsoft Visual C++ Runtime, DLL files from which cannot be linked statically. The traditional solution is the inclusion of Microsoft Visual C++ Redistributable in the setup file.</p>
<p>With BoxedApp SDK, you can emulate the availability of Microsoft Visual C++ Runtime. Right after the application is launched, before using the components compiled in Managed C++, create the files, from which the Managed C++ DLL depends (<a href = "#how_to_find_dependency">how to find paths to dependencies</a>):</p>
<pre name="code" class="c-sharp" cols="35" rows="10">
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;

namespace WindowsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            BoxedAppSDK.NativeMethods.BoxedAppSDK_Init();

            string PathOfWinSxS = 
                Directory.GetParent(Environment.SystemDirectory).FullName + 
                @"\WinSxS";

            if (!Directory.Exists(PathOfWinSxS))
                // Win2k
                PathOfWinSxS = Application.StartupPath;

            Stream fromStream =
                Assembly.
                GetExecutingAssembly().
                GetManifestResourceStream("WindowsApplication1.res.msvcm80d.dll");

            CreateDLLInMemory(
                PathOfWinSxS + 
                @"\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcm80d.dll",
                fromStream);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            BoxedAppSDK.NativeMethods.BoxedAppSDK_Exit();
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool CloseHandle(IntPtr hObject);

        static void CreateDLLInMemory(string strVirtualPath, Stream stream)
        {
            const int BufferSize = 1024;
            byte[] buffer = new byte[BufferSize];

            IntPtr hHandle =
                BoxedAppSDK.NativeMethods.BoxedAppSDK_CreateVirtualFile(
                strVirtualPath,
                BoxedAppSDK.NativeMethods.EFileAccess.GenericWrite,
                BoxedAppSDK.NativeMethods.EFileShare.Read,
                IntPtr.Zero,
                BoxedAppSDK.NativeMethods.ECreationDisposition.New,
                BoxedAppSDK.NativeMethods.EFileAttributes.Normal,
                IntPtr.Zero);
            CloseHandle(hHandle);

            int nReadBytes;

            using (FileStream VirtualFileStream = new FileStream(strVirtualPath, FileMode.Open))
            {
                while ((nReadBytes = stream.Read(buffer, 0, BufferSize)) > 0)
                    VirtualFileStream.Write(buffer, 0, nReadBytes);
            }
        }
    }
}
</pre>
<p><a name = "how_to_find_dependency"></a>To find out, which exact DLL files does your Managed C++ DLL has dependencies from, use the depends.exe application from Visual Studio setup. Open your Managed C++ DLL in depends.exe, and you will easily find, which VC++ Runtime components it depends from:</p>
<p><a href='http://boxedapp.com/blog/wp-content/uploads/2008/07/depends.png' target = "_blank"><img src="http://boxedapp.com/blog/wp-content/uploads/2008/07/depends.png" alt="" title="depends" width="500" height="400" class="aligncenter size-full wp-image-8" /></a>
<p class="addtoany_share_save">
    <a name="a2a_dd" onmouseover="a2a_show_dropdown(this)" onmouseout="a2a_onMouseOut_delay()" href="http://www.addtoany.com/bookmark?sitename=BoxedApp%20Blog&amp;siteurl=http%3A%2F%2Fboxedapp.com%2Fblog%2F&amp;linkname=C%2B%2B%20%2F%20CLI%20-%20How%20To%20Use%20Managed%20C%2B%2B%20DLL%20when%20Microsoft%20Visual%20C%2B%2B%20Redistributable%20is%20not%20installed%3F&amp;linkurl=http%3A%2F%2Fboxedapp.com%2Fblog%2F2008%2F07%2F16%2Fc-cli-how-to-use-managed-c-dll-when-microsoft-visual-c-redistributable-is-not-installed%2F"><img src="http://boxedapp.com/blog/wp-content/plugins/add-to-any/share_save_256_24.gif" width="256" height="24" border="0" alt="Share/Save/Bookmark"/></a><br />
    <script type="text/javascript">
		a2a_linkname="C++ / CLI - How To Use Managed C++ DLL when Microsoft Visual C++ Redistributable is not installed?";
		a2a_linkurl="http://boxedapp.com/blog/2008/07/16/c-cli-how-to-use-managed-c-dll-when-microsoft-visual-c-redistributable-is-not-installed/";
				a2a_show_title=1;    </script><br />
    <script type="text/javascript" src="http://www.addtoany.com/menu/page.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://boxedapp.com/blog/2008/07/16/c-cli-how-to-use-managed-c-dll-when-microsoft-visual-c-redistributable-is-not-installed/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Use ActiveX components without registration</title>
		<link>http://boxedapp.com/blog/2008/06/29/use-activex-components-without-registration/</link>
		<comments>http://boxedapp.com/blog/2008/06/29/use-activex-components-without-registration/#comments</comments>
		<pubDate>Sun, 29 Jun 2008 16:55:55 +0000</pubDate>
		<dc:creator>Artem A. Razin</dc:creator>
		
		<category><![CDATA[activex]]></category>

		<guid isPermaLink="false">http://boxedapp.com/blog/?p=6</guid>
		<description><![CDATA[Many applications use ActiveX and COM components. But all ActiveXs should be registered in the system registry to be used properly. This is a problem:

registration in the registry requires admin rights
sometimes registration influences to other applications
for example, your application uses Flash 7, but another application requires Flash 9
your application should register an ActiveX at startup [...]]]></description>
			<content:encoded><![CDATA[<p>Many applications use ActiveX and COM components. But all ActiveXs should be registered in the system registry to be used properly. This is a problem:</p>
<ul>
<li>registration in the registry requires admin rights</li>
<li>sometimes registration influences to other applications<br />
<i>for example, your application uses Flash 7, but another application requires Flash 9</i></p>
<li>your application should register an ActiveX at startup and unregister it when ActiveX is not needed anymore<br />
<i>for example, your application is so-called <a href = "http://en.wikipedia.org/wiki/Portable_application" target = "_blank">portable application</a></i>
</li>
</ul>
<p><span id="more-6"></span>How to solve the task using BoxedApp?</p>
<p>BoxedApp provides a very interesting function <a href = "http://www.boxedapp.com/boxedappsdk/help/index/functions/boxedappsdk_registercomlibraryinvirtualregistry.html" target = "_blank">BoxedAppSDK_RegisterCOMLibraryInVirtualRegistry</a>. This function does the same things as <a href = "http://en.wikipedia.org/wiki/Regsvr32" target = "_blank">regsvr32</a> does.  BoxedAppSDK_RegisterCOMLibraryInVirtualRegistry loads a file (virtual or real) and calls <a href = "http://msdn.microsoft.com/en-us/library/ms682162(VS.85).aspx" target = "_blank">DllRegisterServer</a>. What&#8217;s important: <b>all changes of the registry save in virtual registry, not real!</b></p>
<p>So, when an application creates an ActiveX, COM subsystem finds all what it needs in virtual registry.</p>
<p>As a result, the system registry remains unchanged, and COM subsystem &#8220;thinks&#8221; that the ActiveX is really registered in the system.
<p class="addtoany_share_save">
    <a name="a2a_dd" onmouseover="a2a_show_dropdown(this)" onmouseout="a2a_onMouseOut_delay()" href="http://www.addtoany.com/bookmark?sitename=BoxedApp%20Blog&amp;siteurl=http%3A%2F%2Fboxedapp.com%2Fblog%2F&amp;linkname=Use%20ActiveX%20components%20without%20registration&amp;linkurl=http%3A%2F%2Fboxedapp.com%2Fblog%2F2008%2F06%2F29%2Fuse-activex-components-without-registration%2F"><img src="http://boxedapp.com/blog/wp-content/plugins/add-to-any/share_save_256_24.gif" width="256" height="24" border="0" alt="Share/Save/Bookmark"/></a><br />
    <script type="text/javascript">
		a2a_linkname="Use ActiveX components without registration";
		a2a_linkurl="http://boxedapp.com/blog/2008/06/29/use-activex-components-without-registration/";
				a2a_show_title=1;    </script><br />
    <script type="text/javascript" src="http://www.addtoany.com/menu/page.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://boxedapp.com/blog/2008/06/29/use-activex-components-without-registration/feed/</wfw:commentRss>
		</item>
		<item>
		<title>.NET Runtime Embedding</title>
		<link>http://boxedapp.com/blog/2008/06/11/net-runtime-embedding/</link>
		<comments>http://boxedapp.com/blog/2008/06/11/net-runtime-embedding/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 08:22:54 +0000</pubDate>
		<dc:creator>Artem A. Razin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[examples]]></category>

		<category><![CDATA[use cases]]></category>

		<guid isPermaLink="false">http://boxedapp.com/blog/?p=5</guid>
		<description><![CDATA[If you are into developing .NET applications, you are likely to know that it requires .NET Runtime to have the success running them. If the runtime suite is not installed, whenever the program attempts to launch, it shows an ugly messagebox notifying you that mscoree.dll could not be found.
So, we&#8217;ve got a .net application WindowsApplication1.exe, [...]]]></description>
			<content:encoded><![CDATA[<p>If you are into developing .NET applications, you are likely to know that it requires .NET Runtime to have the success running them. <span id="more-5"></span>If the runtime suite is not installed, whenever the program attempts to launch, it shows an ugly messagebox notifying you that mscoree.dll could not be found.</p>
<p>So, we&#8217;ve got a .net application WindowsApplication1.exe, which displays a simple form with a progress bar and a couple of links. Now we are to create the environment where WindowsApplication1.exe would successfully launch on systems that don&#8217;t have .net runtime.</p>
<p>BoxedApp SDK creates the infrastructure of a virtual file system and a virtual registry; so once the corresponding virtual kes and virtual files are created, the .NET application can be launched successfully.</p>
<p>It is known that the hive HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework contains the key InstallRoot that points to the folder with subfolders with different versions of.net runtime. For instance, on my computer the key contains the value  содержит значение &#8220;C:\WINDOWS\Microsoft.NET\Framework&#8221;. That folder includes the following runtime versions:</p>
<p>v1.0.3705<br />
v1.1.4322<br />
v2.0.50727<br />
v3.0<br />
v3.5</p>
<p>In this example, we assume that our runtime version is 2.0.50727. For the folder for storing our runtime files, let&#8217;s choose some path; e.g., C:\DotNet. Next, let&#8217;s create the virtual files within that folder, and the application will see it as if .net runtime was actually installed on the computer.</p>
<p>So, let&#8217;s create our virtual key:</p>
<pre name="code" class="cpp" cols="35" rows="10">
// Initialize BoxedAppSDK
BoxedAppSDK_Init();

tstring strDotNetRoot = _T("C:\\DotNet");

DWORD dwDisposition;
HKEY hKey__DotNet;
LONG lRes = 
BoxedAppSDK_CreateVirtualRegKey(
	HKEY_LOCAL_MACHINE, 
	_T("SOFTWARE\\Microsoft\\.NETFramework"), 
	0, 
	NULL, 
	REG_OPTION_NON_VOLATILE, 
	KEY_ALL_ACCESS, 
	NULL, 
	&#038;hKey__DotNet, 
	&#038;dwDisposition);

LPCTSTR szValue = _T("C:\\DotNet\\");
RegSetValueEx(hKey__DotNet, _T("InstallRoot"), 0, REG_SZ, (CONST BYTE*)szValue, (lstrlen(szValue) + 1) * sizeof(TCHAR));
</pre>
<p>We will need paths to the system folders:</p>
<pre name="code" class="cpp" cols="35" rows="10">
TCHAR szWinDir[MAX_PATH] = { 0 };
GetWindowsDirectory(szWinDir, MAX_PATH);
tstring strWinDir = szWinDir;

TCHAR szSystemDir[MAX_PATH] = { 0 };
GetSystemDirectory(szSystemDir, MAX_PATH);
tstring strSystemDir = szSystemDir;
</pre>
<p>In the resource file, specify which files are to be embedded in the application. We are going to embed both .net runtime and the WindowsApplication1.exe files, plus the AppLauncher.dll assembly, which we&#8217;re going to cover a bit further:</p>
<pre name="code" class="cpp" cols="35" rows="10">
WindowsApplication1.exe BIN "WindowsApplication1.exe"
AppLauncher.dll BIN "AppLauncher.dll"

mscoree.dll BIN "system32\\mscoree.dll"
mscorjit.dll BIN "v2.0.50727\\mscorjit.dll"
mscorwks.dll BIN "v2.0.50727\\mscorwks.dll"
comctl32.dll BIN "WinSxS\\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2982_x-ww_ac3f9c03\\comctl32.dll"
msvcr80.dll BIN "WinSxS\\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.1433_x-ww_5cf844d2\\msvcr80.dll"
msvcr80d.dll BIN "WinSxS\\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\\msvcr80d.dll"
System.Windows.Forms.dll BIN "assembly\\GAC_MSIL\\System.Windows.Forms\\2.0.0.0__b77a5c561934e089\\System.Windows.Forms.dll"
mscorlib.dll BIN "assembly\\GAC_32\\mscorlib\\2.0.0.0__b77a5c561934e089\\mscorlib.dll"
mscorrc.dll BIN "v2.0.50727\\mscorrc.dll"
sorttbls.nlp BIN "assembly\\GAC_32\\mscorlib\\2.0.0.0__b77a5c561934e089\\sorttbls.nlp"
sortkey.nlp BIN "assembly\\GAC_32\\mscorlib\\2.0.0.0__b77a5c561934e089\\sortkey.nlp"
System.dll BIN "v2.0.50727\\System.dll"
System.Data.dll BIN "v2.0.50727\\System.Data.dll"
System.Drawing.dll BIN "v2.0.50727\\System.Drawing.dll"
Culture.dll BIN "v2.0.50727\\Culture.dll"
Accessibility.dll BIN "v2.0.50727\\Accessibility.dll"
GdiPlus.dll BIN "system32\\GdiPlus.dll"
</pre>
<p>In the properties of the rc file (right-click on the rc file and then select Properties, Configuration Properties -> Resources -> General, Additional Include Directories), specify the additional folders to be searched for the files:</p>
<p><code><br />
"$(FrameworkDir)";"$(SystemRoot)";"..\WindowsApplication1\bin\x86\$(ConfigurationName)";"..\AppLauncher\bin\x86\$(ConfigurationName)"<br />
</code></p>
<p>Here&#8217;s a light helper function for handling resources:</p>
<pre name="code" class="cpp" cols="35" rows="10">
void LoadResourceHelper( /* in */ LPCTSTR lpszName, 
                         /* in */ LPCTSTR lpszType, 
                         /* out */ LPVOID&#038; lpData, 
                         /* out */ DWORD&#038; dwSize)
{
    HMODULE hModule = GetModuleHandle(NULL);
    HRSRC hResInfo = FindResource(hModule, lpszName, lpszType);
    HGLOBAL hResData = LoadResource(hModule, hResInfo);
    lpData = LockResource(hResData);
    dwSize = SizeofResource(hModule, hResInfo);
}
</pre>
<p>And here&#8217;s the function for creating a virtual file; the content will be taken from the specified resource:</p>
<pre name="code" class="cpp" cols="35" rows="10">
void CreateVirtualFileFromResource(LPCTSTR szVirtualPath, LPCTSTR szResName, LPCTSTR szResType)
{
	DWORD temp;

	LPVOID pData;
	DWORD dwSize;

	LoadResourceHelper(szResName, szResType, pData, dwSize);

	HANDLE hFile = BoxedAppSDK_CreateVirtualFile(szVirtualPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, 0, NULL);
	WriteFile(hFile, pData, dwSize, &#038;temp, NULL);
	CloseHandle(hFile);
}
</pre>
<p>Now we can create the virtual files, required for the successful performance of the .net runtime:</p>
<pre name="code" class="cpp" cols="35" rows="10">
struct SFileResInfo
{
	tstring strVirtualPath;
	LPCTSTR szResName;
};

const SFileResInfo ResInfo[] = 
{
		{ strSystemDir + _T("\\mscoree.dll"), _T("mscoree.dll") }, 

		{ strDotNetRoot + _T("\\v2.0.50727\\mscorjit.dll"), _T("mscorjit.dll") }, 

		{ strDotNetRoot + _T("\\v2.0.50727\\mscorwks.dll"), _T("mscorwks.dll") }, 

		{ strWinDir + _T("\\WinSxS\\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2982_x-ww_ac3f9c03\\comctl32.dll"), _T("comctl32.dll") }, 
		{ strSystemDir + _T("\\comctl32.dll"), _T("comctl32.dll") }, 

		{ strWinDir + _T("\\WinSxS\\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.1433_x-ww_5cf844d2\\msvcr80.dll"), _T("msvcr80.dll") }, 
		{ strSystemDir + _T("\\msvcr80.dll"), _T("msvcr80.dll") }, 

		{ strWinDir + _T("\\assembly\\GAC_MSIL\\System.Windows.Forms\\2.0.0.0__b77a5c561934e089\\System.Windows.Forms.dll"), _T("System.Windows.Forms.dll") }, 

		{ strWinDir + _T("\\assembly\\GAC_32\\mscorlib\\2.0.0.0__b77a5c561934e089\\mscorlib.dll"), _T("mscorlib.dll") }, 

		{ strDotNetRoot + _T("\\v2.0.50727\\mscorrc.dll"), _T("mscorrc.dll") }, 

		{ strWinDir + _T("\\assembly\\GAC_32\\mscorlib\\2.0.0.0__b77a5c561934e089\\sorttbls.nlp"), _T("sorttbls.nlp") }, 
		{ strWinDir + _T("\\assembly\\GAC_32\\mscorlib\\2.0.0.0__b77a5c561934e089\\sortkey.nlp"), _T("sortkey.nlp") }, 

		{ strDotNetRoot + _T("\\v2.0.50727\\System.dll"), _T("System.dll") }, 

		{ strDotNetRoot + _T("\\v2.0.50727\\System.Data.dll"), _T("System.Data.dll") }, 
		{ strDotNetRoot + _T("\\v2.0.50727\\System.Drawing.dll"), _T("System.Drawing.dll") }, 

		{ strDotNetRoot + _T("\\v2.0.50727\\Culture.dll"), _T("Culture.dll") }, 

		{ strWinDir + _T("\\assembly\\GAC_MSIL\\System\\2.0.0.0__b77a5c561934e089\\System.dll"), _T("System.dll") }, 
		{ strWinDir + _T("\\assembly\\GAC_MSIL\\System.Drawing\\2.0.0.0__b03f5f7f11d50a3a\\System.Drawing.dll"), _T("System.Drawing.dll") }, 

		{ strWinDir + _T("\\Microsoft.NET\\Framework\\v2.0.50727\\Accessibility.dll"), _T("Accessibility.dll") }, 
		{ strWinDir + _T("\\assembly\\GAC_MSIL\\Accessibility\\2.0.0.0__b03f5f7f11d50a3a\\Accessibility.dll"), _T("Accessibility.dll") }, 

		{ strSystemDir + _T("\\GdiPlus.dll"), _T("GdiPlus.dll") }, 


		{ _T("C:\\AppLauncher.dll"), _T("AppLauncher.dll") }, 
		{ _T("C:\\WindowsApplication1.exe"), _T("WindowsApplication1.exe") }
};

for (int i = 0; i < sizeof(ResInfo) / sizeof(ResInfo[0]); i++)
	CreateVirtualFileFromResource(ResInfo[i].strVirtualPath.c_str(), ResInfo[i].szResName, _T("BIN"));
</pre>
<p>To get along with .net runtime, we&#8217;re going to use the mscoree.dll library (please note that mscoree.dll is embedded in the application). mscoree.dll exports the function CorBindToRuntimeEx, which will give us the interface ICLRRuntimeHost. The method ICLRRuntimeHost::ExecuteInDefaultAppDomain loads the assembly and calls the specified method. However, that method is to have a very specific signature; you cannot call just any method, but we&#8217;re going to cover this later. So, here&#8217;s our ICLRRuntimeHost:</p>
<pre name="code" class="cpp" cols="35" rows="10">
HMODULE hMSCoree = LoadLibrary((strSystemDir + _T("\\mscoree.dll")).c_str());

typedef HRESULT (__stdcall *P_CorBindToRuntimeEx)(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor, DWORD startupFlags, REFCLSID rclsid, REFIID riid, LPVOID FAR *ppv);
P_CorBindToRuntimeEx pCorBindToRuntimeEx = (P_CorBindToRuntimeEx)GetProcAddress(hMCoree, "CorBindToRuntimeEx");

ICLRRuntimeHost* pCLRRuntimeHost = NULL;
pCorBindToRuntimeEx(L"v2.0.50727", NULL, 0, CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (void**)&#038;pCLRRuntimeHost);
</pre>
<p>ICLRRuntimeHost::ExecuteInDefaultAppDomain can be called only by a method with the following signature:</p>
<pre name="code" class="c-sharp" cols="35" rows="10">
int SomeMethod (string Argument);
</pre>
<p>Now, our job is to load the assembly of the .net application and call its point of entry. However, the point of entry has a different signature:</p>
<pre name="code" class="c-sharp" cols="35" rows="10">
static void Main();
</pre>
<p>Therefore, we&#8217;re going to create another assembly, which will be a class with a static method, which in its turn will load the assembly of the main application and call its point of entry:</p>
<pre name="code" class="c-sharp" cols="35" rows="10">
using System;
using System.Reflection;
using System.Text;

namespace AppLauncher
{
    public class Launcher
    {
        public static int Launch(string strPath)
        {
            Assembly assembly = Assembly.LoadFile(strPath);
            assembly.EntryPoint.Invoke(null, null);

            return 0;
        }
    }
}
</pre>
<p>Now we&#8217;ve got everything ready for running the application:</p>
<pre name="code" class="cpp" cols="35" rows="10">
pCLRRuntimeHost->Start();

DWORD nRetValue;
pCLRRuntimeHost->ExecuteInDefaultAppDomain(
	L"C:\\AppLauncher.dll", 
	L"AppLauncher.Launcher", 
	L"Launch", 
	L"C:\\WindowsApplication1.exe", 
	&#038;nRetValue);

pCLRRuntimeHost->Stop();

pCLRRuntimeHost->Release();

FreeLibrary(hMSCoree);
</pre>
<p>Related links:</p>
<ul>
<li><a href = "http://boxedapp.com/download/boxedappsdk__demo.zip" target = "_blank">Download example; see the folder samples\Misc\DotNetEmbedding</a></li>
<li><a href = "http://msdn.microsoft.com/en-us/library/99sz37yh(VS.71).aspx" target = "_blank">Description of CorBindToRuntimeEx</a></li>
<li><a href = "http://boxedapp.com/boxedappsdk/help/index/functions/boxedappsdk_createvirtualregkey.html" target = "_blank">BoxedAppSDK_CreateVirtualRegKey</a></li>
<li><a href = "http://boxedapp.com/boxedappsdk/help/index/functions/boxedappsdk_createvirtualfile.html" target = "_blank">BoxedAppSDK_CreateVirtualFile</a></li>
</ul>
<p class="addtoany_share_save">
    <a name="a2a_dd" onmouseover="a2a_show_dropdown(this)" onmouseout="a2a_onMouseOut_delay()" href="http://www.addtoany.com/bookmark?sitename=BoxedApp%20Blog&amp;siteurl=http%3A%2F%2Fboxedapp.com%2Fblog%2F&amp;linkname=.NET%20Runtime%20Embedding&amp;linkurl=http%3A%2F%2Fboxedapp.com%2Fblog%2F2008%2F06%2F11%2Fnet-runtime-embedding%2F"><img src="http://boxedapp.com/blog/wp-content/plugins/add-to-any/share_save_256_24.gif" width="256" height="24" border="0" alt="Share/Save/Bookmark"/></a><br />
    <script type="text/javascript">
		a2a_linkname=".NET Runtime Embedding";
		a2a_linkurl="http://boxedapp.com/blog/2008/06/11/net-runtime-embedding/";
				a2a_show_title=1;    </script><br />
    <script type="text/javascript" src="http://www.addtoany.com/menu/page.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://boxedapp.com/blog/2008/06/11/net-runtime-embedding/feed/</wfw:commentRss>
		</item>
		<item>
		<title>.NET Applications: How to Hide Assemblies And Unmanaged DLLs</title>
		<link>http://boxedapp.com/blog/2008/06/11/net-applications-how-to-hide-assembly-and-unmanaged-dlls/</link>
		<comments>http://boxedapp.com/blog/2008/06/11/net-applications-how-to-hide-assembly-and-unmanaged-dlls/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 08:21:32 +0000</pubDate>
		<dc:creator>Artem A. Razin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[examples]]></category>

		<category><![CDATA[use cases]]></category>

		<guid isPermaLink="false">http://boxedapp.com/blog/?p=4</guid>
		<description><![CDATA[One interesting task I&#8217;ve heard from one of our first customers is hidding assemblies and unmanaged DLLs. The target language is C#. Let&#8217;s explore how to solve this task with BoxedApp SDK.
At the first, decide how to store an assembly. In this example, I keep an assembly in static array. To create C# code with [...]]]></description>
			<content:encoded><![CDATA[<p>One interesting task I&#8217;ve heard from one of our first customers is hidding assemblies and unmanaged DLLs. The target language is C#. <span id="more-4"></span>Let&#8217;s explore how to solve this task with BoxedApp SDK.</p>
<p>At the first, decide how to store an assembly. In this example, I keep an assembly in static array. To create C# code with such array, I use the following code:</p>
<pre name="code" class="c-sharp" cols="35" rows="10">
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConvertBinToSharpCode
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] data = File.ReadAllBytes(args[0]);

            Console.Write("namespace BinData\n{\n\tpublic class Data1\n\t\t{\n\n\t\tpublic static byte[] data = \n{\n");

            foreach (byte b in data)
            {
                Console.Write(b.ToString() + ",");
            }

            Console.Write("};\n}\n}\n");
        }
    }
}
</pre>
<p>It takes one argument (path to an assembly) and outputs C# code, save it as data.cs, for example:</p>
<pre name="code" class="c-sharp" cols="35" rows="10">
namespace BinData
{
	public class Data1
		{

		public static byte[] data = 
{
77,90,144,0,3,0 .......
}
}
</pre>
<p>Include it to the main solution.</p>
<p>Now, how to create a virtual file with assembly and fill it from the BinData.Data1.data:</p>
<pre name="code" class="c-sharp" cols="35" rows="10">
static void CreateAssemblyInMemory(
   string strVirtualPath, 
   byte[] data)
{
   IntPtr hHandle =
      BoxedAppSDK.NativeMethods.BoxedAppSDK_CreateVirtualFile(
         strVirtualPath,
         BoxedAppSDK.NativeMethods.EFileAccess.GenericWrite,
         BoxedAppSDK.NativeMethods.EFileShare.Read,
         IntPtr.Zero,
         BoxedAppSDK.NativeMethods.ECreationDisposition.New,
         BoxedAppSDK.NativeMethods.EFileAttributes.Normal,
         IntPtr.Zero);
   CloseHandle(hHandle);

   using (FileStream VirtualFileStream = 
            new FileStream(strVirtualPath, FileMode.Open))
   {
      VirtualFileStream.Write(data, 0, (int)data.Length);
   }
}
</pre>
<p>Now how to create an assembly into memory:</p>
<pre name="code" class="c-sharp" cols="35" rows="10">
CreateAssemblyInMemory(@"Z:\myclass.dll", BinData.Data1.data);

Assembly assembly = Assembly.LoadFrom(@"Z:\myclass.dll");
object o = assembly.CreateInstance("myclass.Class1");
o.GetType().GetMethod("SomeMethod").Invoke(o, null);
</pre>
<p>Another option, when your application uses some assembly and you would like to keep this assembly in memory. In this case, you write:</p>
<pre name="code" class="c-sharp" cols="35" rows="10">
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        BoxedAppSDK.NativeMethods.BoxedAppSDK_Init();

        // Creates a virtual file with assembly
        // before using the assembly, in 
        // other words, the assembly is not 
        // loaded yet
        CreateAssemblyInMemory(
           Application.StartupPath + @"\\Assembly1.dll", 
           BinData.Data1.data);

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        // The Form1 will be created, 
        // Form1 loads the assembly Assembly1.dll, 
        // BoxedApp SDK provides it from memory
        Application.Run(new Form1());

        BoxedAppSDK.NativeMethods.BoxedAppSDK_Exit();
   }
...
}
</pre>
<p class="addtoany_share_save">
    <a name="a2a_dd" onmouseover="a2a_show_dropdown(this)" onmouseout="a2a_onMouseOut_delay()" href="http://www.addtoany.com/bookmark?sitename=BoxedApp%20Blog&amp;siteurl=http%3A%2F%2Fboxedapp.com%2Fblog%2F&amp;linkname=.NET%20Applications%3A%20How%20to%20Hide%20Assemblies%20And%20Unmanaged%20DLLs&amp;linkurl=http%3A%2F%2Fboxedapp.com%2Fblog%2F2008%2F06%2F11%2Fnet-applications-how-to-hide-assembly-and-unmanaged-dlls%2F"><img src="http://boxedapp.com/blog/wp-content/plugins/add-to-any/share_save_256_24.gif" width="256" height="24" border="0" alt="Share/Save/Bookmark"/></a><br />
    <script type="text/javascript">
		a2a_linkname=".NET Applications: How to Hide Assemblies And Unmanaged DLLs";
		a2a_linkurl="http://boxedapp.com/blog/2008/06/11/net-applications-how-to-hide-assembly-and-unmanaged-dlls/";
				a2a_show_title=1;    </script><br />
    <script type="text/javascript" src="http://www.addtoany.com/menu/page.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://boxedapp.com/blog/2008/06/11/net-applications-how-to-hide-assembly-and-unmanaged-dlls/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to embed Flash Player ActiveX using BoxedApp SDK</title>
		<link>http://boxedapp.com/blog/2008/05/23/how-to-embed-flash-player-activex-using-boxedapp-sdk/</link>
		<comments>http://boxedapp.com/blog/2008/05/23/how-to-embed-flash-player-activex-using-boxedapp-sdk/#comments</comments>
		<pubDate>Fri, 23 May 2008 17:49:34 +0000</pubDate>
		<dc:creator>Artem A. Razin</dc:creator>
		
		<category><![CDATA[C++]]></category>

		<category><![CDATA[activex]]></category>

		<category><![CDATA[examples]]></category>

		<category><![CDATA[flash]]></category>

		<category><![CDATA[use cases]]></category>

		<guid isPermaLink="false">http://boxedapp.com/blog/?p=3</guid>
		<description><![CDATA[OK, let’s get started.
One of the most important features is embedding ActiveX into an application. BoxedAppSDK_RegisterCOMLibraryInVirtualRegistry registers a virtual file in a virtual registry. In detail, it loads a specified file (LoadLibrary) and calls DllRegisterServer (exported from the DLL). All changes made by DllRegisterServer are saved in the virtual registry; the system registry remains unchanged.
After [...]]]></description>
			<content:encoded><![CDATA[<p>OK, let’s get started.</p>
<p>One of the most important features is embedding ActiveX into an application. <a href="http://boxedapp.com/boxedappsdk/help/index/functions/boxedappsdk_registercomlibraryinvirtualregistry.html" target="_blank">BoxedAppSDK_RegisterCOMLibraryInVirtualRegistry</a> registers a virtual file in a virtual registry. In detail, it loads a specified file (LoadLibrary) and calls DllRegisterServer (exported from the DLL). All changes made by DllRegisterServer are saved in the virtual registry; <span id="more-3"></span>the system registry remains unchanged.</p>
<p>After that, CoCreateInstance (and the related functions) will receive the correct information from the “registry”.</p>
<p>Embedding Flash step-by-step:</p>
<p>1. Get started with BoxedApp SDK:</p>
<pre name="code" class="cpp" cols="35" rows="10">
BoxedAppSDK_Init();
</pre>
<p>2. Create a virtual file with Flash.ocx:</p>
<pre name="code" class="cpp" cols="35" rows="10">
LPVOID pBuffer;
DWORD dwSize;

// LoadResourceHelper gets resource pointer and size
LoadResourceHelper(
   MAKEINTRESOURCE(IDR_BIN_FLASH_OCX), 
   _T("BIN"), 
   pBuffer, 
   dwSize);

HANDLE hVirtualFile1 = 
   BoxedAppSDK_CreateVirtualFile(
      _T("C:\\Flash9e.ocx"), 
      GENERIC_READ, 
      FILE_SHARE_READ, 
      NULL, 
      CREATE_NEW, 
      0, 
      NULL);  

DWORD dwTemp; 
WriteFile(hVirtualFile1, pBuffer, dwSize, &amp;dwTemp, NULL);

CloseHandle(hVirtualFile1);
</pre>
<p>3. Register the COM library in the virtual registry:</p>
<pre name="code" class="cpp" cols="35" rows="10">
BoxedAppSDK_RegisterCOMLibraryInVirtualRegistry(_T("C:\\Flash9e.ocx"));
</pre>
<p>Further on, when you have created a Flash ActiveX instance, the COM subsystem (CoCreateInstance) will read information from the virtual registry and create an ActiveX object from the virtual DLL.</p>
<p>In particular, using this method you can create applications that are ready to work without installation (so-called &#8220;portable applications&#8221;) even if they use ActiveX or others COM-objects. Simply create a virtual file with an appropriate DLL and call <a href="http://boxedapp.com/boxedappsdk/help/index/functions/boxedappsdk_registercomlibraryinvirtualregistry.html" target="_blank">BoxedAppSDK_RegisterCOMLibraryInVirtualRegistry</a>.</p>
<hr />
<p>Related links:</p>
<ol>
<li><a href = "http://boxedapp.com/download/boxedappsdk__demo.zip" target = "_blank">Download BoxedApp SDK samples</a></li>
<li><a href = "http://f-in-box.com/" target = "_blank">The best solution to work with Flash ActiveX (embedding is also supported)</a></li>
<li><a href="http://boxedapp.com/boxedappsdk/help/index/functions/boxedappsdk_registercomlibraryinvirtualregistry.html" target="_blank">Function BoxedAppSDK_RegisterCOMLibraryInVirtualRegistry</a></li>
</ol>
<p class="addtoany_share_save">
    <a name="a2a_dd" onmouseover="a2a_show_dropdown(this)" onmouseout="a2a_onMouseOut_delay()" href="http://www.addtoany.com/bookmark?sitename=BoxedApp%20Blog&amp;siteurl=http%3A%2F%2Fboxedapp.com%2Fblog%2F&amp;linkname=How%20to%20embed%20Flash%20Player%20ActiveX%20using%20BoxedApp%20SDK&amp;linkurl=http%3A%2F%2Fboxedapp.com%2Fblog%2F2008%2F05%2F23%2Fhow-to-embed-flash-player-activex-using-boxedapp-sdk%2F"><img src="http://boxedapp.com/blog/wp-content/plugins/add-to-any/share_save_256_24.gif" width="256" height="24" border="0" alt="Share/Save/Bookmark"/></a><br />
    <script type="text/javascript">
		a2a_linkname="How to embed Flash Player ActiveX using BoxedApp SDK";
		a2a_linkurl="http://boxedapp.com/blog/2008/05/23/how-to-embed-flash-player-activex-using-boxedapp-sdk/";
				a2a_show_title=1;    </script><br />
    <script type="text/javascript" src="http://www.addtoany.com/menu/page.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://boxedapp.com/blog/2008/05/23/how-to-embed-flash-player-activex-using-boxedapp-sdk/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
