I have a need to generate 3MF files on my WiseJ app. The app is running on a VB Desktop project. Using Microsoft’s tutorial: https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/generate-3mf, I’ve successfully rewritten the code in vb, it complies, and runs until the final method. The initial issue I had was that some of the required references are in the Universal Windows Platform. I was able to solve that by creating an alternate AppDomain to run the method but the FileSavePicker used to generate the final .3MF file is still a problem. The code for the final method (below) fails at “Dim storageFile = Await savePicker.PickSaveFileAsync()”. The error (next below) shows “Invalid window handle”. The code shows my attempt to fix that with “IInitializeWithWindow”, but it still fails at the same point.
I’m wondering if there is a connection with the way WiseJ works with dialog boxes and also if there is a work around?
Private Async Function SaveTo3mf(localPackage As Printing3D3MFPackage) As Task(Of Boolean)
Dim savePicker As FileSavePicker = New FileSavePicker()
savePicker.DefaultFileExtension = “.3mf”
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary
savePicker.FileTypeChoices.Add(“3MF File”, {“.3mf”})
Call CType(CObj(savePicker), IInitializeWithWindow).Initialize(Process.GetCurrentProcess().MainWindowHandle)
Dim storageFile = Await savePicker.PickSaveFileAsync()
If storageFile Is Nothing Then
Return False
End If
Using stream = Await localPackage.SaveAsync()
stream.Seek(0)
Using dataReader = New DataReader(stream)
Await dataReader.LoadAsync(CUInt(stream.Size))
Dim buffer = dataReader.ReadBuffer(CUInt(stream.Size))
Await FileIO.WriteBufferAsync(storageFile, buffer)
End Using
End Using
Return True
End Function
ERROR MESSAGE
System.Exception
HResult=0x80070578
Message=Invalid window handle. (Exception from HRESULT: 0x80070578)
Source=mscorlib
StackTrace:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at HAADesignPortal.Data3MFFunctions.VB$StateMachine_8_SaveTo3mf.MoveNext() in C:\Users\lemay\Source\Repos\HAADesignPortal\Data3MFFunctions.vb:line 257
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at HAADesignPortal.Data3MFFunctions.VB$StateMachine_1_Build3MF.MoveNext() in C:\Users\lemay\Source\Repos\HAADesignPortal\Data3MFFunctions.vb:line 43
This exception was originally thrown at this call stack:
[External Code]
HAADesignPortal.Data3MFFunctions.SaveTo3mf(Windows.Graphics.Printing3D.Printing3D3MFPackage) in Data3MFFunctions.vb
[External Code]
HAADesignPortal.Data3MFFunctions.Build3MF() in Data3MFFunctions.vb
I thought I’d add my solution to my problem here although the point of the question becomes moot. As a relative nube on WiseJ, an alternative solution resulted from realizing that I’m using WebSocket. The technique to download I was working on relied on HttpRequest which is incompatible when using WebSocket. The solution was to skip providing a FileSavePicker and simplify the process using the Application Download(stream,”FileName.3mf”) method. The stream resulting from the 3D Printing UWP code running in the sub-domain returns a zip file. I simply downloaded it into the Downloads folder on my PC. It works great. I’m still not sure how to implement the FileSavePicker in a sub-domain. My attempt at using Invoke to call the FileSavePicker process also failed to generate a window handle, but it became unnecessary.
Thanks for the assistance.
Gerry
That is for a desktop app and uses the process MainWindowHandle.
Wisej doesn’t have a UI thread and doesn’t have a main window handle because it’s not a desktop app. You would be opening a file explorer on a web server!
If you are using the Wisej.Application exe for a standalone app you have to invoke the call on the main UI thread.
Thanks for your reply! I thought I added the handle using the technique from this article: https://stackoverflow.com/questions/57161258/invalid-window-handle-error-when-using-fileopenpicker-from-c-sharp-net-framwo. My vb code is below. I’ve been reading through the variety of related topics but this keeps coming up as the accepted approach. Is there another resource you can point me toward? Thanks again!
Code:
<ComImport>
<Guid(“3E68D4BD-7135-4D10-8018-9FB6D9F33FA1”)>
<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Interface IInitializeWithWindow
Sub Initialize(ByVal hwnd As IntPtr)
End Interface
Private Async Function SaveTo3mf(localPackage As Printing3D3MFPackage) As Task(Of Boolean)
Dim savePicker As FileSavePicker = New FileSavePicker()
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary
savePicker.DefaultFileExtension = “.3mf”
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary
savePicker.FileTypeChoices.Add(“3MF File”, {“.3mf”})
Call CType(CObj(savePicker), IInitializeWithWindow).Initialize(Process.GetCurrentProcess().MainWindowHandle)
Dim storageFile = Await savePicker.PickSaveFileAsync()
If storageFile Is Nothing Then
Return False
End If
Using stream = Await localPackage.SaveAsync()
stream.Seek(0)
Using dataReader = New DataReader(stream)
Await dataReader.LoadAsync(CUInt(stream.Size))
Dim buffer = dataReader.ReadBuffer(CUInt(stream.Size))
Await FileIO.WriteBufferAsync(storageFile, buffer)
End Using
End Using
Return True
End Function
Here is the test 3MFTest project. I had also built a non-WiseJ Forms App and the code works there.
The discussion where is shows the AppDomain switching approach is here: https://stackoverflow.com/questions/40613882/uwp-api-in-asp-net
The discussion where it shows the possible solution to the Handle problem is here: https://stackoverflow.com/questions/57161258/invalid-window-handle-error-when-using-fileopenpicker-from-c-sharp-net-framwo
Thanks very much!
Gerry
Hi Gerald,
Can you please wrap up a small sample that reproduces the issue ?
Best,
Alaa
Please login first to submit.