Sytem.InvalidCastException on Windows Phone : Unity 3d - arrays

One of my code throws this exception:
An exception of type 'System.InvalidCastException' occurred in mscorlib.ni.dll but was not handled in user code
Additional information: At least one element in the source array could not be cast down to the destination array type.
I get this exception on visual studio 2013 when I run this project on Windows Phone. OS version is 8.1 developer preview.
the Code snippet is:
private var BootlePattern : int[];
var BootleHits : boolean[];
var maxBottle : int;
function Start() {
...
BootlePattern=new Array(maxBottle);
BootleHits=new Array(maxBottle);
...
}
Can anyone please suggest anything about how to solve this?

Explicitly initializing the arrays like:
BootlePattern = [0, 0, 0, 0, 0, 0];
instead of :
BootlePattern = new Array(maxBottle);
solved my problem. I guess Windows phone could not handle the datatype during the declaration without value. Though I don't know the real reason.

Related

Can type mismatch error from VBScript caused by assessing non-variant array returned by COM interface be resolved by changing languages?

I am using a windows application that provides a COM interface for extension scripts. The scant documentation provides a few examples in VBScript so I started implementing my automation in VBScript.
This would look something like:
Set App = GetObject(, "MYAPP.Application")
MyObj = App.Object
MyArr = MyObj.ArrData
IsArray(MyArr) ' True
TypeName(MyArr) ' Long()
Lbound(MyArr) ' 0
Ubound(MyArr) ' 10
MyArr(1) ' Type Mismatch Error
On closer inspection, the array object is of type Long() (Array of Longs). After researching the problem, it is my understanding that VBScript cannot access elements of an array by index unless the array is of type Variant; that this is a limitation of VBScript as a language. I am not able to implement changes as in this answer because I don't have source access to the application I am trying to extend.
Is the error I am seeing purely a limitation of the language? Will I be able to access the array variables returned by the COM interface by porting my code to another language like C#, for example?
I have confirmed that porting to C# solves the type mismatch error encountered when accessing non-variant array data returned by the COM interface without requiring any change to the COM server.
After including the appropriate references, the code becomes:
var app = (MYAPP.Application)Microsoft.VisualBasic.Interaction.GetObject(null, "MYAPP.Application");
var myObj = (MYAPP.Obj) app.GetObject();
var myArr = (int[]) myObj.GetArrData();
if (myArr.Length() > 0)
{
System.Console.WriteLine(myArr[0]); // prints value at index 0 without error
}
The limitation is therefore with VBScript; the type mismatch is due to the language being able to handle only variant type arrays.

What would cause a data grid error on an image column only when deployed on a Windows 10 machine?

I have a C# windows forms app with a data grid. One column of the grid is an image column which I put a bitmap in. It works fine on the Windows 10 machine I developed it on and when I deploy it on Windows 7 machines, but on all other Windows 10 machines I get an error. The error is "System.FormatException: Formatted value of the cell has a wrong type." I haven't been able to recreate the error on my development machine, so I'm not sure how to figure out what's wrong. I thought it might be occurring because some prerequisite was not included when I deploy it (it's a Click Once application), but I don't see anything missing. I thought maybe it couldn't find the links to the image, so I tried drawing them instead and still got the same error. I'm guessing it has to do with my bitmap column, but I don't know why it would work on my machine and Windows 7 machines and not other Windows 10 machines. Any ideas on what's happening and/or how to fix this?
Here is the designer for my grid:
Here is the DefaultCellStyle settings. I thought the NullValue might be the cause. I originally had System.Drawing.Bitmap in it. I tried using null, but still get an error.
Here's the code for how I populate that column.
Bitmap bmpCombineOnly = new Bitmap(1, 1);
Bitmap bmpYellow = new Bitmap(26, 26);
Graphics grYellow = Graphics.FromImage(bmpYellow);
grYellow.DrawEllipse(new Pen(Color.Yellow, 2), 0, 0, 25, 25);
grYellow.FillEllipse(new SolidBrush(Color.Yellow), 0, 0, 25, 25);
bmpCombineOnly = bmpYellow;
foreach (DataGridViewRow row in ItemsInLocationDataGridView.Rows)
{
row.Cells["Icon"].Value = bmpCombineOnly;
}

"PrintTicket provider failed to convert DEVMODE to PrintTicket"

