Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Thursday, October 4, 2018

HttpClient.GetAsync times out when connected to VPN

Leave a Comment

C# 4.5.2 framework HttpClient.GetAsync() method works fine on Windows 10 when system is not using VPN.

When VPN is connected HttpClient.GetAsync() call to the same address just blocks until it times out. Both Edge and Chrome have no issues accessing that same address.

Is there a way to see what is happening? What is HttpClient doing differently?

Update: Got some interesting clues by calling Dns.GetHostEntry(). Without VPN this call returned only IPv4 addresses that all could be connected to. With VPN client connected Dns.GetHostEntry() returned additional IPv6 addresses at the top of the list. Connection to all IPv6 addresses timed out but all IPv4 ones still worked OK. Now is there a way to figure out without trying to connect which addresses work and which ones do not?

2 Answers

Answers 1

In my experience, this sounds like a VPN / firewall issue to me. One quick thing to toggle in windows is under you VPN adapter properties, try unchecking "Use default gateway on remote network" - I know it sounds like a long shot but have had this problem in the past... enter image description here

Answers 2

Have to answer this myself as this problem has a simple cause but very confusing symptoms.

The root cause:

DNS reports only IPv4 addresses for the host when system is not connected to VPN. All IPv4 addresses are usable.

When VPN connection is active DNS returns IPv6 addresses in addition to IPv4. IPv4 addresses are still accessible but IPv6 are not.

The cause of such invalid network configuration is still a mystery that deserves its own separate post.

Confusing part:

Some apps work no matter what VPN connection status is.

"But web browser can connect to the same host with or without VPN." True. Browsers may use Happy eyeballs approach attempting to connect using both IPv4 and IPv6 at the same time.

"But my old app has not problems connecting." Also true. Some older and not so old apps use IPv4 protocol by default. Support for IPv6 or IPv4+IPv6 has to be explicitly implemented.

"But it works sometimes". This happens when VPN connections are not reliable. It leads to all sorts of solutions that are mere coincidences.

What exactly is happening:

HttpClient.GetAsync() uses default DNS resolution and can connect using both IPv4 and IPv6 addresses. It does not discriminate and there is no direct way to influence protocol selection. If DNS returns inaccessible address then HttpClient may use that invalid address to connect resulting in timeout.

Possible workarounds:

The best: ask IT to fix IPv6 DNS issues. DNS should not report inaccessible addresses.

Good: implement Happy eyeballs approach. Connect to both IPv6 and IPv4 host addresses using numeric IP instead of automatic resolution using host name.

OK: Always connect to IPv4 using numeric IP.

Here is the piece of code that shows how to connect to a specific IP address:

// Get DNS entries for the host. var hostEntry = Dns.GetHostEntry(uri.Host);   // Get IPv4 address var ip4 = hostEntry.AddressList.First(addr => addr.AddressFamily == AddressFamily.InterNetwork); // Build URI with numeric IPv4 var uriBuilderIP4 = new UriBuilder(uri);  uriBuilderIP4.Host = ip4.ToString());  var uri4 = uriBuilder4.Uri;   // Get IPv6 address var ip6 = hostEntry.AddressList.First(addr => addr.AddressFamily == AddressFamily.InterNetworkV6);  // Build URI with numeric IPv6 var uriBuilderIP6 = new UriBuilder(uri);  uriBuilderIP6.Host = $"[{ip6}]";  var uri6 = uriBuilder6.Uri;  

For HTTPS connections numeric addresses work only with "host" header with the name of the host (not an IP address) in it. Here is the way to add it.

var client = new HttpClient();  // Add "host" header with real host name e.g. stackoverflow.com  client.DefaultRequestHeaders.Add("Host", uri.Host);  
Read More

Friday, September 21, 2018

Pip --user installs package to Default user directory on Windows 10

Leave a Comment

I have a problem on Windows 10 where both Python 2.6 and 2.7 are installed.

python -m pip install myPack --no-index --find-links=. --user 

When running this command with user AutoUser it installs myPack to Default user directory C:\Users\Default\Python\Python27\site-packages or C:\Users\Default\Appdata\Roaming\Python\site-packages instead C:\Users\Autouser\Appdata\Roaming\Python\site-packages

  • Installation is automatic soon after windows logon, but I can see in logs that "query user" returns a row with AutoUser (before calling pip).
  • Other OS don't have this problem.
  • Reproduction is unstable on Windows 10: maybe 1 time of 100.
  • Truth that python 2.6 is also installed on these machines, but I'm not sure it is meaningful: 2.6 goes later than 2.7 in Path system variable. Here they write it could be a problem, but pip doesn't confuse python versions, it confuses users' directories.

Path:

C:\ProgramData\Oracle\Java\javapath;C:\Python27\;C:\Python27\Scripts\;C:\Python26\;C:\Python26\Scripts;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\ProgramData\chocolatey\bin; 

Python version:

python --version Python 2.7.13 

Pip version:

python -m pip --version Pip version: pip 9.0.1 from C:\Python27\lib\site-packages (python 2.7)  

1 Answers

Answers 1

You can try setting the install target with the --target option like so:

pip install --target=C:\Users\Autouser\Appdata\Roaming\Python\site-packages package_name 

If that doesn't work, another option is to try using --install-option like this:

pip install --install-option="--prefix=$PREFIX_PATH" package_name 

Finally, if all else fails, here's one more way to do it:

PYTHONUSERBASE=/path/to/install/to pip install --user 

You can specify which python version to install the package for by using python2.x -m pip install ...

Hopefully one of these helps you! :)

Read More

Wednesday, September 19, 2018

Problems with Impersonated Service and Network Locations

Leave a Comment

I have a windows service that polls a user selected directory for new files.When a Network location is selected (a folder on a remote machine on the same network),i query the user for the username and password of the remote machine (user who has access to the folder),i validate the same and store it to the registry for the service.This approach works fine.(service runs as localmachine and uses impersonation code).

But there are some scenarios that im unaware of ..

1. Wireless HDDs /Storage devices that are directly connected to the network.Currently i use impersonation code to supply user credentials.In such scenarios will the drive be directly accessible to the service running as localmachine?

2. How to deal with Domain Users who has access to the Folder?

I'm using the following code to impersonate a user

https://stackoverflow.com/a/9213965/848968

UserImpersonation impersonator = new UserImpersonation(); impersonator.impersonateUser("username", "", "password"); //No Domain is required 

What to do in case of domain users?

1 Answers

Answers 1

  1. Wireless HDDs /Storage devices that are directly connected to the network.Currently i use impersonation code to supply user credentials.In such scenarios will the drive be directly accessible to the service running as localmachine?

How is the drive connected to? If its SMB via UNC paths, that should be fine. When you get into mapped drives, those are not available to the windows service.

  1. How to deal with Domain Users who has access to the Folder?

impersonator.impersonateUser("username", "domain", "password");

You may also want to check out this answer for more impersonation information and sample code. There's a great library that wraps this up for you.

