program Sample04_IStreamBasedFile;

{$APPTYPE CONSOLE}

uses
  SysUtils,Classes,Windows,ActiveX,Dialogs,untMyStreams,
  BoxedAppSDK_static in '..\..\!Common\BoxedAppSDK_static.pas';
var
  FileHandle: THandle;
  szText: string;
  temp: DWORD;
  IStreamImpl: IStream;



begin
  // Init BoxedApp SDK
	BoxedAppSDK_Init;

	// Allow injection BoxedApp engine into child processes
	BoxedAppSDK_EnableOption(DEF_BOXEDAPPSDK_OPTION__EMBED_BOXEDAPP_IN_CHILD_PROCESSES, TRUE);

  IStreamImpl:=TIMemoryStream.Create;
//  CreateStreamOnHGlobal(0, true, IStreamImpl);

  //Now we can create virtual file based on IStream
  FileHandle :=
		BoxedAppSDK_CreateVirtualFileBasedOnIStream(
      '1.txt',
			GENERIC_WRITE,
			FILE_SHARE_READ,
			nil,
			CREATE_NEW,
			0,
			0,
			IStreamImpl);
  szText := 'This is a virtual file. Cool! You have just loaded the virtual file into notepad.exe!'+sLineBreak+'Don''t forget to obtain a license ;)'+sLineBreak+'http://boxedapp.com/order.html';
	WriteFile(FileHandle, szText[1], Length(szText),temp,nil);
  CloseHandle(FileHandle);


	// Now notepad loads the virtual file
	WinExec('notepad.exe 1.txt', SW_SHOW);

	WriteLn('Press Ctrl + C to finish');

	Sleep(INFINITE);
end.