few years ago I wrote a WPF (Visual Basic 2012 based) app which included "printing" some report using standard WPF printdialogs/paginators/features....
everyting worked fine since then (2011/2015), and I did not change a line of that working code...
Now I had to change something (not printing related) to that project and of course I checked the printing features as well...
now the "printing" code fails when using it... actually is not the "paginator" code, but accessing the PrintTicket/Que objects fails reporting
"PrintTicket provider failed to convert PrintTicket to DEVMODE. Win32 error: The parameter is incorrect. "
but it's weird as it does not always fail, and it fails differently with different printers...
using for instance "PDF Creator" virtual printer, it fails writing/settings some properties of my Que object and not others, while using a different printer fails on others...
for instance, PDF Creator virtual printer:
1) myQue.DefaultPrintTicket = myTicket = OK
2) myQue.CurrentJobSettings.Description = "some title" = OK with some printer, FAILS with a different printer
but 2) only fails the very first time it is set, and I can "resolve" "looping" a few times with a minor time sleep.... again, the second or third time it succeded...
so I wrote an ugly code like
Dim dlg As New PrintDialog
.....
myTicket = dlg.PrintTicket
myQue = dlg.PrintQueue
'-----
' to check in a loop if the interesting
' properties has been set
Dim ticketIsSet(1) As Boolean
For iLoop As Integer = 1 To 5
'it can fail with DEVMODE = NULL
Try
If Not ticketIsSet(0) Then
myQue.DefaultPrintTicket = myTicket
ticketIsSet(0) = True
End If
If Not ticketIsSet(1) Then
myQue.CurrentJobSettings.Description = Me.Title
ticketIsSet(1) = True
End If
Catch ex As Exception
Errors.Add(ex.Message)
System.Threading.Thread.CurrentThread.Sleep(1000)
End Try
If ticketIsSet(0) AndAlso ticketIsSet(1) Then Exit For
Next
If Errors.Count Then
' it's still ok if title can't be set... :(
If ticketIsSet(0) Then Errors.Clear()
End If
so far, "so good" as I get the job done... but only partially... as my Paginator is used to (succesfully) populate a System.Windows.Controls.DocumentViewer... this alternate documentviewer window can later actually print if requested...
and again, with different printers it succed or fail with "PrintTicket provider failed to convert PrintTicket to DEVMODE. Win32 error: The parameter is incorrect. " exception...
with PDF Creator virtual printer it succed, with a different physical printer it fails...
I tried the very same looping trick as above, like
Dim Errors As New Specialized.StringCollection
Dim bDone As Boolean = False
For iLoop As Integer = 1 To 5
Try
Dim writer As System.Windows.Xps.XpsDocumentWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(m_printQueue)
writer.Write(Me.docViewer.Document.DocumentPaginator, Me.m_printTicket)
writer = Nothing
bDone = True
Catch ex As Exception
Errors.Add(ex.Message)
System.Threading.Thread.CurrentThread.Sleep(1000)
End Try
If bDone Then Exit For
Next
If bDone Then Errors.Clear()
If Errors.Count <> 0 Then BasShowErrors(Errors, Me, False)
and here it fails (again, on some printers) on
writer.Write(Me.docViewer.Document.DocumentPaginator, Me.m_printTicket)
with "PrintTicket provider failed to convert PrintTicket to DEVMODE. Win32 error: The parameter is incorrect. " exception...
obviously that printer is ok as regards other programs like World, Excel and the like, and it's ok as well with all other (NOT WPF based) projects... and again this only occur "NOW", as it worked like a charme at the time of the initial development...
my current settings are:
Win7 Ultimate sp 1
SQL 2014 Dev Edition (not relevant)
Visual Studio 2012 Ultimate Version 11.0.61219.00 Update 5
Microsoft .NET Framework version 4.6.01055
I even tried "repairing" the NET Framework with NetFramwork Repair Tool (https://www.microsoft.com/en-us/download/details.aspx?id=30135) with no success...
any hint is very appreciated :)
TIA
asql

Cloning a PathGeometry in Silverlight / WPF

