I'm trying to embed video in desktop application using Linphone C API.
By default, if to use samples from tutorial during call video window appears in separate window.
Could you please tell me, how to insert this video in my desktop application?
All I found seems helpful in Linphone C API reference, function:
linphone_core_set_native_video_window_id
Description:
Set the native video window id where the video is to be displayed.
For MacOS, Linux, Windows: if not set or zero the core will create
its own window, unless the special id -1 is given.
Could it be helpful? How to use it?
Here is the function from linphone SDK to get window handle:
void *get_native_handle(GdkWindow *gdkw){
#ifdef GDK_WINDOWING_X11
return (void *)GDK_WINDOW_XID(gdkw);
#elif defined(WIN32)
return (void *)GDK_WINDOW_HWND(gdkw);
#elif defined(__APPLE__)
return (void *)gdk_quartz_window_get_nsview(gdkw);
#endif
g_warning("No way to get the native handle from gdk window");
return 0;
}
Here is few lines of code:
GtkWidget *video_window, *window;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
video_window = gtk_drawing_area_new();
unsigned long *videoID;
videoID = malloc(sizeof(unsigned long));
*videoID = get_native_handle(gtk_widget_get_window(video_window));
linphone_core_set_native_video_window_id(lc, *videoID);
Worked for me!
Related
I am working on a project in c which i am trying to simulate chrome browsing.
my only difficult is to find a way to get links position on screen after i receive a handle to chrome window. anyone knows how to get the links position??
i am familiar with selenium and managed to do so in python but i require to do so in c too.
any ideas?
int wmain()
{
HWND hwnd = NULL;
for (;;)
{
hwnd = FindWindowEx(0, hwnd, L"Chrome_WidgetWin_1", 0);
if (!hwnd)
break;
////Get Links position
}
I trying to display video in GTK# using Gstreamer via P/Invoke(on Ubuntu). I tried to use many code samples but nothing is working. Here is one of them:
GTK# code:
[DllImport("libgstTestDLL.so", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public extern static int play_file (StringBuilder path, IntPtr win);
...
play_file (new StringBuilder ().Append ("file:///home/user/Downloads/test.mp4"), screen.GdkWindow.Handle);
C code:
void play_file(char* path, void* hwnd_ptr){
GdkWindow* gdkWin = (GdkWindow*)hwnd_ptr;
pipeline = gst_element_factory_make("playbin", "player");
g_object_set (G_OBJECT (pipeline), "uri", path, NULL);
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(pipeline), GDK_WINDOW_XID(gdkWin));
gst_element_set_state(pipeline, GST_STATE_PLAYING);
}
After executing play_file function my GTK# app just closes.
How can I correctly use play_file in GTK# and what I need to execute from play_file function in C to display video in GTK# application?
I found solution.
I linked my shared library with libgstvideo-1.0.so using this:
target_link_libraries(${PROJECT_NAME} gstvideo-1.0). Full description of the solution available here.
I want to write an application that will automatically detect and fill the text field in the window shown below:
(assuming the data to be entered is in a file).
The question is how does my application find this text field?
I can do this job if I am able to find the location of the text field on the desktop through program.
Can someone help me understand possible ways for finding this text field?
I am using Windows Form application in C++.
Update:
I played with spy++.
I used spy++, to find the window handle. I did it by putting finder on the window I am interested in. Its giving handle in hex values: 00080086 (actually just for testing purpose I put the finder tool on Visual Studio new project page ). How do I interpret this Hex value into meaningful window name ?
See the below figure.
What is the next step to get to the text field " Enter name" under "name" field.
****Any sample code will be highly appreciated.**
I am open to any solution not necessarily how I am doing this.
One solution is to use the Microsoft UI Automation technology. It's shipped out-of-the-box with Windows since Vista. It's usable from .NET but also from C++ using COM.
Here is a short C++ console application example that displays the class name of the UI Automation Element currently at the middle of the desktop window, each second (you can have it run and see what it displays):
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
IUIAutomation *pAutomation; // requires Uiautomation.h
HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation), NULL, CLSCTX_INPROC_SERVER, __uuidof(IUIAutomation), (LPVOID *)&pAutomation);
if (SUCCEEDED(hr))
{
RECT rc;
GetWindowRect(GetDesktopWindow(), &rc);
POINT center;
center.x = (rc.right - rc.left) / 2;
center.y = (rc.bottom - rc.top) / 2;
printf("center x:%i y:%i'\n", center.x, center.y);
do
{
IUIAutomationElement *pElement;
hr = pAutomation->ElementFromPoint(center, &pElement);
if (SUCCEEDED(hr))
{
BSTR str;
hr = pElement->get_CurrentClassName(&str);
if (SUCCEEDED(hr))
{
printf("element name:'%S'\n", str);
::SysFreeString(str);
}
pElement->Release();
}
Sleep(1000);
}
while(TRUE);
pAutomation->Release();
}
CoUninitialize();
return 0;
}
From this sample, what you can do is launch the application you want to automate and see if the sample detects it (it should).
You could also use the UISpy tool to display the full tree of what can be automated in your target app. You should see the windows and other elements (text field) of this target app and you should see the element displayed by the console application example.
From the pElement discovered in the sample, you can call FindFirst with the proper condition (class name, name, control type, etc...) to get to the text field. From this text field, you would use one of the UI Automation Patterns that should be available (probably TextPattern or ValuePattern) to get or set the text itself.
The cool thing is you can use the UISpy tool to check all this is possible before actually coding it.
You could enumerate windows and then find it.
For exploring application on your screenshot you could you Spy++ (spyxx.exe) that is distributed with visual studio. In you code, you clould use EnumWindows and EnumChildWindows to enumerates all window or all child windows to find one you need.
Although the answer given by Simon is accepted and is the best one, but still for future visitors I am providing this link which has more description for UI automation of windows applications. .
Also for automating a web application one may want to go to this link
I have been struggling with NPAPI for "good" examples and "specific" documentations (on Unix). I have my libflashplayer.so, on which I can have access to certain information which is:
nm -D libflashplayer.so | more
00000000005cc4e0 T FlashPlayer_11_2_202_235_FlashPlayer
00000000005cc4a0 T Flash_DisableLocalSecurity
00000000005cc4c0 T Flash_EnforceLocalSecurity
00000000005ca870 T NP_GetMIMEDescription
00000000005ca850 T NP_GetValue
00000000005ca4d0 T NP_Initialize
00000000005ca4c0 T NP_Shutdown
I easily checked the MIMEDescription by doing this. Which return the flash MIME.
static char* (*mNP_GetMIMEDescription)(void);
//MAIN
lib = dlopen("./libflashplayer.so", RTLD_LAZY | RTLD_LOCAL);
*(void **) &mNP_GetMIMEDescription = dlsym(lib, "NP_GetMIMEDescription");
fprintf(stderr, "NP_GetMIMEDescription=%p\n", mNP_GetMIMEDescription);
fprintf(stderr, "NP_GetMIMEDescription=%s\n", mNP_GetMIMEDescription());
//output
NP_GetMIMEDescription=0x7f2d2239f870
NP_GetMIMEDescription=application/x-shockwave-flash:swf:Shockwave Flash;application/futuresplash:spl:FutureSplash Player
Now, I'm really lost on how to render (or test, open a swf file) content on a Xlib Window.
First, I do NP_INITIALIZE which I believe (from what I understand) provides Global Initialization (especially for NPNetscapeFuncs and NPPluginFuncs "instance"). Then the fun part begins, My guess (with what I read and understand, correct me if I am wrong) was to create an instance of my plugin (with NPP_New) and after, set my window (with NPP_SetWindow) to link my plugin instance and my window (from Xlib for example...)
This is exacly what I want to achieve.
http://blog.henryfan.net/post/2012/01/09/gtk%E6%92%AD%E6%94%BEflash.aspx
thanks to whoever wrote that down.
I'm actually trying to read a specific pixel on a window which is hidden by others. I want to use the GetPixel function from GDI library but it seems it only works with the global device context. I can't read pixel from a specific window and I don't understand why..
I found this article which uses the PrintWindow function to copy a specific window content to a temporary device context which can be read. But I can't reproduce it.
EDIT
Thank you all my problem is solved :)
This script give you the RGB color of the pointer on the choosen window, even though the window is hidden. Remind that this program must be launch with admin privileges to get the pixels of processes launched with admin privileges.
#define STRICT
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
// 0x0501 for PrintWindow function
// You must be at least running Windows XP
// See http://msdn.microsoft.com/en-us/library/6sehtctf.aspx
#include <stdio.h>
#include <string.h>
#include <windows.h>
#define WINDOW_LIST_LIMIT 32
#define WINDOW_NAME_LIMIT 1024
void FatalError(char* error)
{
printf("%s", error);
exit(-1);
}
HWND window_list[WINDOW_LIST_LIMIT];
unsigned int window_list_index = 0;
BOOL EnumWindowsProc(HWND window_handle, LPARAM param)
{
char window_title[WINDOW_NAME_LIMIT];
if(!IsWindowVisible(window_handle)) return TRUE;
RECT rectangle = {0};
GetWindowRect(window_handle, &rectangle);
if (IsRectEmpty(&rectangle)) return TRUE;
GetWindowText(window_handle, window_title, sizeof(window_title));
if(strlen(window_title) == 0) return TRUE;
if(!strcmp(window_title, "Program Manager")) return TRUE;
window_list[window_list_index] = window_handle;
window_list_index++;
printf("%u - %s\n", window_list_index, window_title);
if(window_list_index == WINDOW_LIST_LIMIT) return FALSE;
return TRUE;
}
int main(int argc, char** argv)
{
unsigned int i, input;
EnumWindows((WNDENUMPROC) EnumWindowsProc, (LPARAM) NULL);
printf("\nChoose a window: ");
scanf("%u", &input);
printf("\n");
if(input > window_list_index) FatalError("Bad choice..\n");
HDC window_dc = GetWindowDC(window_list[input - 1]), global_dc = GetDC(0), temp_dc;
if(!window_dc && !global_dc) FatalError("Fatal Error - Cannot get device context.\n");
POINT cursor, previous_cursor;
while(1)
{
temp_dc = CreateCompatibleDC(window_dc);
if(!temp_dc) FatalError("Fatal Error - Cannot create compatible device context.\n");
RECT window_rectangle;
GetWindowRect(window_list[input - 1], &window_rectangle);
HBITMAP bitmap = CreateCompatibleBitmap(window_dc,
window_rectangle.right - window_rectangle.left,
window_rectangle.bottom - window_rectangle.top);
if (bitmap)
{
SelectObject(temp_dc, bitmap);
PrintWindow(window_list[input - 1], temp_dc, 0);
DeleteObject(bitmap);
}
GetCursorPos(&cursor);
if(cursor.x != previous_cursor.x && cursor.y != previous_cursor.y)
{
COLORREF color = GetPixel(temp_dc, cursor.x - window_rectangle.left, cursor.y - window_rectangle.top);
int red = GetRValue(color);
int green = GetGValue(color);
int blue = GetBValue(color);
printf("\rRGB %02X%02X%02X", red, green, blue);
cursor = previous_cursor;
}
DeleteDC(temp_dc);
Sleep(50); // for lags
}
ReleaseDC(window_list[input - 1], window_dc);
return 0;
}
I've changed some things, now User32 isn't dynamically loaded.
It compiles with
gcc main.c -o main.exe -lGid32 -lUser32
Have a great day !
You are passing a process handle to GetDC. That's not right. Processes don't have device contexts, windows do. Remember a process can have many windows, or even none at all.
You need to get hold of the window handle, the HWND, for the window in question, and pass that to GetDC. I'd look to using FindWindow or EnumWindows to find your target top-level window.
Of course, there may be other problems with your code, but that's the one that jumps out at me.
HDC process_dc = GetDC(process_handle)
Well that's all kinds of wrong. GetDC accepts a window handle, not a process handle.
In order to find such errors, recompile with
#define STRICT
placed before your includes.
This is a bit of a confusing topic, so let's see if I can clarify a few things.
First things first: as both David and Ben have already answered, you're passing a process handle to the GetDC function, which is wrong. GetDC accepts a handle to a window (the HWND type), and it returns a device context (DC, the HDC type) corresponding to that window. You need to get that fixed before anything else will work.
Now, as the article you've read indicates, windows (assuming they've been correctly programmed) respond to the WM_PRINT or WM_PRINTCLIENT messages by rendering an image of themselves into the specified device context (HDC). This is a simple and effective way of capturing an "image" of a window, whether an overlapping window or the window of an individual control.
The rub comes in, as Hans mentioned in a comment, because handles to device contexts have process affinity, which means that the HDC you pass to the window in a separate process, into which it is supposed to render itself, will not be valid from that other process. Handles to device contexts cannot be passed across process boundaries. That's the primary reason that your code fails (or is going to fail, once you fix the handle type problems). The MSDN entry on GDI Objects makes this explicitly clear:
Handles to GDI objects are private to a process. That is, only the process that created the GDI object can use the object handle.
Fixing or getting around that is going to be a bit of an uphill battle. The only solution that I know of is to inject code into the other application's process that first creates a DC in memory, then sends the WM_PRINT or WM_PRINTCLIENT message to a window owned by that process to draw into that in-memory device context, and then transfers the resulted bitmap back to your own application. This is going to require that you implement some type of inter-process communication mechanism.
I've seen some anecdotal evidence that passing device context handles between processes via the WM_PRINT and WM_PRINTCLIENT messages "works", but it's unclear to me whether this is an artifact of the current implementation (and therefore subject to breaking in future versions of Windows), or if this is because Windows is actually handling the marshaling between processes. I haven't seen any documentation one way or the other. If this is a one-off project you're doing for fun or for a limited use, you might try it and get away with it. For other purposes, you probably want to investigate using IPC to really do this the right way.
Don't use GetDC for the DC to pass to PrintWindow. You need to create a compatible DC as you're doing (though you can pass it NULL to get a generic screen DC), then create a compatible bitmap the size of the window you're trying to capture and select it into the DC. Then pass that DC handle to PrintWindow.
Windows aren't required to respond properly to WM_PRINT or WM_PRINTCLIENT, so there may be some glitches even when you get this to work.