* * *

Author Topic: [SOLVED] a simple program for WinCE with API calls  (Read 1408 times)

doni1

  • Newbie
  • Posts: 4
[SOLVED] a simple program for WinCE with API calls
« on: February 11, 2018, 09:53:00 pm »
Hello, I have a problem with a simple program who calls some API functions on a WinCE 6.0 device/emulator. When the code is called in a Graphical Free Pascal Application, let's say in a button click or a timer event, everything is fine.
Can you help me to make this program work without forms/GUI?
Thank you, best regards!

this is the code (it just changes the z-order of some windows):
-declarations of functions:
function FindWindow(lpClassName:widestring; lpWindowName:widestring): thandle; stdcall; external 'coredll.dll' name 'FindWindowW';
function SetWindowPos(hWnd:integer; hWnd2:integer; x:integer; y:integer; cx:integer; cy: integer; wFlag:integer): thandle; stdcall; external 'coredll.dll';
-use of them:
hwnd:=FindWindow('49154','');
hwnd2:=FindWindow('SystemInformation.Class','');
SetWindowPos(hwnd2, hwnd, 0, 0, 0, 0, 19);
« Last Edit: February 13, 2018, 09:35:31 pm by doni1 »

jamie

  • Hero Member
  • *****
  • Posts: 785
Re: a simple program for WinCE with API calls
« Reply #1 on: February 11, 2018, 10:06:15 pm »
make a console app instead.

do what you need like you are now.
you will need to include the proper Units like windows etc.
Program Name
Uses ....

Begin
 your code
end;

 And setup your scheduler in the WinCe device to call it periodically.
I suppose you can keep this program running if you wish but it would involve using a timer or sorts

 Maybe using a Sleep(X-number of MSecs); can work in your loop.



doni1

  • Newbie
  • Posts: 4
Re: a simple program for WinCE with API calls
« Reply #2 on: February 11, 2018, 10:16:28 pm »
Thank you for your reply.
No, I don't need the timer, I used it just for testing, the same as the button. It is one-time job.
Do you know exactly what do I have to include for the app to work?
Everything I tried was compiled without errors, but the exe didn't run on the device/emulator, no errors, no other messages, but just did nothing.

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: a simple program for WinCE with API calls
« Reply #3 on: February 11, 2018, 11:34:26 pm »
Everything I tried was compiled without errors, but the exe didn't run on the device/emulator, no errors, no other messages, but just did nothing.
First, you haven't shown any form of output in presented code-lines.... so what would you expect it to output ?

Secondly, see here:
Quote
WinCE does not have support for console applications by default. But you can install console support by yourself. Please note that FPC creates GUI applications for WinCE target by default. To create console application you should use -WC compiler switch or put {$APPTYPE CONSOLE} directive to source code.
To enable console in WinCE install one of the following programs:
  • PocketCMD by SymbolicTools. It is recommended solution.
     Get it here
    Seems to be ARM/PPC-only
  • PPC Command Shell from Microsoft Windows Mobile Developer Power Toys. Get it here
    PPC Command Shell have less features than PocketCMD. Also it have some issues. One of them - a new console window is opened even if an application is started from a console command prompt.
  • To enable console output in Windows Mobile 5 and 6 set "HKEY_LOCAL_MACHINE\Drivers\Console\OutputTo" to 0.

doni1

  • Newbie
  • Posts: 4
Re: a simple program for WinCE with API calls
« Reply #4 on: February 12, 2018, 06:04:42 pm »
First, you haven't shown any form of output in presented code-lines.... so what would you expect it to output ?

Thank you for your kind reply. Molly, I tried to follow your instructions, but without success, those links (PocketCMD and PPC Command) are not valid anymore. Anyway, I don't need any output from my program, just to change the z-order of those windows. And, as I said, with no effect, the code lines works only from a full loaded form...  :(

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: a simple program for WinCE with API calls
« Reply #5 on: February 13, 2018, 02:02:35 am »
There are two approaches possible:

