Category Archives: Windows

All ip assigned within a network

Many a time you wanted to know the assigned ip addresses in your LAN. My use cases:

I was working on nano pi neo cheapest things can be!!

You don’t want to move from your¬†ass but this is more of a reason when you already know how to do this ūüôā

Okay so on windows you have Advanced ip scanner. You can even log into your router and get to see the client list this is always the easiest way but (ohh but again).. ¬†¬†what if you have reset your router password. Most of the time you will forgot it!! It is not often that you need to login into your router so reset password means forgot. Do you want to reset the router password now just for this with router’s reset button. Or you are in office or you don’t own the router.

Anyway whatever your cause is here is a very easy command to get the list of all assigned ip using linux system:


sudo apt-get install nmap


:~$ sudo nmap 192.168.100.5/24 -sP

Starting Nmap 6.40 ( http://nmap.org ) at 2017-01-04 18:23 IST
Nmap scan report for domain.name.dlink.com (192.168.100.1)
Host is up (0.00024s latency).
MAC Address: 1C:5F:2B:4D:05:71 (Unknown)
Nmap scan report for 192.168.100.26
Host is up (0.00028s latency).
MAC Address: FA:7B:DE:ED:46:55 (Unknown)
Nmap scan report for 192.168.100.4
Host is up.
Nmap done: 256 IP addresses (3 hosts up) scanned in 3.27 seconds

If there are not much of machines in your LAN you will figure out which machine has what ip or you can ssh to each to verify. Without nmpa also you can ping all the ip and get the list but nmpa also tells you the friendly name of the machine.
 

 

Advertisements

Qt: Sending a window on top from another application

The problem statement was very simple. I had two applications in Qt say A and B both were communicating with Qt shared memory(very useful feature and easy to use also). Okay so two applications in Qt and what I wanted to do was on some trigger from application A, I want to send a window created in application B on top.

In other words:

Let us say you have two qt application on click of a button on one application window you want to bring the other application window on top. You can use any method of IPC between these two applications.

Solution:

Isn’t this a very simple problem? There is a catch
In windows above is not possible directly with qt. You can bang your head with all feature of qt to send a window on top. Give it focus make it active but to no avail. The window will remain on back.

You need to have a hack around to make this possible.

One peculiar observation was on the first try window was not coming on front but in next all try it comes in the front. So you can open close the window fast for the first time and going forward you have resolved the problem.

There are some other workaround mentioned on qt forum but it didn’t worked for me. I tried with registry setting but didn’t wanted to restart the system so not sure whether it works or not.

activate qt window


// HACK: bringing window to top
// from QT documentation:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// void QWidget::activateWindow ()
// ...
// On Windows, if you are calling this when the application is not currently
// the active one then it will not make it the active window. It will change
// the color of the taskbar entry to indicate that the window has changed in
// some way. This is because Microsoft do not allow an application to
// interrupt what the user is currently doing in another application.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

My hack was very simple by using windows API to bring a window on top

 

SetWindowPos((HWND)viewer->winId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

 

Giving it focus is still not working for me for my particular application so I have to try the first hack also opening and closing the window fast. You can make this a very small window and put it in some weird position so no one can notice. Also this is needed only once so you can do it when your second application starts(whose window you want to bring in front from another app trigger).

 

Changes that affected me in Win 10 Anniversary edition

C# Fullscreen app problem

Some .net Full Screen apps are no longer fullscreen:  fullscreen app(.net apps mostly) which are always in front and top of taskbar were not actually in front anymore in win 10 anniversary edition. In new edition microsoft has made some changes in way taskbar behaves I think this is because they have put notification. I see the problem is faced by many games also. Anyhow the fix was very simple earlier I had

<Window
x:Class="YourClass"
xmlns:Menus=""
Icon="yourIcon.ico"

Title="Anniversary Edition"
WindowState="Maximized"


WindowStyle="None"
ShowInTaskbar="True">

 

 

Which now changed to

 


<Window
x:Class="YourClass"
xmlns:Menus=""
Icon="yourIcon.ico"

Title="Anniversary Edition"
WindowState="Maximized"


WindowStyle="None"
Topmost="True"
ShowInTaskbar="True">

So You need to modify your application and add this extra element
 
Topmost=”True”

Touch Related changes

Changes in kernel touch: Seems like with new edition some more¬†stringent check implemented in the way kernel handles touch inputs from hardware/virtual driver. For me the problem was related to drivers “inrange” and “confidence” bit.

Another change related to Touch was the way windows handles out of screen points. Earlier these touch inputs are rejected(no effect on any clicks which happens at incorrect coordinate). But now this is not true. Even if you fire an input at (-10,-10) it will go on screen at (0,0). I don’t know what is the need for this correction from microsoft side. Anyhow now you can fire at any coordinate and it will automatically be typecasted with screen size and will fall on edges or corner of the screen.

Windows Programming Multi language

Now a days I am doing some windows application development and I am loving it :).

I was not such a big fan of windows ever. I can understand the reasons for most of the issues which make windows not so great. Like they have to support so many verities of hardware so many versions and so many applications and all.

Anyhow I noticed that windows has very user-friendly development environment. There is less open source projects and less help for obvious reasons. This is kind of bottleneck but when I learned about dllimport I was like… ahhh great now I can do whatever I want. Developing GUI and simple stuff in C# and then using dllexport for existing libraries by just writing a wrapper file around the library. From there onward I have used it so much in all kind of development.

To explain usefulness of  dllexport I will use an example where I have to do some Video encoding and streaming. You already have great open source project for that. I want to integrate this with my C# application. For X264 encoding for sending data from network programming in C++. Used libavcodec libavformat to convert between formats muxing video data.

C/C++ Part

I will explain with one simple example how to use dllexport to create cool windows project here.

Let us first take a simple example, say I want to use a C function defined below:

int func(int arg)
{
  int result = 0;
//some kind of processing
  //may call other defined C functions
  return result;
}

Say above function call along with all useful stuff is in some file example.c. It may be in multiple files also. We just want to use the func call in C#. We will redefine the function as below:

__declspec(dllexport) int  __cdecl func(int arg)
{
//..
//same stuff
 

To be user-friendly we can define two macros as below:

#define DLLEXPORT __declspec(dllexport)
#define CDECL __cdecl

Now our function will look pretty good:

DLLEXPORT int  CDECL func(int arg)
{
  //..
  //same stuff

by //same stuff I mean the function body of func
Now we compile all c code as we were doing earlier but this time we compile it to create a dll library. GCC provides command to do so:
First compile all the files including example.c file and get the object files

gcc -c -o example.o example.c

gcc <strong>-shared</strong> -o library.dll example.o other_files.o other_libraries.a
 

other_files.o other_libraries.a are optional only required if your C project is big and uses multiple files and libraries. We will see it in next example when using X264 for encoding from C# project.

C# Part

We are almost done Now we just need to write our C# code and wherever in C# we want to use the function(func) from example.c we first declare the function as below:

[DllImport("library.dll")]
static extern int func(int arg);

Now we are free to use this function in our C# code just like any other function.
func(3);
That’s all so simple.
Now let us check one example where we will use libx264.¬†We can do the same for ffmpeg by creating the ffmpeg dll.¬†Sometime when there is problem of passing one struct variable from one C function to another C function. Say you want to use ffmpeg from one side while you also want to use X264. Since in C# we can’t just define these struct we will use IntPtr whenever there is any such requirement. This generally comes very handy in some cases.
I guess I will do another post for this as this post is already long.
Scratch Pad:

gcc -shared -o libmpegts.dll main.o libmpegts.a
gcc -I. -c -o tsmuxer.o tsmuxer.c

gcc -shared -o tsmuxer.dll tsmuxer.o -L. -lavformat -lavcodec -lavutil -lWs2
_32 -liconv

Scratchpad:

[DllImport("Kernel32.dll")]
static extern Boolean Beep(UInt32 frequency, UInt32 duration);

[DllImport("libx264", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr initializePicOut();

DLLEXPORT x264_picture_t* CDECL initializePicOut()
{
}

DLLEXPORT x264_t* CDECL setX264Params(int width, int height, int FPS)
{
printf("setX264Params width: %d, height: %%d FPS: %d.\n", width, height, FPS);
x264_param_t param;
int res = 0;
res = x264_param_default_preset(¶m, "veryfast", "zerolatency");
if(res != 0) {
printf("error: cannot set the default pre-set on x264.\n");
return -1;
}
param.i_threads = 1;
param.i_width = width;
param.i_height = height;
param.i_fps_num = FPS;
param.i_fps_den = 1;
// Intra refres:
param.i_keyint_max = FPS;
param.b_intra_refresh = 1;
//Rate control:
param.rc.i_rc_method = X264_RC_CRF;
param.rc.f_rf_constant = FPS-5;
param.rc.f_rf_constant_max = FPS + 5;
//For streaming:
param.b_repeat_headers = 1;
param.b_annexb = 1;
res = x264_param_apply_profile(¶m, "baseline");
if(res != 0) {
printf("error: cannot set the baseline profile on x264.\n");
return -2;
}
}