Thursday, January 9, 2020

Testing private Methods in C#


Testing private methods in a C# can be a bit challenging. This is just to remember what's needed.


[TestClass]
private MyUnit _myUnit;

[TestMethod]
var testMyUnit = new PrivateObject(_myUnit);
...
var result = (List<string>)testMyUnit.Invoke("MyMethod", parameter1, parameter2); 

Assert.AreEqual(2, result.Count());


Original method (to be tested) in MyUnit:
private List<string> MyMethod(string sv1, string sv2)
{
   List<string> retval = new List<string>
                {
                sv1,
                sv2
                };
  return retval;
}


       

Saturday, December 29, 2018

Setting up atmega328 and CP2102 USB Module

For a while I've been playing around with different arduino compatible cards. I've been using JOY-IT UNO, Nano v3.0 atmega328P with integrated USB and now the atmega328P Mini which needs external USB Module to upload your sketches. Setting up is a bit complicated so I'll write instructions here so it will be easier next time, or if someone else wants to do the same thing. 

First you need to get drivers for you CP2102 module as Windows does not recognize it automatically. Silicon Labs has universal drivers for different operating systems. 


After successful installation you can see the port in device manager. Then you need to setup you breadboard so you have USB Module correctly connected to your atmega328P card. 

Note that USB module I'm using has power connections for 5V and 3,3V. My atMega card is 5V version so I've connected VCC into 5V. Should you have 3,3V card, you need to use 3,3V connection. Also If you have a reset (RST) instead of DTR in your USB card, you need to modify connection a bit. 


When you are done with wiring, you need to setup your IDE to support you connected USB device. Connect the USB cable and make sure PWR lights on both cards light up.  

I'm using Arduino IDE, in which you need to go to "Tools" - menu. Select port: "COM7" or whatever your device installed into earlier. Select card: "Arduino Pro or Arduino Pro Midi" and processor: "ATMega328P (5V 16MHz)". Should you use 8MHz or 3,3V card, make sure you make a correct selection, there were 4 versions when I wrote this. 

When you are finished, use for example this faster led_blink sketch to test it:

void setup() {                
  pinMode(13, OUTPUT);
}
void loop() {
      digitalWrite(13, LOW); // LED off
      delay(100);
      digitalWrite(13, HIGH);// LED on
      delay(100);

}

Upload sketch into your card and voila, you have a really small arduino compatible card ready for whatever you are going to do with it.






Thursday, December 27, 2018

Configure Raspberry Pi+ 3 with 3,5" Touch Screen Display

I just got a cheap 3,5" Raspberry LCD that I ordered from eBay. It seems that when you get a bargain, you get what you order, nothing else. There was no instructions manual nor drivers with the display. Luckily there was a small label printed in LCD box with some information about it:
Driver: ILI9486, Dots: 320x480, Touch:Yes, SKU: MPI3501

After googling I figured out that it's most probably this display:
http://www.lcdwiki.com/zh/3.5inch_RPi_Display
and the drivers can be downloaded from here:
http://www.lcdwiki.com/3.5inch_RPi_Display
https://github.com/goodtft/LCD-show

After extracting the driver into:
 /home/pi/LCD-show

Open LXTerminal and use commands:
cd LCD-show
./LCD35-show 
and your Raspberry Pi reboots and starts with your 3,5" display.

From 3,5" display back to normal HDMI view open LXTerminal again and use commands:
cd LCD-show
./LCD-hdmi

Sunday, October 21, 2018

Update Linx7 tablet to Windows 1803

Some time ago I wrote about 1803 upgrade problem I had with my desktop PC.

I had another upgrade problem with my Linx7 tablet. This tablet has only 1 gigabyte of memory and 32 gigabyte disk, but it can still run 1803.

In this case the tablet refused to update and rolled back simply because of too low free space in my disk drive C:\ (6 Gb). From the users perspective it would be great if Microsoft's update would first check if it has enough free space to complete the installation before even trying it...

First I emptied the recycle bin and used Disk Cleanup to get rid of extra files. That way I gained around 4 gigabytes. Deleting Windows ESD installation files may free ~3Gb of disk space.

To figure out what else has been eating my disk space I downloaded a tool called SpaceSniffer. Using it I figured out that Windows component store (c:\Windows\WinSxS) had grown to almost 7 gigabytes. There is a way to reduce it's size. You can use elevated command prompt (Start CMD as Administrator) by giving a command:

Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase

/ResetBase switch option deletes all superseded versions of every component in the component store. Note that you can't uninstall existing service packs and updates after completing this command, but it does not prevent installing future updates. I got more than a gigabyte freed by that trick.

If you still don't have enough free space, you can try compacting you os installation:
Compact.exe /CompactOS:always

Also you can gain a bit more space by removing you hibernation file:
powercfg /h off

With approximately 11 gigabytes of free space when upgrade 1803 was loaded, it finally installed without a problem.

Sunday, August 19, 2018

Windows 10 upgrade from 1709 to 1803 kept failing at 86% no matter what.

Windows 10 upgrade from 1709 to 1803 fails. Maybe other upgrades too? Provisioning may be the reason for the failure. 

Symptoms: Installation stops at 86% no matter what, reverts back to 1709 and gives an Error Code: 0xC1900101-0x4001E  "The installation failed in the SECOND_BOOT phase with an error during PRE_OOBE operation"

Since April 2018, My Windows 10 has been trying to update itself to version 1803 and every single time the update has failed. So it took several months to succeed, but finally today (2018-8-19) I got my Windows updated to 1803 and no clean install was needed. All my software and app's are still in place. That was the main reason to keep trying, installing all the app´s again was not an option. 

When automatic update fails, there are some alternative solutions. Lots of articles suggest removing 3'rd party antivirus software, external devices etc. Some sites suggest disabling Intel virtualization etc. but according to my experience it's nonsense. After trying all that and a lot more, I also chose to update my motherboards and SSD's firmware as suggested in some sources. That obviously solved nothing either. 

When automatic update had failed several times, it was time to try Windows Update Assistant. It may have solved some cases, but not this one. After that, I took some time using Media Creation tool to create DVD for installation. As expected, it failed too. Then I used it to create bootable USB and tried updating from it. Same crash happened again. 

At this point I figured out that I need to find out the real reason behind the problem. By googling around, I figured out that there is no definitive answer for the error code given by Windows. Finally I found out that there are hidden logs created during the update process, telling the real reason for crash. They are the only way to figure out what really caused the crash. 

The answer lies within "setuperr" -logfile which Microsoft places into hidden folder structure just to make things a bit more complicated. File "setuperr" can usually be found from folder: "C:\$Windows.~BT\source\panther\". Should it not be readable over there (user rights), you can copy it to alternative location and read using notepad etc. 


ERROR:

In my case the fatal error was found at last line of "setuperr" - logfile:
2018-08-18 15:45:16, FatalError [0x090001] PANTHR Exception (code 0xC0000005: ACCESS_VIOLATION) occurred at 0x00007FFF80CF494B in C:\Windows\System32\provengine.dll (+000000000000494B). 
Minidump attached (99815 bytes) to diagerr.xml and C:\Windows\Panther\mnd68D9.diagerr.mdmp.

At this point I started googling around about Provengine and figured out that it's related to Windows 10 Provisioning packages which are located at C:\Windows\Provisioning -folder and registry tree HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Provisioning. As I have installed my Windows 10 from the scratch couple of years ago, I should have only Microsoft's default packages, so it seemed that something (Microsoft probably) has messed my registry settings. 

Having a Windows 10 as virtual machine also, I was able to compare the folder structures both in registry and C:\Windows\Provisioning and figured out that there are some differences in the registry tree though there should not be, so I decided to try to replace "Proviosioning" tree by copying it from my virtual machine. At this point I also found out that someone else had tried this too, so I decided to give it a go. 

Edit (2018-8-20): I've checked also Windows Home versions registry and Provisioning tree is very similar in both so I've added both Pro and Home registry into this document in case you have no access to suitable 1709 version. 

SOLUTION  (Or how I finally got my Windows Updated to 1803)

NOTE! Actions suggested here require using registry editor (regedit)! If you don't understand what you are doing, don't do it and stay with 1709 version or ask some technical person to help you! Make sure your problem is also with provengine.dll, otherwise you'll just probably damage your Windows even more! Try this at your own risk. I can't quarantee this will work with your system! Be careful! Do not mess around registry elsewhere but under KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Provisioning


STEPS:

YOU NEED WORKING REGISTRY BRANCH, GET ONE FROM ANOTHER PC WITH WINDOWS 1709 INSTALLED OR DOWNLOAD ONE I'VE CREATED

1) Export registry tree KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Provisioning 
Select "Provisioning" and right click "Export"... and save file as "provisioning.reg"

Edit (2018-8-20): I've created registry entries for both Windows 10 Pro and Windows 10 Home in case you have no Windows v. 1709 available. There seem to be only minor differences in these files. Both files are in txt -format, so you need to rename one before you can import. I used the Pro version to fix my registry. I can't guarantee these will work for you!

ON A MACHINE WHERE UPDATE FAILS

Importing registry may be a bit harder. I deleted all the old keys (under Provisioning naturally) first as there were some extra branches under provisioning which did not exist in my virtual Windows. Anyway, removing them required that I took the ownership of keys first as I'm not usually running my system as an Administrator. After that you may proceed:

2) Make backup of your "KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Provisioning" 
just in case all fails and you need to get them back (don't know why though). If you are still reading this, there's a big change they're corrupted anyway so... 


REMOVE SUBKEYS UNDER: KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Provisioning

3) You need to start regedit as Administrator or as with SYSTEM account.
4) Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Provisioning
5) Delete everything inside Provisioning (See how to take ownership if needed)
6) Using regedit.exe as administrator to Import Provisioning registry settings (provisioning.reg) you created at the other machine. 
7) If there are differences between files in subfolders of C:\Windows\Provisioning you may copy files to there also from another PC.

Note: I renamed the folder "C:\Windows\Provisioning" to "Provisioning.old" before copying. As I have a Virtual Machine with identical setup, I was able to copy the folder incredients from there. When comparing data though it seemed identical so I guess that fixing registry was/is enough. 

That's it. Now you can retry update again and it should go pass 86% and Windows 1803 should work after installation is completed. 

Btw: If you renamed old Provisioning -> Provisioning.old like I did:

To clean up "provisioning.old", you may need to use CMD (Execute it as Administrator) and take the ownership of incredients:

takeown /f "C:\Windows\Provisioning.old"​
takeown /f "C:\Windows\Provisioning.old\Packages"
icacls "C:\Windows\Provisioning.old\Packages" /grant Admin:F
cd Packages
del *.*
rmdir Packages
takeown /f "C:\WINDOWS\Provisioning.old\Cosa\"
takeown /f "C:\WINDOWS\Provisioning.old\Cosa\Microsoft"
icacls "C:\WINDOWS\Provisioning.old\Cosa\Microsoft\Microsoft.Windows.Cosa.Desktop.Client.ppkg" /grant Admin:F
etc...


Great thanks to Microsoft for making our lives so hard! I wonder how many people will end up doing clean install or buying new PC after all the frustration with 1803 update.  

P.S. If you don't have another Windows machine where you could copy the registry settings for Provisioning, you may consider creating a virtual Windows 10 machine using for example VmWare workstation player.  https://www.vmware.com/products/workstation-player.html.

Testing private Methods in C#

Testing private methods in a C# can be a bit challenging. This is just to remember what's needed. [TestClass] private MyUnit _myUni...