Read More

Tuesday, September 11, 2018

Unable to run cygwin in Windows Docker Container

Leave a Comment

I've been working with Docker for Windows, attempting to create a Windows Container that can run cygwin as the shell within the container itself. I haven't had any luck getting this going yet. Here's the Dockerfile that I've been messing with.

# escape=` FROM microsoft/windowsservercore  SHELL ["powershell", "-command"]  RUN Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression RUN choco install cygwin -y RUN refreshenv RUN [Environment]::SetEnvironmentVariable('Path', $env:Path + ';C:\tools\cygwin\bin', [EnvironmentVariableTarget]::Machine) 

I've tried setting the ENTRYPOINT and CMD to try and get into cygwin, but neither seems to do anything. I've also attached to the container with docker run -it and fired off the cygwin command to get into the shell, but it doesn't appear to do anything. I don't get an error, it just returns to the command prompt as if nothing happened.

Is it possible to run another shell in the Windows Container, or am I just doing something incorrectly?

Thanks!

2 Answers

Answers 1

You don't "attach" to a container with docker run: you start a container with it.

In your case, as seen here, docker run -it is the right approach.

You can try as an entry point using c:\cygwin\bin\bash, as seen in this issue.

As commented in issue 32330:

Don't get me wrong, cygwin should work in Docker Windows containers.

But, it's also a little paradoxical that containers were painstakingly wrought into Windows, modeled on containers on Linux, only for people to then want to run Linux-utils in these newly minted Docker Windows containers...

That same issue is still unresolved, with new case seen in May and June 2018:

We have an environment that compiles with Visual Studio but still we want to use git and some very useful commands taken from linux.
Also we use of-the-shelve utilities (e.g. git-repo) that uses linux commands (e.g. curl, grep,...)

Some builds require Cygwin like ICU (a cross-platform Unicode based globalization library), and worst: our builds require building it from source.


You can see an example of a crash in MSYS2-packages issue 1239:

Step 5/5 : RUN "C:\\msys64\\usr\\bin\\ls.exe"  ---> Running in 5d7867a1f8da The command 'cmd /S /C "C:\\msys64\\usr\\bin\\ls.exe"' returned a non-zero code: 3221225794 

This can get more information on the crash:

PS C:\msys64\usr\bin>    Get-EventLog -Index 28,29,30 -LogName "Application" | Format-List -Property * 

The workaround was:

PS > xcopy /S C:\Git C:\Git_Copy PS > C:\Git_Copy\usr\bin\sh.exe --version > v.txt PS > type v.txt 

As mentioned in that thread, the output gets lost somewhere in the container, thus sending it to a text file.

Answers 2

Mainly the solution is to use winpty ahead of mintty

Read More

Monday, September 10, 2018

Excel as inlay frame in WPF has disabled ExcelWorksheet

Leave a Comment

I found a solution to setup Excel instance in WPF by using the SetParent() function of Windows.

Problem is, that mouse and keyboard is not reacting to the sheet but to the Workbook it does.

Full sample Project Download here

I also tried with WindowsFormsHost but it has the same effect.

XAML

<Window x:Class="ExcelEditor.SimpleWindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:local="clr-namespace:ExcelEditor"     mc:Ignorable="d" Loaded="Window_Loaded" Closing="Window_Closing"     Title="SimpleWindow" Height="450" Width="800"> <Grid x:Name="LayoutRoot">  </Grid> 