1) create a console application (as suggested by jamie and to which i replied with additional information). In that case you would require it to be invoked from the command prompt. In order to be able to do that your wince environment has to have a command prompt to begin with. How to obtain one is beyond the scope of these forums, so i can't help you with that. In case the offered links in the wiki-page don't work anymore you would have to find one on your own.

You won't see any output until the moment you have a writeln in there that actually shows some output.

2).create a gui application, but do not use the normal application startup. Instead just invoke the commands as you showed (create gui application with lazarus, then edit your .lpr file). The only output you will be able to retrieve then is either from debug output or when you show a dialog and/or messagebox of some sorts. Another possibility to check if your program was executed correctly is to write some data into a file or something similar.


It goes without saying that the code-lines you showed will only have effect when the window actually exist. That is why jamie suggested to use some sorts of a loop, and using sleep in order to let your program run until the windows are actually found (and for which you can hen change the z-ordering).

If no window is found then the code-lines you showed will be executed but no z-ordering will be changed (how could it without being able to locate the window for which you wish to change the z-order), and your program will simply end..

doni1

  • Newbie
  • Posts: 4
Re: a simple program for WinCE with API calls
« Reply #6 on: February 13, 2018, 09:34:08 pm »
Hey, Thank You, it works!!!  :)  :)  :)
Best idea to put some infos in a file, thanks molly. Window handlers were ok, but declaration of functions was wrong (still ok in GUI, which tricked me). Now I'm using 'windows' (works fine for ShowWindow and SetWindowPos).
The simple code is this:
Code: Pascal  [Select]
  1. {$include c:\lazarus\fpc\3.0.4\source\rtl\wince\wininc\coredll.inc}
  2. program Project1;
  3. uses  windows;
  4. function FindWindow(lpClassName:widestring; lpWindowName:widestring): thandle; stdcall; external 'coredll.dll' name 'FindWindowW';
  5. var
  6.     hwnd, hwnd2: thandle;
  7. begin
  8.     hwnd:=FindWindow('49154','');
  9.     hwnd2:=FindWindow('SystemInformation.Class','');
  10.     SetWindowPos(hwnd, hwnd2, 0, 0, 0, 0, 19);
  11. end.
  12.  

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: [SOLVED] a simple program for WinCE with API calls
« Reply #7 on: February 13, 2018, 11:40:56 pm »
Thanks for the feedback doni1, and congratulations  :)

Since the topic was raised in this thread, i would suggest a bit more defensive approach (using layman's logging):

Code: Pascal  [Select]
  1. program test;
  2.  
  3. {$MODE OBJFPC}
  4.  
  5. uses
  6.   Classes, SysUtils, Windows;
  7.  
  8. function FindWindow(lpClassName:widestring; lpWindowName:widestring): thandle; stdcall; external 'coredll.dll' name 'FindWindowW';
  9.  
  10. const
  11.   wnd1classname = '49154';
  12.   wnd2classname = 'SystemInformation.Class';
  13. var
  14.   Log: TStringList;
  15.   hwnd1, hwnd2: thandle;
  16. begin
  17.   Log := TStringList.Create;
  18.   try
  19.     hwnd1 := FindWindow(wnd1classname,'');
  20.     if (hwnd1 <> 0) then
  21.     begin
  22.       Log.Add('Window with classname ' + wnd1classname + ' found');
  23.       hwnd2 := FindWindow(wnd2classname,'');
  24.       if (hwnd2 <> 0) then
  25.       begin
  26.         Log.Add('Window with classname ' + wnd2classname + ' found');
  27.         if SetWindowPos(hwnd1, hwnd2, 0, 0, 0, 0, 19)
  28.         then Log.Add('SetWindowPos succeeded')
  29.         else Log.Add('SetWindowPos failed')
  30.       end
  31.       else Log.Add('Unable to find window with classname ' + wnd2classname);
  32.     end
  33.     else Log.Add('Unable to find window with classname ' + wnd1classname);
  34.   finally
  35.    Log.SaveToFile('log.txt');
  36.    Log.Free;
  37.   end;
  38. end.
  39.  

Make sure you are writing the log to a location where the user/program has sufficient access rights.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus