Added an experiment.

This commit is contained in:
Pietro Gagliardi 2015-11-24 20:09:26 -05:00
parent 980b93381b
commit d2449b6c55
6 changed files with 196 additions and 0 deletions

3
wpf/build.bat Executable file
View File

@ -0,0 +1,3 @@
cl /clr /AI "c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" wpf.cpp /link /dll /out:wpf.dll ole32.lib
cl /Tc program.c /link /out:program.exe wpf.lib

22
wpf/program.c Normal file
View File

@ -0,0 +1,22 @@
// 24 november 2015
#include <stdio.h>
#include "wpf.h"
static void onClosing(wpfWindow *w, void *data)
{
printf("exiting\n");
wpfQuit();
}
int main(void)
{
wpfWindow *w;
wpfInit();
w = wpfNewWindow("Hi", 320, 240);
wpfWindowOnClosing(w, onClosing, NULL);
wpfRun();
printf("out\n");
return 0;
}

88
wpf/wpf.cpp Normal file
View File

@ -0,0 +1,88 @@
// 24 november 2015
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <vcclr.h>
#define EXPORT __declspec(dllexport)
#include "wpf.h"
#using <System.dll>
#using <WindowsBase.dll>
#using <PresentationCore.dll>
#using <PresentationFramework.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows;
ref class windowEvents {
public:
wpfWindow *w;
// TODO the using namespace above doesn't work?
void closingEvent(Object ^sender, CancelEventArgs ^e);
};
struct wpfWindow {
gcroot<Window ^> window;
gcroot<windowEvents ^> events;
void (*onClosing)(wpfWindow *, void *);
void *onClosingData;
};
void windowEvents::closingEvent(Object ^sender, CancelEventArgs ^e)
{
(*(this->w->onClosing))(this->w, this->w->onClosingData);
}
wpfWindow *wpfNewWindow(const char *title, int width, int height)
{
wpfWindow *w;
w = new wpfWindow;
w->window = gcnew Window();
w->window->Title = gcnew String(title);
w->window->Width = width;
w->window->Height = height;
w->events = gcnew windowEvents();
w->events->w = w;
w->window->Closing += gcnew CancelEventHandler(w->events, &windowEvents::closingEvent);
w->window->Show();
return w;
}
void wpfWindowOnClosing(wpfWindow *w, void (*f)(wpfWindow *w, void *data), void *data)
{
w->onClosing = f;
w->onClosingData = data;
}
static gcroot<Application ^> app;
void wpfInit(void)
{
HRESULT hr;
// see http://stackoverflow.com/questions/24348205/how-do-i-solve-this-com-issue-in-c
// we MUST be running STA
// .net initializes as MTA for some stupid reason
// TODO https://msdn.microsoft.com/en-us/library/5s8ee185%28v=vs.71%29.aspx use CoInitializeEx()?
hr = CoInitialize(NULL);
if (hr != S_OK && hr != S_FALSE)
DebugBreak();
app = gcnew Application();
}
void wpfRun(void)
{
app->Run();
CoUninitialize();
}
void wpfQuit(void)
{
app->Shutdown();
}

22
wpf/wpf.h Normal file
View File

@ -0,0 +1,22 @@
// 24 november 2015
#ifndef EXPORT
#define EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct wpfWindow wpfWindow;
extern EXPORT wpfWindow *wpfNewWindow(const char *title, int width, int height);
extern EXPORT void wpfWindowOnClosing(wpfWindow *w, void (*f)(wpfWindow *w, void *data), void *data);
extern EXPORT void wpfInit(void);
extern EXPORT void wpfRun(void);
extern EXPORT void wpfQuit(void);
#ifdef __cplusplus
}
#endif

44
wpf/wpf.vcxproj Normal file
View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 24 november 2015 -->
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- TODO target name -->
<!-- TODO make sure all cl flags we care about are present -->
<PropertyGroup Label="Configuration">
<TargetFrameworkVersion>v3.0</TargetFrameworkVersion>
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CLRSupport>true</CLRSupport>
<CharacterSet>Unicode</CharacterSet>
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<!-- TODO make this unnecessary -->
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ItemDefinitionGroup>
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<!-- TODO no precompiled headers -->
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies />
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<ClInclude Include="wpf.h" />
<ClCompile Include="wpf.cpp" />
</ItemGroup>
</Project>

17
wpf/xtest.cpp Normal file
View File

@ -0,0 +1,17 @@
// 24 november 2015
#using <System.dll>
#using <WindowsBase.dll>
#using <PresentationCore.dll>
#using <PresentationFramework.dll>
using namespace System;
using namespace System::ComponentModel;
int main(void)
{
System::ComponentModel::CancelEventArgs ^e;
e = gcnew CancelEventArgs();
e->Cancel = true;
System::Console::WriteLine(e->Cancel);
return 0;
}