C# code

    using System;     using System.Windows;   namespace ExcelEditor {     /// <summary>     /// Interaktionslogik für SimpleWindow.xaml     /// </summary>     public partial class SimpleWindow : Window     {         private Microsoft.Office.Interop.Excel.Application ExcelApplication;          public SimpleWindow()         {             InitializeComponent();         }          private void Window_Loaded(object sender, RoutedEventArgs e)         {             System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(this); // <- testing only (no success)             ExcelApplication = new Microsoft.Office.Interop.Excel.Application();             ExcelApplication.DisplayAlerts = false;             System.Windows.Interop.HwndSource hwnd = (System.Windows.Interop.HwndSource)System.Windows.Interop.HwndSource.FromVisual(this.LayoutRoot);             var excelWnd = Microsoft.Win32.Interop.FindWindow("XLMAIN", null);             GenerateTestData(ExcelApplication);             var successOfParentSetup = Microsoft.Win32.Interop.SetParent(excelWnd, hwnd.Handle);             var isMovedToLeftTop = Microsoft.Win32.Interop.MoveWindow(excelWnd, 10, 10, 800, 600, true);             ExcelApplication.Visible = true;         }          private void GenerateTestData(Microsoft.Office.Interop.Excel.Application excelApplication)         {             var excelWorkbook = excelApplication.Workbooks.Add();             var excelWorksheet = excelWorkbook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;             excelWorksheet.Cells[1, 1] = "Today";             excelWorksheet.Cells[2, 1] = DateTime.Now.ToString();         }          private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)         {             if (ExcelApplication != null)             {                 ExcelApplication.ActiveWorkbook.Close();                 ExcelApplication.Visible = false;                 ExcelApplication.Quit();                 ExcelApplication = null;             }         }     } } 

Api calls

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]     public static extern bool SetWindowPos(         IntPtr hWnd,               // handle to window         IntPtr hWndInsertAfter,    // placement-order handle         int X,                  // horizontal position         int Y,                  // vertical position         int cx,                 // width         int cy,                 // height         uint uFlags             // window-positioning options     );      [DllImport("user32.dll", EntryPoint = "MoveWindow")]     public static extern bool MoveWindow(         IntPtr hWnd,         int X,         int Y,         int nWidth,         int nHeight,         bool bRepaint     );     [DllImport("USER32.DLL", SetLastError = true)]     public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);      //Sets a window to be a child window of another window     [DllImport("USER32.DLL", SetLastError = true)]     public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 

Question is, how to enable the mouse and keyboard handling in Excel to allow editing the worksheet?

Excel not working propert as parent

*Tested at: Windows 10 x64, Single screen (optional dual screen) Excel 2016 MSO 32-Bit (16.0.10325.20082) out of Office 365

This issue is also reported on Microsoft Forum to check if there is a solution.

In the following video you see how the click sometimes will select cells and sometimes it will not recognize the mouse.

Excel

1 Answers

Answers 1

You need to make sure Excel has been shown once before you can call SetParent. Of course, it looks ugly, so you also need to hide Excel UI somehow from the end-user. For example:

    private void Window_Loaded(object sender, RoutedEventArgs e)     {         ...         // get excel window         var excelWnd = Microsoft.Win32.Interop.FindWindow("XLMAIN", null);          // move excel somewhere outside the screen (of course you should compute it, not hardcode it)         Microsoft.Win32.Interop.MoveWindow(excelWnd, -10000, -10000, 800, 600, true);          // show it so it's initialized properly         ExcelApplication.Visible = true;         GenerateTestData(ExcelApplication);          // now call SetParent         Microsoft.Win32.Interop.SetParent(excelWnd, hwnd.Handle);          // move it to parent         Microsoft.Win32.Interop.MoveWindow(excelWnd, 10, 10, 800, 600, true);     } 
Read More

Saturday, September 1, 2018

Excel as inlay frame in WPF has disabled ExcelWorksheet

Leave a Comment

I found a solution to setup Excel instance in WPF by using the SetParent() function of Windows.

Problem is, that mouse and keyboard is not reacting to the sheet but to the Workbook it does.

Full sample Project Download here

I also tried with WindowsFormsHost but it has the same effect.

XAML

<Window x:Class="ExcelEditor.SimpleWindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:local="clr-namespace:ExcelEditor"     mc:Ignorable="d" Loaded="Window_Loaded" Closing="Window_Closing"     Title="SimpleWindow" Height="450" Width="800"> <Grid x:Name="LayoutRoot">  </Grid> 

C# code

    using System;     using System.Windows;   namespace ExcelEditor {     /// <summary>     /// Interaktionslogik für SimpleWindow.xaml     /// </summary>     public partial class SimpleWindow : Window     {         private Microsoft.Office.Interop.Excel.Application ExcelApplication;          public SimpleWindow()         {             InitializeComponent();         }          private void Window_Loaded(object sender, RoutedEventArgs e)         {             System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(this); // <- testing only (no success)             ExcelApplication = new Microsoft.Office.Interop.Excel.Application();             ExcelApplication.DisplayAlerts = false;             System.Windows.Interop.HwndSource hwnd = (System.Windows.Interop.HwndSource)System.Windows.Interop.HwndSource.FromVisual(this.LayoutRoot);             var excelWnd = Microsoft.Win32.Interop.FindWindow("XLMAIN", null);             GenerateTestData(ExcelApplication);             var successOfParentSetup = Microsoft.Win32.Interop.SetParent(excelWnd, hwnd.Handle);             var isMovedToLeftTop = Microsoft.Win32.Interop.MoveWindow(excelWnd, 10, 10, 800, 600, true);             ExcelApplication.Visible = true;         }          private void GenerateTestData(Microsoft.Office.Interop.Excel.Application excelApplication)         {             var excelWorkbook = excelApplication.Workbooks.Add();             var excelWorksheet = excelWorkbook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;             excelWorksheet.Cells[1, 1] = "Today";             excelWorksheet.Cells[2, 1] = DateTime.Now.ToString();         }          private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)         {             if (ExcelApplication != null)             {                 ExcelApplication.ActiveWorkbook.Close();                 ExcelApplication.Visible = false;                 ExcelApplication.Quit();                 ExcelApplication = null;             }         }     } } 

Api calls

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]     public static extern bool SetWindowPos(         IntPtr hWnd,               // handle to window         IntPtr hWndInsertAfter,    // placement-order handle         int X,                  // horizontal position         int Y,                  // vertical position         int cx,                 // width         int cy,                 // height         uint uFlags             // window-positioning options     );      [DllImport("user32.dll", EntryPoint = "MoveWindow")]     public static extern bool MoveWindow(         IntPtr hWnd,         int X,         int Y,         int nWidth,         int nHeight,         bool bRepaint     );     [DllImport("USER32.DLL", SetLastError = true)]     public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);      //Sets a window to be a child window of another window     [DllImport("USER32.DLL", SetLastError = true)]     public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 

Question is, how to enable the mouse and keyboard handling in Excel to allow editing the worksheet?

Excel not working propert as parent

*Tested at: Windows 10 x64, Single screen (optional dual screen) Excel 2016 MSO 32-Bit (16.0.10325.20082) out of Office 365

1 Answers

Answers 1

You need to make sure Excel has been shown once before you can call SetParent. Of course, it looks ugly, so you also need to hide Excel UI somehow from the end-user. For example:

    private void Window_Loaded(object sender, RoutedEventArgs e)     {         ...         // get excel window         var excelWnd = Microsoft.Win32.Interop.FindWindow("XLMAIN", null);          // move excel somewhere outside the screen (of course you should compute it, not hardcode it)         Microsoft.Win32.Interop.MoveWindow(excelWnd, -10000, -10000, 800, 600, true);          // show it so it's initialized properly         ExcelApplication.Visible = true;         GenerateTestData(ExcelApplication);          // now call SetParent         Microsoft.Win32.Interop.SetParent(excelWnd, hwnd.Handle);          // move it to parent         Microsoft.Win32.Interop.MoveWindow(excelWnd, 10, 10, 800, 600, true);     } 
Read More

Thursday, August 16, 2018

SSL Secure Renegotiation with .NET SslStream

Leave a Comment

I'm trying to do secure renegotiation with .NET SslStream, but I'm not sure how to achieve it, or if it is even possible. My goal is to renew the shared secret, using the same certificate for the server.

I set up a server using OpenSSL :

s_server -accept 443 -key privkey.pem 

Here's my client code :

    public static void RunClient()     {         TcpClient client = new TcpClient("localhost", 443);         Console.WriteLine("Client connected.");         SslStream sslStream = new SslStream(innerStream: client.GetStream(), leaveInnerStreamOpen: true, userCertificateValidationCallback: (a,b,c,d) => true, userCertificateSelectionCallback: null);         sslStream.AuthenticateAsClient("localhost");         byte[] message = Encoding.UTF8.GetBytes("Hello from the client.");         sslStream.Write(message);         sslStream.Dispose();         // if code below is commented, there's no error.         sslStream = new SslStream(innerStream: client.GetStream(), leaveInnerStreamOpen: true, userCertificateValidationCallback: (a, b, c, d) => true, userCertificateSelectionCallback: null);         message = Encoding.UTF8.GetBytes("Hello from the client again, renegotiated.");         sslStream.AuthenticateAsClient("localhost");         sslStream.Write(message);         //         client.Close();         Console.WriteLine("Client closed.");     }     public static int Main(string[] args)     {         RunClient();         return 0;     } 

The second call to AuthenticateAsClient, on the new SslStream fails with "A call to SSPI failed, see inner exception.". The inner exception is "Win32Exception: The message received was unexpected or badly formatted".

On the server side, I get an error too :

CIPHER is ECDHE-RSA-AES256-GCM-SHA384 Secure Renegotiation IS supported Hello from the client.ERROR 8404:error:1408F119:SSL routines:ssl3_get_record:decryption failed or bad record mac:ssl\record\ssl3_record.c:469: shutting down SSL CONNECTION CLOSED ACCEPT 

Do you know what's wrong here, or if my use case is even supported with SslStream ?

0 Answers

Read More

Wednesday, August 1, 2018

Memory Error in Python 3 and Windows 64

Leave a Comment

I know Memory Error related questions have been asked before, for example here, here, here, here, or here. And the suggested solutions always are to switch to Python 3 and/or to Window 64bit, or in case of faulty code, to fix the code. However, I am already on Python 3 and Win 64. I also can see from windows task manager that I have several GB of my 64GB of RAM still available when Python throw the Memory Error.

I have about 15 date-indexed, pandas data frames each with 14000 rows and on average 5000 columns of float data, and about 40-50% NaN values, that I read in from the hard drive. I can not simply drop NaNs because different columns have NaNs at different dates. Memory Error happens when I try to concatenate them with pd.concat(). So it's not a matter of some faulty code or while loop. If I leave some of the data frames out of concatenation, Memory Error does not happen in concatenation, but then when I try to do a Scikit learn decision tree analysis on the concatenated data, it happens.

My question is how can I get Python to use all the available memory and not throw Memory Error?

Edit: screenshots added IPython interpreter screenshot (I don't have Python 2 even installed): enter image description here

System information screenshot: enter image description here

1 Answers

Answers 1

Here are some links with snippets of information from them, I hope it helps.

How to give programs more RAM

Increase your pagefile. The pagefile is a section of the hard drive that is used as RAM by the processor and is also called Virtual Memory. Although it is not as fast as RAM because it is physically located on a hard drive, increasing it can sometimes increase program performance. To access it, open your Control Panel. Click "System," then "Advanced system settings," and then "Settings" in the "Performance" tab.

How do you set the memory usage for python programs

if u wanna limit the python vm memory usage,you can try this: 1、Linux, ulimit command to limit the memory usage on python 2、you can use resource module to limit the program memory usage; if u wanna speed up ur program though giving more memory to ur application, you could try this: 1\threading, multiprocessing 2\pypy 3\pysco on only python 2.5

Assign memory application

!.Right-click MY COMPUTER 2.Properties>Advanced>Performance>Settings>Advanced 3.Under, Virtual Memory check Paging File size,(if memory serves, the rule of thumb is 1/2 of total V. Memory may be used. If you use more, the paging file fights between the app. and the OS. Hope this help's

Allocate memory process server

Because some programs need more memory than others to work, you can use the Task Manager to allocate extra memory to specific processes in order to increase performance.

Others

http://stackoverflow.com/questions/1760025/limit-python-vm-memory

http://stackoverflow.com/questions/2308091/how-to-limit-python-heap-size

Read More

Wednesday, June 20, 2018

E2E test orchestration with Gulp on Windows: Unable to kill process(es)

Leave a Comment

What I'm trying to achieve

This question is related to another one I recently closed with a horrible hack™.

I am trying to write a script that can be used a step in a context of a CI/build pipeline.

The script is supposed to run Protractor-based end-to-end tests for our Angular single-page application (SPA).

The script is required to do the following actions (in order):

  1. run a .NET Core microservice called "App"
  2. run a .NET Core microservice called "Web"
  3. run the SPA
  4. run a command that executes Protractor tests
  5. after steps 4 is complete (either successfully or with an error), terminate processes created on steps 1-3. This is absolutely necessary otherwise the build will never finish in CI and/or there will be zombie Web/App/SPA processes which will break future build pipeline execution.

The issue

I haven't started working on step 4 ("e2e test") because I really want to make sure that the step 5 ("cleanup") works as intended.

As you could guess (right), the cleanup step does not work. Specifically, the processes "App" and "Web" do not get killed for some reason and continue running.

BTW, I made sure that my gulp script is executed with elevated (admin) privileges.

Issue - UPDATE 1

I have just discovered the direct cause of the issue (I think), I don't know what's the root cause though. There are 5 processes launched instead of 1 as I was expecting. E.g., for App process the following processes are observed in Process manager:

{                             "id": 14840,                "binary": "cmd.exe",        "title": "Console"        },                          {                             "id": 12600,                "binary": "dotnet.exe",     "title": "Console"        },                          {                             "id": 12976,                "binary": "cmd.exe",        "title": "Console"        },                          {                             "id": 5492,                 "binary": "cmd.exe",        "title": "Console"        },                          {                             "id": 2636,                 "binary": "App.exe",   "title": "Console"        }                           

Similarly, five processes rather than one are created for Web service:

{                             "id": 13264,                "binary": "cmd.exe",        "title": "Console"        },                          {                             "id": 1900,                 "binary": "dotnet.exe",     "title": "Console"        },                          {                             "id": 4668,                 "binary": "cmd.exe",        "title": "Console"        },                          {                             "id": 15520,                "binary": "Web.exe",   "title": "Console"        },                          {                             "id": 7516,                 "binary": "cmd.exe",        "title": "Console"        }                           

How I am doing that

Basically, the work horse here is the runCmdAndListen() function that spins off the processes by running the cmd provided as an argument. When the function launches a process be the means of Node.js's exec(), it is then pushed to the createdProcesses array for tracking.

The Gulp step called CLEANUP = "cleanup" is responsible for iterating through the createdProcesses and invoking .kill('SIGTERM') on each of them, which is supposed to kill all the processes created earlier.

gulpfile.js (Gulp task script)

Imports and constants

const gulp = require('gulp'); const exec = require('child_process').exec; const path = require('path');  const RUN_APP = `run-app`; const RUN_WEB = `run-web`; const RUN_SPA = `run-spa`; const CLEANUP = `cleanup`;  const appDirectory = path.join(`..`, `App`); const webDirectory = path.join(`..`, `Web`); const spaDirectory = path.join(`.`);  const createdProcesses = []; 

runCmdAndListen()

/**  * Runs a command and taps on `stdout` waiting for a `resolvePhrase` if provided.  * @param {*} name Title of the process to use in console output.  * @param {*} command Command to execute.  * @param {*} cwd Command working directory.  * @param {*} env Command environment parameters.  * @param {*} resolvePhrase Phrase to wait for in `stdout` and resolve on.  * @param {*} rejectOnError Flag showing whether to reject on a message in `stderr` or not.  */ function runCmdAndListen(name, command, cwd, env, resolvePhrase, rejectOnError) {    const options = { cwd };   if (env) options.env = env;    return new Promise((resolve, reject) => {     const newProcess = exec(command, options);      console.info(`Adding a running process with id ${newProcess.pid}`);     createdProcesses.push({ childProcess: newProcess, isRunning: true });      newProcess.on('exit', () => {       createdProcesses         .find(({ childProcess, _ }) => childProcess.pid === newProcess.pid)         .isRunning = false;     });      newProcess.stdout       .on(`data`, chunk => {         if (resolvePhrase && chunk.toString().indexOf(resolvePhrase) >= 0) {           console.info(`RESOLVED ${name}/${resolvePhrase}`);           resolve();         }       });      newProcess.stderr       .on(`data`, chunk => {         if (rejectOnError) reject(chunk);       });      if (!resolvePhrase) {       console.info(`RESOLVED ${name}`);       resolve();     }   }); } 

Basic Gulp tasks

gulp.task(RUN_APP, () => runCmdAndListen(   `[App]`,   `dotnet run --no-build --no-dependencies`,   appDirectory,   { 'ASPNETCORE_ENVIRONMENT': `Development` },   `Now listening on:`,   true) );  gulp.task(RUN_WEB, () => runCmdAndListen(   `[Web]`,   `dotnet run --no-build --no-dependencies`,   webDirectory,   { 'ASPNETCORE_ENVIRONMENT': `Development` },   `Now listening on:`,   true) );  gulp.task(RUN_SPA, () => runCmdAndListen(   `[SPA]`,   `npm run start-prodish-for-e2e`,   spaDirectory,   null,   `webpack: Compiled successfully   `,   false) );  gulp.task(CLEANUP, () => {   createdProcesses     .forEach(({ childProcess, isRunning }) => {       console.warn(`Killing child process ${childProcess.pid}`);        // if (isRunning) {       childProcess.kill('SIGTERM');       // }     }); }); 

The orchestrating task

gulp.task(   'e2e',   gulp.series(     gulp.series(       RUN_APP,       RUN_WEB,     ),     RUN_SPA,     CLEANUP,   ),   () => console.info(`All tasks complete`), );  gulp.task('default', gulp.series('e2e')); 

0 Answers

Read More

Friday, June 1, 2018

Firefox not updating application associations in Preferences on a Windows machine

Leave a Comment

When a JNLP is launched from a Firefox browser, the first time, it shows the following dialog . If I check the "do this automatically" check box, from the next time on, it doesn't ask this question.

The preferences seem to get stored in Firefox-> Options-> General-> Preferences.

Preferences

This works just fine on my machine. At a customer site, it doesn't seem to get updated. Even after checking the checkbox, each time it asks again & preferences also don't seem to get updated. What kind of permission/privilege issues could be causing this? How can I troubleshoot this?

1 Answers

Answers 1

Try and reinstall Firefox on the customer site

Read More

Tuesday, May 22, 2018

Is the HPET directly accessible in Windows?

Leave a Comment

I would like to use the High Performance Event Timer (HPET) for an profiling tool to take very high precision measurements, quickly. timeGetTime does not provide sufficient resolution at 1ms, and QueryPerformanceCounter is much slower per read than I'd like. I came across the HPET while researching the problem, but I can't see any samples of how to actually get at it.

So can I use it directly (assembly is fine), or do I have to rely on the multimedia/high performance timing tools already built into the Win32 API?

2 Answers

Answers 1

I found this info while digging around, and it seems it can be the most cost effective way. I will try it out when I get the guts to dig into assembly. :)

UPDATE

I tested this with my profiler. although a bit faster, it seems I still have tonnes of other overhead :( (I did not bother with timing as I it did not seem to be enough benefit)

Answers 2

I am also interested in using the HPET, but as a timer. The way I understand it, QueryPerformanceCounter and QueryPerformanceFrequency are actually accessing the counter and clock for the HPET, and this works under Windows XP (see, for example, http://www.geisswerks.com/ryan/FAQS/timing.html).

So as far as timing code, I think by using QueryPerformanceCounter you are in fact getting access to the counter that form the base of HPET, and this is all present in the chipset (rather than the processor).

Read More

Monday, April 16, 2018

Ruby: How to Get Screen Resolution in Windows

Leave a Comment

I saw one post back a while ago, but the main answer was for Linux. Currently, what's the easiest way to get the screen resolution (width/height) using Ruby on Windows.

3 Answers

Answers 1

One easy way is to wrap system commands and execute them in Ruby:

@screen = `wmic desktopmonitor get screenheight, screenwidth` 

You can either display them or save its output in a file.

To actually parse it, I found a helper for Windows cmd.exe in this post:

for /f %%i in ('wmic desktopmonitor get screenheight^,screenwidth /value ^| find "="') do set "%%f" echo your screen is %screenwidth% * %screenheight% pixels 

This way you can easily get the values in variables and store them in your Ruby program.

I couldn't find a simple gem though to do this, as you have for Linux.

Answers 2

You might try this code as suggested on ruby-forum.com which uses the WIN32OLE library. This works for Windows exclusively, though.

require 'dl/import' require 'dl/struct'  SM_CXSCREEN   =   0 SM_CYSCREEN   =   1  user32 = DL.dlopen("user32")  get_system_metrics = user32['GetSystemMetrics', 'ILI'] x, tmp = get_system_metrics.call(SM_CXSCREEN,0) y, tmp = get_system_metrics.call(SM_CYSCREEN,0)  puts "#{x} x #{y}" 

Answers 3

I also suggest to go straight with system command wrapping. Tested on win7.

# also this way res_cmd =  %x[wmic desktopmonitor get screenheight, screenwidth] res = res_cmd.split p w = res[3].to_i p h = res[2].to_i  # or this way command = open("|wmic desktopmonitor get screenheight, screenwidth") res_cmd = command.read() res = res_cmd.split p w = res[3].to_i p h = res[2].to_i  # or making a method def screen_res     res_cmd =  %x[wmic desktopmonitor get screenheight, screenwidth]     res = res_cmd.split     return res[3].to_i, res[2].to_i end  w, h = screen_res  p w p h 
Read More

Sunday, April 8, 2018

Pipe dcraw output as input to cjpeg.exe

Leave a Comment

Here is the command i'm trying in windows powershell.

 .\dcraw-9.27-ms-64-bit.exe -c "C:\Downloads\RAW_CANON_10D.CRW" | cjpeg\cjpeg.exe temp.jpg 

But cjpeg isn't getting the input and show this message. enter image description here

1 Answers

Answers 1

You need to call cjpeg.exe with both input and output arguments.

cjpeg.exe <input-file> <output-file> 

You are calling cjpeg.exe with single argument generating the error.

cjpeg.exe temp.jpg 

Actually you are trying to feed the result of the first command as the input of the cjpeg command. If dcraw returns the path of the input file then you have to execute the following:

.\dcraw-9.27-ms-64-bit.exe -c "C:\Downloads\RAW_CANON_10D.CRW" | %{ cjpeg\cjpeg.exe $_ temp.jpg } 

If dcraw returns raw binary output of the image you need to save it to a temporary file.

.\dcraw-9.27-ms-64-bit.exe -c "C:\Downloads\RAW_CANON_10D.CRW" | Out-file temp.tiff | %{ cjpeg\cjpeg.exe temp.tiff temp.jpg } 
Read More

Monday, March 12, 2018

Install keras and tensorflow using Rstudio

Leave a Comment

While trying to following the instructions of installing Keras and TensorFlow on Rstudio link https://keras.rstudio.com/index.html I get the following error. It is a work computer that is running Windows 7. I am not familiar with python, but I believe I have python 3.6 installed correctly (I am able to run simple python code in the Spyder IDE). Thanks in advance for any suggestions on how to get this working.

> install_keras() Creating r-tensorflow conda environment for TensorFlow installation... Solving environment: ...working... failed  CondaHTTPError: HTTP 000 CONNECTION FAILED for url  <https://repo.continuum.io/pkgs/main/noarch/repodata.json.bz2>  Elapsed: -  An HTTP error occurred when trying to retrieve this URL. HTTP errors are often intermittent, and a simple retry will get you on your  way. ConnectionError(MaxRetryError("HTTPSConnectionPool(host='repo.continuum.io', port=443): Max retries exceeded with url: /pkgs/main/noarch/repodata.json.bz2 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000000000474D860>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))",),)   Error: Error 1 occurred creating conda environment r-tensorflow In addition: Warning message: running command '"C:\Users\...\...\Local\CONTIN~1\ANACON~1\Scripts\conda.exe" "create" "--yes" "--name" "r-tensorflow" "python=3.6"' had status 1  

1 Answers

Answers 1

Installing Keras and TensorFlow using install_keras() isn't required to use the Keras R package. You can do a custom installation of Keras (and desired backend) as described on the Keras website and the Keras R package will find and use that version.

Source

So you can circumvent this issue with the firewall using a custom installation. The R package keras will then find that installation automatically. See the linked source for more information on how to do a custom installation.

edit: btw, there is a similar question that has been answered here. That poster goes into changing the proxy settings to cicrumvent the firewall. I cannot mark this question as a duplicate due to active bounty.

Read More

Thursday, March 8, 2018

WinAPI: How to determine when a network is identifying?

Leave a Comment

When a connect some devices via ethernet I can see the adapter status in the Network Connections panel change to "Identifying Network". I want to query for this status using c++ Windows API.

I'm implementing somewhat of a dynamic ipconfig /all listing in my project. When a network on a network interface is identifying I'll want to display "Identifying Network".

I'm aware of the INetwork interface, but it annoyingly is only accessible through COM. Is there another way? Maybe from the information about the Network Interface itself?

1 Answers

Answers 1

How does INetwork know?

If you would search for the relevant property bag key L"NA_NetworkClass" in system files and you would have some reverse engineering skills and a bit of luck, you could find that the relevant implementation of the INetwork interface apparently lies in C:\Windows\System32\netprofmsvc.dll.

The code implementing IPropertyBag::Read() for NA_NetworkClass seems to determine the state by looking at some other GUID property of the network. Apparently, both the unidentified and identifying states are signaled by one of two hardcoded values there, while any other value means the connection is fully identified.

So far so good. If you could determine this GUID by some other means (e.g. through some "vanilla" Win32 API that you would be comfortable with) and compare it yourself, you should be able to determine the identification state as well. But here comes the catch: the exact GUID values for unidentified and identifying are dynamically generated at a different place in netprofmsvc.dll, so they will be different every time!

At this point you should stop trying. This is one of the techniques that can be deliberately used by OS authors to tell you that messing with system internals in this way is discouraged, error-prone and dangerous.

So just use the intended interface as everybody else. Come on, compared to some of the other horrible things out there, COM is not bad at all!

How was this done before Windows Vista?

The answer is it wasn't done at all.

The GUID I wrote about above actually happens to be a "network profile GUID", calculated from many network parameters like domain authentication state, gateway's MAC address etc., and later associated with one of the access profiles like Domain/Work/Home/Public.

All this got introduced only in Vista, together with the whole new "Network and Sharing Center" and other improvements in firewall and network management. See this document "Exploring The Windows Firewall" from 2007 and search for a chapter called "Network Profiles" for more information.

Are there any alternative solutions?

If you insist on avoiding INetwork, you might never be able to match the Windows network identification state 100%.

As explained in the article above, the Network Location Awareness (NLA) service apparently goes through a lot of work to reliably identify the network, considering many other parameters than just assigned IP addresses or enabled protocols. Replicating all that yourself (to determine when you know everything necessary to say a network is identified) would be challenging, especially because the exact details of the process are undocumented.

That said, checking for a valid received DHCP configuration (valid host and gateway IP addresses) might be a good enough approximation in most cases. Good luck!

Read More

Sunday, February 18, 2018

Pyinstaller distributing opencv from Windows 10 to Windows <10, missing ucrt dlls api-ms-win-crt

Leave a Comment

I have a Python 3.5 64 bit program (required by tensorflow for Windows) that uses OpenCV. I am distributing it with pyinstaller.

I built my program with Windows 10

/c/Python35/Scripts/pyinstaller -c DeepMeerkat.spec 

On my computer, the .exe builds and runs perfectly. On any other non-Windows 10 machine

On

import cv2 

Returns

ImportError: DLL load failed: The specified module could not be found.

I can see a huge amount of discussion on this on pyInstaller thread, but I can't quite tell how to put it into practice. Dependency walker says I'm missing a number of DLLs

api-ms-win-crt-**.dll 

Okay, from the pyInstaller threads, I know that these DLLs exist

C:\Program Files (x86)\Windows Kits\10\Redist\ucrt\DLLs 

Under multiple subfolders base on architecture.

I tried adding to my .spec file

pathex=["C:/Program Files (x86)/Windows Kits/10/Redist/ucrt/DLLs/"], 

Or for my particular architecture

pathex=["C:/Program Files (x86)/Windows Kits/10/Redist/ucrt/DLLs/arm"], 

Which is what I thought was being suggested here

"Install the Windows Software Development Kit (SDK) for Windows 10 and expand the .spec-file to include the required DLLs, see “Distributing Software that uses the Universal CRT“ in the above-mentioned link, number 6."

That did not have any effect. I am getting hundreds of errors like

121472 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\python35\DLLs\_ssl.pyd 

But I can see that DLL here

C:\Program Files (x86)\Windows Kits\10\Redist\ucrt\DLLs\arm 

enter image description here

So then I literally copied posthoc the entire folder

cp -r "C:/Program Files (x86)/Windows Kits/10/Redist/ucrt/DLLs/" dist/Lib/ 

But it's not clear how to connect these to the .exe. Clearly, letting pyInstaller know beforehand is preferable.

I also tried

/c/Python35/Scripts/pyinstaller --path "C:/Program Files (x86)/Windows Kits/10/Redist/ucrt/DLLs/arm" -c DeepMeerkat.spec 

And it still did not find them

I've also tried adding that folder to PATH. Any ideas?

3 Answers

Answers 1

I need to do this myself but haven't yet. I'll try to post my full solution when I do. In the mean time...

I think you may have to explicitly request they be included instead of just expanding the search path.

https://pythonhosted.org/PyInstaller/spec-files.html#adding-binary-files

Probably using the Tree class they mention to collect all the files for you.

https://pythonhosted.org/PyInstaller/advanced-topics.html#the-tree-class

Answers 2

If you would have provided your spec file I'd could see what's going on. From here its likely your not including files.

There is two methods to go from here:

  1. Create "one" single file that includes all dll's, pyd files and more... a large exe-file as result.
  2. The other way is to have it as file + folder filled with dll files, etc... you get a small exe-file.

Check add binary (incl. dll) files here the pyinstaller documentation about including files manually.

Check add data files here the pyinstaller documentation about including files manually.

An example spec-file that includes dll files from your dll folder.

block_cipher = None a = Analysis(['minimal.py'], pathex = ['/Developer/PItests/minimal'], binaries = [ ( 'C:\Program Files (x86)\Windows Kits\10\Redist\ucrt\DLLs', '.' ) ], datas = [ ('helpmod/help_data.txt', 'helpmod' ) ], hiddenimports = [], hookspath = None, runtime_hooks = None, excludes = None, cipher = block_cipher) pyz = PYZ(a.pure, a.zipped_data, cipher = block_cipher) exe = EXE(pyz,... ) coll = COLLECT(...)

Answers 3

I've seen PyInstaller and Py2exe fail to pickup dlls countless times. Personally, I wrap my uses of them in batch or bash for a number of reasons to extend what they do. Logically, I see an argument for wrapping them in py scripts themselves...

Anyway, it may be easier to just copy the dependencies into your installation package through a wrapper script rather then fight with this.

Typically, you'll get a dll missing error thrown at you when try to run something it's missing. Add each one manually to the directory, noting what you needed to include yourself. Then script that.

Read More

Thursday, February 8, 2018

All npm commands giving same error (ENOENT)

Leave a Comment

I'm using windows 8.1 x64 with all updates.

I was using nodejs 8.9.1 - I took an error on command "npm -v".
So I uninstalled this version and install 8.9.3. It gave me same error on the command.
And then I uninstalled 8.9.3 and clean all paths using this answer; https://stackoverflow.com/a/20711410/1143249 Then installed new version 9.3.0...

But still, I'm getting same error in all npm commands.
What am I missing? How can I solve this problem?

C:\Users\UserName\Desktop>npm -v Error: spawn C:\Program Files\nodejs\node.exe ENOENT     at _errnoException (util.js:999:13)     at Process.ChildProcess._handle.onexit (internal/child_process.js:201:19)     at onErrorNT (internal/child_process.js:389:16)     at process._tickCallback (internal/process/next_tick.js:152:19)     at Function.Module.runMain (module.js:703:11)     at startup (bootstrap_node.js:194:16)     at bootstrap_node.js:618:3 C:\Program Files\nodejs\node_modules\npm\lib\npm.js:51       throw new Error('npm.load() required')       ^  Error: npm.load() required     at Object.get (C:\Program Files\nodejs\node_modules\npm\lib\npm.js:51:13)     at process.errorHandler (C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:205:18)     at process.emit (events.js:159:13)     at process._fatalException (bootstrap_node.js:387:26) 5.5.1  events.js:136       throw er; // Unhandled 'error' event       ^  Error: spawn C:\Program Files\nodejs\node.exe ENOENT     at _errnoException (util.js:999:13)     at Process.ChildProcess._handle.onexit (internal/child_process.js:201:19)     at onErrorNT (internal/child_process.js:389:16)     at process._tickCallback (internal/process/next_tick.js:152:19)     at Function.Module.runMain (module.js:703:11)     at startup (bootstrap_node.js:194:16)     at bootstrap_node.js:618:3 

Update:
Everytime I uninstalled nodejs, clean/delete folders and tried these versions;
- node-v9.3.0-x64 not working
- node-v8.9.3-x64 not working
- node-v8.9.1-x64 not working
- node-v8.9.0-x64 not working
- node-v7.10.0-x64 shows version number also a small error.
Here is the output;

C:\Users\UserName\Desktop>npm -v 5.6.0  events.js:163       throw er; // Unhandled 'error' event       ^  Error: spawn C:\Program Files\nodejs\node.exe ENOENT     at exports._errnoException (util.js:1050:11)     at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)     at onErrorNT (internal/child_process.js:367:16)     at _combinedTickCallback (internal/process/next_tick.js:80:11)     at process._tickCallback (internal/process/next_tick.js:104:9)     at Module.runMain (module.js:607:11)     at run (bootstrap_node.js:427:7)     at startup (bootstrap_node.js:151:9)     at bootstrap_node.js:542:3 

Update:
I uninstalled Avg anti-virus protection and installed node-v6.12.3-x64.msi. Still I have problems, an example; I can't run commands like; Node v6.12.3 on Windows 8.1 x64

7 Answers

Answers 1

The workaround is to ensure that C:\Users\{user}\AppData\Roaming\npm exists and is writable with your normal user account.

see Troubleshooting

Answers 2

Although haven't tested Node on Windows 8, I suggest you get rid of npm installation under AppData folder altogether. You can achieve this by using a Node version manager. I recommend Nodist. You can easily switch between versions as well.

With Nodist you can run the command as such:

 nodist 6.2.10 

One of the things I suspect is Node runtime's limited access to required files under C:/ drive - maybe something related to Windows 8. That said, you could install nodist in any drive other than C:/. Meanwhile as mentioned by @fjoe, stick with LTS versions especially v6.x.x.

Answers 3

Here are a few things to consider when picking your node.js version.

Version labelled as LTS (Long Term Support) are the releases they stand behind.

You can see a full list of versions here notice the LTS column. It is also interesting to see the versions of V8 and npm that are packaged with each.

  • Node 9.* has no official LTS release.
  • Node 8.* officially entered LTS on 10-31-2017 (3 months ago).
    • I personally have run into problems with it.
  • Node 7.* never officially released as LTS.
  • Node 6.* has 2 years of official LTS releases and they are continuing to support it.
    • With the most recent release on 01-02-2018.

Given this information might I recommend you try the proven long supported version 6.*

If this works then maybe hold off on version 8.* until more things get ironed out.

Other people have commented on your question about environment variables for node or npm. This is obviously not the issue as the commands are recognized and running (but they error).

Also I would recommend you follow this full uninstall guide: How to completely remove node.js from Windows

Answers 4

My coding buddy had a similar error yesterday, although on Linux.

The error was in the node_modules folder, so removing that and reinstalling node solved it for him.

That would be your node_module folder in AppData\Roaming\npm.

Answers 5

I guess the problem can be in NPM itself. try to update your NPM.

Try to use this great approach

By the way, it's great to know what npm version you have with your changes of node.js versions.

Answers 6

From the logs you provided it looks like a cache bug, because of some junk that previously exists and that is causing issues.

Open the command prompt with admin privileges and follow the below steps:

1) Clear the cache: npm cache clean --force. You can also run npm cache verify.

2) Delete node_modules folder using: rm -rf node_modules.

3) Delete any package-lock.json file.

4) Remove the .npm directory.

5) Remove following directories :

   5.1) C:\Users\<username>\AppData\Roaming\npm\.      5.2) C:\Users\<username>\AppData\Roaming\npm-cache. 

6) Also, Try by setting environment variables:

C:\Users\<username>\AppData\Roaming\npm;C:\Program Files\nodejs.

Type in the command line: which npm.

Maybe you did not set the system value : NODE_PATH it should point to your global module location.

7) Update to the latest npm with npm i -g npm@latest.

Hope the above trick resolve your issue.

Thanks.

Answers 7

I guess while installing Node.js it don't able to set it's environment path due to permission.

You Need to configure Node path manually into environment variables.

Read More

Saturday, February 3, 2018

Any way to block/remove keyboard hook in uncontrolled window's Menubar?

Leave a Comment

I'm sending keystrokes to an inactive Adobe Flash Projector window with PostMessage, that part works perfectly. I leave it running in the background and it interferes very little with my normal computer usage, which is the intent. The problem comes when I programmatically send the W (or less frequently Q) key while I happen to be holding Ctrl intended for a different windows shortcut. This triggers Ctrl-Q or Ctrl-W, both of which immediately close the Adobe Flash Projector. Ctrl-F and Ctrl-O have some undesirable effects as well.

EDIT: I'm not interested in solutions that briefly release the Ctrl key.

Is there anyway I can unhook shortcut keys from a third party window? It uses a standard OS menubar across the top of the window which is where the shortcuts are listed, so surely there's a way to reassign, unassign, or block it, right?

In the past I tried using these dlls to break the menu. It made it disappear but didn't break the shortcuts.

DllCall("SetMenu", uint, this.id, uint, 0) hMenu := DllCall("GetSystemMenu", "UInt",this.id, "UInt",1) DllCall("DestroyMenu", "Uint",hMenu) 

Sorry for the strange syntax, this is from an early version of my program written in autohotkey.

The language I'm using now is C#, but I assume the solution uses a .dll, so that's not as important. Feel free to suggest or change my tags.

1 Answers

Answers 1

  1. You can try to make inactive (WS_DISABLED - use GetWindowStyle and SetWindowStyle) the main window of destination application (the Window that contains menu).

  2. You can try to find which functions are used by application and rewrite them in local copy of application with VirtualProtect and injecting assembler (dangerous if you have no knowledge about virtualization of memory). Check the application use GetKeyState or GetAsyncKeyState (it will be visible after opening the application in text editor).

  3. You can try: HMENU hMenu=GetMenu(applicationMainWindow); SetMenu(applicationMainWindow,0); // here send your input with SendMessageW instead of PostMessageW SetMenu(applicationMainWindow,hMenu);

Each program can use various methods to handle user keyboard input. In this case probably is used GetAsyncKeyState or GetKeyState (for Ctrl) if you didn't send it and Ctrl is detected.

If it won't help you, please add code with your PostMessage to your question.

BTW. Instead of destroying GetSystemMenu you can clear appropriate window style and after sending input restore it (if the problem is System Menu - probability near 1%).

Read More

Tuesday, December 19, 2017

PyGTK Window always on top of all 'always on top` windows

Leave a Comment

I created a PyGTK application which needs to be always on top. The transparent window redirects mouse clicks to the window below and therefore gets out of focus once you click into an underlaying window. This is the intended behaviour. In fact it should just show a little image that's always above everything.

This works well unless you use something like the libreoffice fullscreen presentation. The presentation window is marked as always on top as well and hence my window goes into the background. Even if I take it to front using Alt+Tab and then click onto the underlaying libreoffice slide (which is neccessary) my window goes into the background again.

This is a really specific question and the solution should work on both Linux and Windows. I have no idea what I should do. I just found many questions on how to have a windows always on top but none of them covering my problem.

Long story short: How can I put my window always on top of every window that is always on top as well?

Any help is appreciated.

1 Answers

Answers 1

Simple Answer: You can use 'Always on Top' in windows with by using AutoHotKey scripts.

Follow these steps:

  • Install AutoHotKey
  • Create a new AHK scripts following the images
  • Add this code ^SPACE:: Winset, Alwaysontop, , A
  • Next, double-click your script to run it. You’ll know it’s running because a green “H” logo appears in your system tray to let you know it’s running as a background process.
  • Now press Ctrl+Space to set any currently active window to be always on top. Press Ctrl+Space again set the window to no longer be always on top.

enter image description here

enter image description here

enter image description here

Read More

Wednesday, November 29, 2017

Relinquish rights on Windows

Leave a Comment

I have a C++ application that runs as administrator (it is compiled with a manifest with requestedExecutionLevel set to requireAdministrator.

At some point, once all the tasks requiring administration rights are done, I would like to relinquish those rights and perform the remaining tasks as the user that launched the application.

Windows provides the ImpersonateLoggedOnUser function, but I can't find any way to obtain a token for the user that called the application.

Are there any other ways to do what I have described here?

2 Answers

Answers 1

Perhaps a better approach would be to request highestAvailable instead of requireAdministrator in your manifest. Then, if you find that you are running elevated, just do everything you need to do. If you find that you are not running elevated:

  1. Launch your program again, using ShellExecute with the runAs verb to run it elevated.
  2. Have your unelevated process wait for the elevated process to do whatever it needs to do. (How to know when the elevated process is done is left as an exercise for the implementer. You also need to worry about what happens if the user does not allow your elevated process to start.)
  3. Once the elevated process completes, do the rest of your unelevated work.

If you want to continue with your original plan, this Raymond Chen blog post explains how to start an unelevated process from an elevated process. (The fact that your manifest requests requireAdministrator may complicate this process.)

Answers 2

msdn:

Enabling a privilege in an access token allows the process to perform system-level actions that it could not previously. Your application should thoroughly verify that the privilege is appropriate to the type of account.

You can check: https://msdn.microsoft.com/en-us/library/windows/desktop/aa446619(v=vs.85).aspx

And here is their c++ sample:

#include <windows.h> #include <stdio.h> #pragma comment(lib, "cmcfg32.lib")  BOOL SetPrivilege(     HANDLE hToken,          // access token handle     LPCTSTR lpszPrivilege,  // name of privilege to enable/disable     BOOL bEnablePrivilege   // to enable or disable privilege     )  {     TOKEN_PRIVILEGES tp;     LUID luid;      if ( !LookupPrivilegeValue(              NULL,            // lookup privilege on local system             lpszPrivilege,   // privilege to lookup              &luid ) )        // receives LUID of privilege     {         printf("LookupPrivilegeValue error: %u\n", GetLastError() );          return FALSE;      }      tp.PrivilegeCount = 1;     tp.Privileges[0].Luid = luid;     if (bEnablePrivilege)         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;     else         tp.Privileges[0].Attributes = 0;      // Enable the privilege or disable all privileges.      if ( !AdjustTokenPrivileges(            hToken,             FALSE,             &tp,             sizeof(TOKEN_PRIVILEGES),             (PTOKEN_PRIVILEGES) NULL,             (PDWORD) NULL) )     {            printf("AdjustTokenPrivileges error: %u\n", GetLastError() );            return FALSE;      }       if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)      {           printf("The token does not have the specified privilege. \n");           return FALSE;     }       return TRUE; } 

And also look at: https://msdn.microsoft.com/en-us/library/windows/desktop/ms717797(v=vs.85).aspx

Hope it helps.

Read More