I have a simple handler that adds an ellipse to an empty Silverlight canvas
private void UCLoaded(object sender, RoutedEventArgs e)
{
var geometry = MakeElipse(20, 15, new Point(100, 100));
var ellipsePath = new Path
{
Data = geometry,
Fill = new SolidColorBrush(Colors.DarkGray),
StrokeThickness = 4,
Stroke = new SolidColorBrush(Colors.Gray)
};
LayoutRoot.Children.Add(ellipsePath);
//
var duplicateEllipsePath = new Path();
//duplicateEllipsePath.Data = ellipsePath.Data;
duplicateEllipsePath.Data = geometry;
duplicateEllipsePath.Fill = ellipsePath.Fill;
duplicateEllipsePath.StrokeThickness = ellipsePath.StrokeThickness;
duplicateEllipsePath.Stroke = ellipsePath.Stroke;
LayoutRoot.Children.Add(duplicateEllipsePath);
}
The first ellipse, ellipsePath, is fine and renders as expected. But the line duplicateEllipsePath.Data = ellipsePath.Data or the alternative duplicateEllipsePath.Data = geometry each throw the System.ArgumentException "Value does not fall within the expected range". How can it be in range once, and out-of-range immediately afterwards? What is the correct way of duplicating a path in code like this?
It looks like the only way to clone a path is to do so manually. To quote this answer from Yi-Lun Luo:
The Data property is actually a Geometry. While not noticeable in Silverlight, A Geometry actually relies on an underlying system resource (because it needs to draw something). If you need to draw another Geometry, you'll need another system resource. So you must clone it before you assign it to a new Path. In WPF, we do have a Clone method on Geometry, unfortunately this is not supported in Silverlight. So you have to manually do the clone.
Another post above Yi-Lun's claims to contain reflective code to clone a geometry, and the same code seems to appear here, although the latter is more clearly formatted. However, in your case, it seems overkill to use a method such as this. The geometry you use is created by your MakeElipse [sic] method. Extracting the common code to generate the geometries into a method seems about the best way to proceed here.
The error message 'Value does not fall within the expected range' is a bit misleading. I don't see anything 'out of range', given that the exact same object was supposedly in range for your first ellipse. I can't say exactly why this error message is reported, but I can speculate. Silverlight is implemented in native code, and I believe that because the native code can't throw exceptions it instead returns numeric error codes. Perhaps there's a limited number of error codes and the one for 'Value does not fall within the expected range' was the one chosen for this error?

Click on button using UI Automation in Winform application in VC++

I want to click on a button using UI Automation. I am using UI Automation in Winform VC++.
Here is my code..
AutomationElement^ Select_connect_button= aeForm->FindFirst(TreeScope::Children,gcnew PropertyCondition(AutomationElement::NameProperty, "Select/Connect"));
InvokePattern^ ipClickButton1 = (InvokePattern)Select_connect_button->GetCurrentPattern(InvokePattern::Pattern);
ipClickButton1->Invoke();
but it is showing these Error:
error C2440: 'type cast' : cannot convert from 'System::Object ^' to 'System::Windows::Automation::InvokePattern'
error C2440: 'initializing' : cannot convert from 'System::Windows::Automation::InvokePattern' to 'System::Windows::Automation::InvokePattern ^'
Can anybody please help me to resolve these Errors.
Thanks.
The build error is you convert (InvokePattern) to "InvokePattern^".
In my testing, update the second line to below code will fix this issue:
InvokePattern^ ipClickButton1 = (InvokePattern^)Select_connect_button->GetCurrentPattern(InvokePattern::Pattern);
You can't cast with those objects. Here is one way to do it in C#. You can get the method names, etc. here. The various constants you need are in something like:
C:\Program Files\Microsoft SDKs\Windows\v7.0\Include\UIAutomationClient.h
(might be the v7.1 directory instead)
public static IUIAutomationInvokePattern elementToInvokePattern(this IUIAutomationElement element)
{
var conditionInvokePattern = auto.CreatePropertyCondition(
WindowsConstants.UIA_IsInvokePatternAvailablePropertyId,
true);
var cacheRequest = auto.CreateCacheRequest();
cacheRequest.AddPattern(WindowsConstants.UIA_InvokePatternId);
var cachedElement = element.FindFirstBuildCache(TreeScope.TreeScope_Element,
conditionInvokePattern,
cacheRequest);
var invokePattern = (IUIAutomationInvokePattern)
cachedElement.GetCachedPattern(WindowsConstants.UIA_InvokePatternId);
return invokePattern;
}
Looks like constants in the example are from here: http://msdn.microsoft.com/en-us/library/dd757483.aspx

Resources